public void UpdateGridview() { SqlConnection conn = new SqlConnection(@"data source = .\sqlexpress; integrated security = true; database = Patient_dentist;"); SqlDataAdapter da = null; DataSet ds = null; DataTable dt = null; //string sqlsel = "SELECT tr_name, tr_price, tr_date from all_treatments"; string sqlsel = "SELECT * from Treatment"; try { // conn.Open(); //SqlDataAdapter will open the connection by itself da = new SqlDataAdapter(); // da = data adapter da.SelectCommand = new SqlCommand(sqlsel, conn); ds = new DataSet(); da.Fill(ds, "MyTreatments"); dt = ds.Tables["MyTreatments"]; GridViewUpdateTR.DataSource = dt; GridViewUpdateTR.DataBind(); } catch (Exception ex) { LabelMessageUpdateTR.Text = ex.Message; } finally { conn.Close(); //SqlDataAdapter will close the connection by itself, but it can fail in case of errors } }
public void UpdateGridview() { SqlConnection conn = new SqlConnection(@"data source = .\sqlexpress; integrated security = true; database = Patient_dentist;"); SqlCommand cmd = null; SqlDataReader rdr = null; try { conn.Open(); cmd = conn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "MySelectTR"; SqlParameter in1 = cmd.Parameters.Add("@ID_treatment", SqlDbType.Int); in1.Direction = ParameterDirection.Input; in1.Value = Convert.ToInt32(Session["id"].ToString()); rdr = cmd.ExecuteReader(); GridViewUpdateTR.DataSource = rdr; GridViewUpdateTR.DataBind(); rdr.Close(); rdr = cmd.ExecuteReader(); DropDownListTR.DataSource = rdr; DropDownListTR.DataTextField = "ID_treatment"; DropDownListTR.DataValueField = "ID_treatment"; DropDownListTR.DataBind(); DropDownListTR.Items.Insert(0, "Select a treatment ID"); } catch (Exception ex) { LabelMessageCRR.Text = ex.Message; } finally { conn.Close(); } }