CODING ISSUE

Ajay Kembale

Total Post:2

Points:14
Posted by  Ajay Kembale
C#
 799  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. Sunil Singh

    Post:25

    Points:177
    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 Jun-17-2015 07:20:54 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!