Friday 4 October 2013

Getting the Records from the database using sqlserver

using system.data;
using system.data.sqlclient;
using system.configuration;

SqlConnection con = new SqlCo<b></b>nnection("Server=ServerName;User Id=sa;Password=sa123;Database=databasename");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}

}
private void BindData()
{
try
{
con.Open();
SqlCommand com = new SqlCommand("select * from TableName", con);
com.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds, "TableName");
grid.DataSource = ds.Tables["TableName"];
grid.DataBind();
con.Close();
lbldisplay.Text = "records are found";
}
catch
{
lbldisplay.Text = "records are not found";
}
}

No comments:

Post a Comment