Coding issue

Total Post:2

Points:14
C#
 1114  View(s)
Ratings:
Rate this:
 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

  1. Post:45

    Points:323
    Re: Coding issue

    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();
    }
    }

      Modified On Apr-10-2018 12:19:31 AM

Answer