Your code is not proper, you do not close the connection properly also there's no diposing of objects. Use below function.
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable ();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].
ConnectionString;
SqlConnection con =new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return null;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
//To call
string strQuery = "select * from customers";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
GridView1.DataSource = dt;
GridView1.DataBind();