public void ShowData() { try { //open connection. this establishes a session with the database conn.Open(); ErrorLabel.Text = ""; SqlString = "Select * from Treatments"; cmd = new SqlCommand(SqlString, conn); rdr = cmd.ExecuteReader(); TreatmentRepeater.DataSource = rdr; TreatmentRepeater.DataBind(); } catch (Exception ex) { //Display error ErrorLabel.Text = "Error: " + ex.Message + ex.StackTrace; } finally { conn.Close(); } }
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ButtonDelete.Enabled = false; ButtonUpdate.Enabled = false; UpdateGridview(); } if (IsPostBack) { ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#MOVEHERE';", true); } DropDownListTreatments.AutoPostBack = true; TreatmentRepeater.DataBind(); ShowData(); if (Session["DentistName"] != null) { LabelUser.Text = Session["DentistName"].ToString(); Master.ShowContent = true; Master.ShowLogout = true; } else { Master.ShowContent = false; Master.ShowLogout = false; } }
public void UpdateGridview() { SqlDataAdapter da = null; DataSet ds = null; DataTable dt = null; string sqlsel = "select * from treatments"; try { //conn.Open(); SqlDataAdapter opens the connextion itself da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(sqlsel, conn); ds = new DataSet(); da.Fill(ds, "MyTreatments"); /* * dt.Columns.Add("SponsorID", typeof(Int32)); * dt.Columns.Add("Name", typeof(string)); * dt.Columns.Add("Website", typeof(string)); * dt.Columns.Add("Picture", typeof(string));*/ dt = ds.Tables["MyTreatments"]; /* * ds = new DataSet("Sponsors"); * dt = ds.Tables.Add("Sponsor"); * * dt.Columns.Add("SponsorID", typeof(Int32)); * dt.Columns.Add("Name", typeof(string)); * dt.Columns.Add("Website", typeof(string)); * dt.Columns.Add("Picture", typeof(string)); * } */ TreatmentRepeater.DataSource = dt; TreatmentRepeater.DataBind(); DropDownListTreatments.DataSource = dt; DropDownListTreatments.DataTextField = "T_Name"; DropDownListTreatments.DataValueField = "T_Id"; DropDownListTreatments.DataBind(); DropDownListTreatments.Items.Insert(0, "Select a Treatment"); } catch (Exception ex) { LabelMessage.Text = ex.Message; } finally { conn.Close(); // SqlDataAdapter closes connextion by itself; but can fail in case of errors } }