Coding issue

 Protected
void BindGridview()
{
using(SqlConnection conn = new SqlConnection("Data Source=abc;Integrated Security=true;Initial Catalog=Test"))
{
con.Open();
SqlCommand cmd =new SqlCommand("Select UserName, First Name,LastName,Location FROM Users", conn);
SqlDataReader sdr =cmd.ExecuteReader();
gvUserInfo.DataSource = sdr;
gvUserInfo.DataBind();
conn.Close();
}
}


Its code I was trying found on  google, what is 'using' here and
'gvUserInfo' please explain I am started with C# basics recently

Last updated:6/17/2015 3:34:48 AM

1 Answers

Sunil Singh
Sunil Singh

Try like this.

Protected void BindGridview()
{
DataTable dt = new DataTable();
  using (SqlConnection conn = new SqlConnection('Data Source=abc;Integrated Security=true;Initial Catalog=Test'))
{
conn.Open();
                SqlCommand cmd = new SqlCommand('Select UserName, First Name,LastName,Location FROM Users', conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                gvUserInfo.DataSource = dt;
gvUserInfo.DataBind();
                conn.Close();
}
}

Answer