1)Create a property class have all the property what ever fields you want to add the generic class
suppose we have Created a class Name userInfo
2)Create a another class for Generic list suppose we are creating a class GenricList
3)Import the above two Name space in your class
using System.Collections.Generic;
using System.Data.SqlClient;
4)Create the object of IList Generic class in class GenericList
Like this:
IList
5)Create a function in class GenericList for Add the value in Generic have IList
Like:
public System.Collections.Generic.IList
{
//SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
// = new IList
string Qstring = "select * from UserRegistration";
using( SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString))
{
SqlCommand cmd = cn.CreateCommand();
cmd.CommandText = Qstring;
cn.Open();
SqlDataReader red = cmd.ExecuteReader();
while (red.Read())
{
UserInfo ObjUserInfo = new UserInfo(red[4].ToString(),red[5].ToString());
UserInfos.Add(ObjUserInfo);
}
red.Close();
return UserInfos;
}
}
//Explanation of function FillList() setp by step
A)Declare a string and Assign SQL statement or procedure name to it
B)Declare and Create the Connection string using SqlConnection
C)Fetch the Data from database using SqlDataReader
D)If you reader gets any row from database than apply for reading the values form SqlDataReader
D)Crate the object of property class like our property class name is userInfo
E)Assign the value of dataReader to the corresponding property define in userInfo class
F)Add this Created Object into Generic List class object
G) And At last return this Generic list class object
//Operation for aspx page
6)Add the GridView on your aspx page
7)Create a function of GenricList Class
8)Create a object of Generic IList
9)Call the FillList() Function and assign the its return value to Generic IList Object
10)Assign the Generic Ilist Object to GridView as DataSource and Bind The Grid
//How to fech the Value form Ilist
for (int i = 0; i<=User.Count - 1; i++)
{
Response.Write("UserName: "+Convert.ToString(User[i].UserName) + " ," +"Password:"+ Convert.ToString(User[i].Password)+"
");
}
Note:- We cannot convert the Genrice List object to Dataset or DataTable its better alternet of DataSet or DataTable Its Incress the performance of System
//All code of Generic List
//.cs class file code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
///
/// Summary description for Col
///
public class UserInfo
{
private string _UserName = string.Empty;
private string _Password = string.Empty;
public UserInfo(string UserName,string Password)
{
this._UserName = UserName;
this._Password = Password;
//
// TODO: Add constructor logic here
//
}
public string UserName
{
get { return _UserName; }
}
public string Password
{
get { return _Password; }
}
}
///
///This Class is for Genric
///
///
public class GenricList
{
IList
public GenricList()
{
}
public System.Collections.Generic.IList
{
//SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
// = new IList
string Qstring = "select * from UserRegistration";
using( SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString))
{
SqlCommand cmd = cn.CreateCommand();
cmd.CommandText = Qstring;
cn.Open();
SqlDataReader red = cmd.ExecuteReader();
while (red.Read())
{
UserInfo ObjUserInfo = new UserInfo(red[4].ToString(),red[5].ToString());
UserInfos.Add(ObjUserInfo);
}
red.Close();
return UserInfos;
}
}
}
//put above code on your aspx.cs page
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
GenricList ObjGenricList = new GenricList();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
System.Collections.Generic.IList
User = ObjGenricList.FillList();
grd.DataSource = User;//Assign the DataSource to GridView
grd.DataBind();
for (int i = 0; i<=User.Count - 1; i++)
{
//Reading the Row by row Data For List
Response.Write("UserName: "+Convert.ToString(User[i].UserName) + " ," +"Password:"+ Convert.ToString(User[i].Password)+"
");
}
}
}
No comments:
Post a Comment