protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString["id"] != null && Request.QueryString["action"] != null) { string id = Request.QueryString["id"]; string action = Request.QueryString["action"]; string name = null; string contact = null; string location = null; con.Open(); cmd = new SqlCommand("Select * from PhoneBook where Id='" + id + "'", con); dr = cmd.ExecuteReader(); if (dr.Read()) { name = dr["Name"].ToString(); contact = dr["Contact"].ToString(); location = dr["Location"].ToString(); } con.Close(); if (action == "1") { con.Open(); cmd = new SqlCommand("Delect from PhoneBook where id='" + id + "'", con); int checkD = cmd.ExecuteNonQuery(); if (checkD == 1) { ClientScript.RegisterStartupScript(this.GetType(), "deleted", "<script>alert('Contact Deleted...!'); location='PhoneBook.aspx';</script>"); } else { ClientScript.RegisterStartupScript(this.GetType(), "fail", "<script>alert('Failed...!');</script>"); } con.Close(); } else if (action == "2") { ButtonAdd.Enabled = false; ButtonUpdate.Enabled = true; Session["id"] = id; TextBoxName.Text = name; TextBoxContact.Text = contact; TextBoxLocation.Text = location; } } con.Open(); cmd = new SqlCommand("select * from PhoneBook", con); dr = cmd.ExecuteReader(); if (dr.HasRows) { GridViewPhoneBook.DataSource = dr; GridViewPhoneBook.DataBind(); } con.Close(); } }
private void showdata() { dt = new DataTable(); conn.Open(); sda = new SqlDataAdapter("SELECT * FROM PhoneBook ORDER BY ID", conn); sda.Fill(dt); if (dt.Rows.Count > 0) { GridViewPhoneBook.DataSource = dt; GridViewPhoneBook.DataBind(); } conn.Close(); }