public void UpdateGridView() { SqlConnection conn = new SqlConnection(@"data source = DESKTOP-VKU3EK5; integrated security = true; database = ExterminatorDB"); SqlCommand cmd = null; SqlDataReader rdr = null; string sqlsel = "SELECT Request.RequestID, Customer.CustomerID, Customer.Firstname, Customer.Lastname, Pest.PestID, Pest.Name, Request.Date " + "FROM Request " + "INNER JOIN Customer ON Request.CustomerID = Customer.CustomerID " + "INNER JOIN Pest ON Request.PestID = Pest.PestID"; //string sqlsel = "SELECT * FROM Request"; try { conn.Open(); cmd = new SqlCommand(sqlsel, conn); rdr = cmd.ExecuteReader(); GridViewRequest.DataSource = rdr; GridViewRequest.DataBind(); rdr.Close(); rdr = cmd.ExecuteReader(); DropDownListDelete.DataSource = rdr; DropDownListDelete.DataTextField = "RequestID"; DropDownListDelete.DataValueField = "RequestID"; DropDownListDelete.DataBind(); DropDownListDelete.Items.Insert(0, "Select a Request"); } catch (Exception ex) { LabelMessage.Text = ex.Message; } finally { conn.Close(); } }
public void UpdateGridview() { //SqlConnection conn = new SqlConnection(@"Data Source = DESKTOP-6CQP77U; integrated security = true; database = PestUpdated2"); SqlConnection conn = new SqlConnection(@"Data Source = localhost\SQLEXPRESS; integrated security = true; database = pestupdate"); SqlDataAdapter da = null; DataSet ds = null; DataTable dt = null; string sqlsel = "select * from Pests"; try { // conn.Open(); SqlDataAdapters open connection by itself. da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(sqlsel, conn); ds = new DataSet(); da.Fill(ds, "DelPests"); dt = ds.Tables["DelPests"]; GridViewpests.DataSource = dt; GridViewpests.DataBind(); DropDownListDelete.DataSource = dt; DropDownListDelete.DataTextField = "pestname"; DropDownListDelete.DataValueField = "idpest"; DropDownListDelete.DataBind(); DropDownListDelete.Items.Insert(0, "Select a Pest"); } catch (Exception ex) { LabelMessage.Text = ex.Message; } finally { conn.Close(); // SqlDataAdapter closes connection by itself; but can fail in case of errors. } }