Home > DeveloperSection > Forums > Coding issue
Ajay Kembale
Ajay Kembale

Total Post:2

Points:14
Posted on    June-16-2015 10:40 PM

 C# Database Connection 
Ratings:


 1 Reply(s)
 498  View(s)
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



Sunil Singh
Sunil Singh

Total Post:11

Points:77
Posted on    June-17-2015 3:34 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick