Click here to Skip to main content
15,313,782 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hai All,when i'm trying develop the infinite scrolling i'm facing the problem like connectionstring is not intiallizzed ,bbut my other pages are working fine with the same connection string.i'll share my page can some body helps me what is the wrong in it.and i'm sharing my handler page also .i thhink it is easy to get my problem
my DataClass.cs:
C#
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()
    {


    }


    public DataTable GetTable(string SPName, Dictionary<string, object> SPParamWithValues)
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter adapter;

        DataTable dataTable = new DataTable();

            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);


        return dataTable;
    }
}

and my handlerr.aspx is

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string startQstring = context.Request.QueryString["start"];
        string nextQstring = context.Request.QueryString["next"];
        //null check
        if ((!string.IsNullOrWhiteSpace(startQstring)) && (!string.IsNullOrWhiteSpace(nextQstring)))
        {
            //convert string to int
            int start = Convert.ToInt32(startQstring);
            int next = Convert.ToInt32(nextQstring);
            
            //setting content type
            context.Response.ContentType = "text/plain";
            DataClass data = new DataClass();
            //writing response
            context.Response.Write(data.GetAjaxContent(start, next));
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}

Thanks and Regards
Posted
Updated 26-Sep-13 10:22am
v2
Comments
[no name] 26-Sep-13 15:26pm
   
If you expected us to help you with a connection string not initialized problem, then wouldn't you think that you would need to show us some sort of code snippet that relates to a connection string?
adityaimmadi 26-Sep-13 15:29pm
   
That means my web.config?? it works fine for all the other pages .in this DataClass.cs only i'm facing the problem.thats why i came for help.i ust want to know what is wrong with my code sir.
Thanks and Regards
ZurdoDev 26-Sep-13 16:42pm
   
What line of code causes the error?
adityaimmadi 27-Sep-13 15:16pm
   
public DataTable GetTable(string SPName, Dictionary<string, object=""> SPParamWithValues)
{
SqlConnection conn;
SqlCommand cmd;
SqlDataAdapter adapter;

DataTable dataTable = new DataTable();

conn = Provider.GetConnection();
//open DB connection
conn.Open();---at this line,it shows connection string error.

Thanks and Regards
ZurdoDev 27-Sep-13 15:41pm
   
So, Provider.GetConnection is not working right You'll have to step into that.
adityaimmadi 27-Sep-13 15:19pm
   
public DataTable GetTable(string SPName, Dictionary<string, object=""> SPParamWithValues)
{
SqlConnection conn;
SqlCommand cmd;
SqlDataAdapter adapter;

DataTable dataTable = new DataTable();

conn = Provider.GetConnection();
//open DB connection
conn.Open();-----at this line i got error like connection string not intialized

Thnaks and Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900