aditya immadi

aditya immadi

  • NA
  • 205
  • 71.6k

connection string intialization failed

Sep 26 2013 10:58 AM
Hai all, when i'm developing infinite scolll down i need to use this handler .aspx .it looks like everything is fine to me but connection string is failed i'm providing youo my page and web.config.can any one help me 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for DataClass
/// </summary>
/// 


public class DataClass
{
    public DataClass()
    {
    }
    /// <summary>
    ///  return rows depend on position
    ///  if you need 10th to 20th you need to pass start=10 and end=20
    /// </summary>
    /// <param name="start">database start position of one row</param>
    /// <param name="next">database end position of one row</param>
    /// <returns></returns>
    public string GetAjaxContent(int start, int end)
    {
        string result = string.Empty;
        //adding sp params with values in Dictionary entry.
        Dictionary<string, object> keyValPair = new Dictionary<string, object>();
        keyValPair.Add("@start", start);
        keyValPair.Add("@next", end);

        DBHelper DBHelper = new DBHelper();
        //passing the Stored Procedure name and keyvalue pair
        DataTable dataTable = DBHelper.GetTable("spuserdata", keyValPair);
        if (dataTable.Rows.Count > 0)
        {
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                result += string.Format(@"<tr>
                                                        <td>
                                                            <table>
                                                                <tr>
                                                                    <td style='width:50px;'>{0}</td><td style='width:400px;'>{1}</td><td style='width:150px;'>{2}</td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                   </tr>", dataTable.Rows[i][0].ToString(), dataTable.Rows[i][1].ToString(), dataTable.Rows[i][2].ToString());
            }

        }
        //this string is going to append on Datalist on client.
        return result;
    }
    /// <summary>
    /// function to bind data on page load
    /// </summary>
    /// <returns></returns>
    public DataTable FirstTenRecords()
    {
        Dictionary<string, object> keyValPair = new Dictionary<string, object>();
        keyValPair.Add("@start", 0);
        keyValPair.Add("@next", 10);

        DBHelper DBHelper = new DBHelper();
        DataTable dataTable = DBHelper.GetTable("spuserdata", keyValPair);
        return dataTable;
    }
}

/// <summary>
/// return sqlconnection string formweb.config file
/// </summary>
public class Provider
{
    public static SqlConnection GetConnection()
    {
        return new SqlConnection(ConfigurationManager.AppSettings["conn"]);
    }
}
/// <summary>
/// Data layer
/// </summary>
public class DBHelper
{
    public DBHelper()
    { }

    SqlConnection conn;
    SqlCommand cmd;
    SqlDataAdapter adapter;
    public DataTable GetTable(string SPName, Dictionary<string, object> SPParamWithValues)
    {
        DataTable dataTable = new DataTable();
        try
        {
            conn = Provider.GetConnection();
            //open DB connection
            conn.Open();
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;
            cmd.CommandText = SPName;
            foreach (KeyValuePair<string, object> paramValue in SPParamWithValues)
            {
                cmd.Parameters.AddWithValue(paramValue.Key, paramValue.Value);
            }
            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(dataTable);
        }
        finally
        {
            //close connection string
            conn.Close();
        }
        return dataTable;
    }
}\





and my web.config

<connectionStrings>
<add name="conn" connectionString="Data Source=USER-PC\ADITYA;Initial Catalog=holder;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
</connectionStrings>


Thanks and Regards

Answers (6)