Friday, October 17, 2008

Display data in GridView in asp.net

Display data in GridView in asp.net:
using System.Data;

using System.Data.SqlClient;
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)

{
SqlConnection cn = new SqlConnection("Server=localhost;database=test;uid=sa;pwd="); SqlDataAdapter da = new SqlDataAdapter("Select * from Products", cn);
DataSet ds = new DataSet();
try
{
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
cn.Close();
da.Dispose();
ds.Dispose();
}
}
}