C#TUTORIALS

Now in this article i am going to explain with example How to bind, save/insert, edit, update and delete

 Implementation: Let's create an asp.net application to understand the operations in repeater data control

 

  using System;
using System.Collections.Generic; using System.Collections; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Data.Sql; using System.Configuration; namespace WebApplication1 { public partial class index : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn = conn = new SqlConnection("Data Source=DESKTOP-REPQT0F;Initial Catalog=hotels;Integrated Security=True"); conn.Open(); string cm = "insert into Student (Name,Place) values(@Name,@Place)"; SqlCommand cmd = new SqlCommand(cm, conn); cmd.Parameters.AddWithValue("@Name", TextBox1.Text); cmd.Parameters.AddWithValue("@Place", TextBox2.Text); cmd.ExecuteNonQuery(); conn.Close(); } protected void Button2_Click(object sender, EventArgs e) { var conn = new SqlConnection("Integrated Security=SSPI;" + "Persist Security Info=False;" + "Initial Catalog=Hotels;Data Source=DESKTOP-REPQT0F"); var query = "SELECT Name ,Place FROM Student"; conn.Open(); SqlCommand cmd = new SqlCommand(query, conn); SqlDataReader rdr = cmd.ExecuteReader(); GridView1.DataSource = rdr; GridView1.DataBind(); conn.Close(); } protected void Button3_Click(object sender, EventArgs e) { var conn = new SqlConnection("Integrated Security=SSPI;" + "Persist Security Info=False;" + "Initial Catalog=hotels;Data Source=DESKTOP-REPQT0F"); conn.Open(); string sql = "UPDATE Hotel SET Name='" + TextBox1.Text + "',Place='" + TextBox2.Text + "'"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); GridView1.DataBind(); conn.Close(); update(); } private void update() { var conn = new SqlConnection("Integrated Security=SSPI;" + "Persist Security Info=False;" + "Initial Catalog=Hotels;Data Source=DESKTOP-REPQT0F"); conn.Open(); string sql = "UPDATE student SET Name='" + TextBox1.Text + "',Place='" + TextBox2.Text + "' WHERE Place='Marmaris'"; SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "student"); GridView1.DataSource = ds.Tables["student"]; SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); } protected void Button4_Click1(object sender, EventArgs e) { var conn = new SqlConnection("Integrated Security=SSPI;" + "Persist Security Info=False;" + "Initial Catalog=Hotels;Data Source=DESKTOP-REPQT0F"); conn.Open(); string sql = "DELETE FROM Student WHERE Name=@Name"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@Name", TextBox1.Text); cmd.ExecuteNonQuery(); conn.Close(); } } }

 
< src="select.zip" >