Wednesday, 6 February 2013
how to show data in GridView - nice and simple code
using dataset
add connection string in web config :
<connectionStrings>
<add name="ConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=USER-PC\SQLEXPRESS;Initial Catalog=master;User id= sa;Password=sa"/>
</connectionStrings>
in .cs page :
string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString(); SqlConnection con = new SqlConnection(str); con.Open(); string s = "select * from employee"; SqlDataAdapter adp = new SqlDataAdapter(s, con); DataSet ds = new DataSet(); adp.Fill(ds, "dataset"); GridView1.DataSource = ds; GridView1.DataBind(); con.Close();
or
SqlConnection con=new SqlConnection(<mention here your connectionstring>);
con.Open();
SqlCommand cmd=new SqlCommand("select * from sam",con);
\\where 'rohit' is table name
SqlDataReader dr=cmd.ExecuteReader();
DataTable dt=new DataTable();
dt.Load(dr);
GridView1.DataSourece=dt;
GridView1.DataBind();
con.Close();
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment