using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
///
/// Summary description for DataClass
///
///
public class DataClass
{
public DataClass()
{
}
///
/// return rows depend on position
/// if you need 10th to 20th you need to pass start=10 and end=20
///
/// database start position of one row
/// database end position of one row
///
public string GetAjaxContent(int start, int end)
{
string result = string.Empty;
//adding sp params with values in Dictionary entry.
Dictionary keyValPair = new Dictionary();
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(@"
", dataTable.Rows[i][0].ToString(), dataTable.Rows[i][1].ToString(), dataTable.Rows[i][2].ToString());
| {0} | {1} | {2} |
}
}
//this string is going to append on Datalist on client.
return result;
}
///
/// function to bind data on page load
///
///
public DataTable FirstTenRecords()
{
Dictionary keyValPair = new Dictionary();
keyValPair.Add("@start", 0);
keyValPair.Add("@next", 10);
DBHelper DBHelper = new DBHelper();
DataTable dataTable = DBHelper.GetTable("spuserdata", keyValPair);
return dataTable;
}
}
///
/// return sqlconnection string formweb.config file
///
public class Provider
{
public static SqlConnection GetConnection()
{
return new SqlConnection(ConfigurationManager.AppSettings["conn"]);
}
}
///
/// Data layer
///
public class DBHelper
{
public DBHelper()
{ }
SqlConnection conn;
SqlCommand cmd;
SqlDataAdapter adapter;
public DataTable GetTable(string SPName, Dictionary 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 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
Thanks and Regards
Posted Sep 28, 2013, 10:41 AM
So please the check the number of items in the "SPParamWithValues" collection.
aditya immadiPosted Sep 27, 2013, 4:25 PM
Thanks for your help.i resolved it,after that a small problem is there at this line in DataClass.cs
here is my stored procedure ,as for i know this error is because when we are specifying more parameters in your c# code than our stored procedure accepts..but i dont understand what are the extra parameters which i add
and here is my stored proc ::
how an i resolve this
Thnaks and Regards
aditya immadiPosted Sep 27, 2013, 3:06 PM
Posted Sep 26, 2013, 7:33 PM
aditya immadiPosted Sep 26, 2013, 2:29 PM
Thanks and Regards
Posted Sep 26, 2013, 12:14 PM
I did not see any value added option that you've a separate class just to return the sqlconnection.
You can move the sql connection into DBHelper class itself.
Can you please post your handler.aspx page?
If you class library project and it is referred in your project, make sure, you have added app.config file in your class library project also.
Hope this helps you.