How to get the data into gridview from sql in asp.net

Ranjeet_8
Posted by Ranjeet_8 under ASP.NET category on | Points: 40 | Views : 1802
using System.Data.SqlClient;
using System.Data;

SqlConnection con = new SqlConnection("Server=YourServerName;Initial Catalog=DatabaseName;User=UserName;password=YourPassword");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
try
{
con.Open();

SqlDataAdapter da = new SqlDataAdapter("select * from Table_Name", con);
DataTable ds = new DataTable();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = ex.Message.ToString();
}
finally
{
con.Close();
}

Comments or Responses

Login to post response