2013-09-27 103 views
0

當我試圖開發無限滾動時,我正面臨着稱爲connection string is not initialized的問題,但我的其他頁面使用相同的連接字符串正常工作。連接字符串未初始化

我會分享我的網頁,這樣有人可以幫助我告訴我它有什麼問題。我認爲讓有經驗的人解決我的問題很容易。

我DataClass.cs這是App.data文件夾:

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



    public class Provider 
{ 
public static SqlConnection GetConnection() 
{ 
    return new SqlConnection(ConfigurationManager.AppSettings["conn"]); 
} 
    } 

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

} 
+0

這是'ConfigurationManager.AppSettings [「conn」]'具體問題嗎? – Esteban

+0

對不起,先生,我沒有得到你,可以請你清楚解釋我 – user2772861

+0

@ user2772861你說它可以在其他頁面中使用。你能展示一些有效的代碼,所以我們可以發現它們的區別嗎? –

回答

0

試着改變SqlConnection(ConfigurationManager.AppSettings["conn"]);SqlConnection(ConfigurationManager.ConnectionStrings["conn"]);

除此之外,我沒有在您的代碼中看到任何奇怪的東西。請考慮查看評論並提供更多信息。