public void UpdateGridview() { SqlConnection conn = new SqlConnection(@"data source = .\sqlexpress; integrated security = true; database = northwind"); SqlDataAdapter da = null; DataSet ds = null; DataTable dt = null; string sqlsel = "select * from shippers"; try { //conn.Open(); SqlDataAdapter opens the connextion itself da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(sqlsel, conn); ds = new DataSet(); da.Fill(ds, "MyShippers"); //Display data dt = ds.Tables["MyShippers"]; GridViewShippers.DataSource = dt; GridViewShippers.DataBind(); //otherwise it won't display } catch (Exception ex) { LabelMessage.Text = ex.Message; } finally { conn.Close(); // SqlDataAdapter closes connection by itself; but can fail in case of errors } }
public void UpdateGridview() { SqlConnection conn = new SqlConnection(@"data source = .\sqlexpress; integrated security = true; database = northwind"); SqlCommand cmd = null; SqlDataReader rdr = null; try { conn.Open(); cmd = conn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Myselectallshippers"; rdr = cmd.ExecuteReader(); GridViewShippers.DataSource = rdr; GridViewShippers.DataBind(); } catch (Exception ex) { LabelMessage.Text = ex.Message; } finally { conn.Close(); } }
protected void LoadGridViewShippers() { ShipperCS ship = new ShipperCS(); GridViewShippers.DataSource = ship.GetShippers(); GridViewShippers.DataBind(); }