Two ways of Implementing CRUD Operations in Aspnet with C#
Hi friends,In this article i would like to explain the procedure of CRUD Operations in two ways.we already know Creation,Updation,Deletion operations are very Important.So lets start with First way that is Implementing CRUD operations Outside of the GRID.that means while we are the Editing GRID the data will be Bind to the Forms Controls.
In the second way Inside the grid , that means while we are the Editing GRID the data will be bind inside. In my view First method is best. why because, 1. Editing the data in Form controls is easy instead of editing Grid controls
2. No need to apply validations again because we are already applying validations at the time of Form creation,but here w need to apply validations again for Grid Controls
So lets the First method
First, Open the Default.aspx.cs page
Next,In the Page Load Bind the Grid as follows
protected void Page_Load(object sender, EventArgs e) { try { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Testing"].ToString()); if (!Page.IsPostBack) { BindData(); } } catch (Exception ex) { lblstatus.Text=ex.message; } }
Next,Create a method BindData()
private void BindData() { try { if (con.State == ConnectionState.Closed) { con.Open(); } SqlCommand com = new SqlCommand("select * from Testing ", con); SqlDataAdapter da = new SqlDataAdapter(com); DataSet ds = new DataSet(); da.Fill(ds, "Testing"); GvTesting.DataSource = ds; GvTesting.DataBind(); } catch (Exception ex) { lblstatus.Text = ex.Message; } }
Next,Apply the Grid Editing Operation
Editing Procedure
protected void GvTesting_RowEditing(object sender, GridViewEditEventArgs e) { try { int id = Convert.ToInt32(GvTesting.DataKeys[e.NewEditIndex].Value); com = new SqlCommand("select * from Testing where TestingId='" + id + "'", con); SqlDataAdapter da = new SqlDataAdapter(com); DataSet ds = new DataSet(); da.Fill(ds, "Testing"); if (ds.Tables["Testing"].Rows.Count != 0) { DataRow dr = ds.Tables["hulling"].Rows[0]; bindItems(dr); } } catch (Exception ex) { lblstatus.Text = ex.Message; } }
Write the following code to bind the data in Form controls and Hide the submit button and display the Update button
private void bindItems(DataRow dr) { txtName.Text = dr["Name"].ToString(); txtSource.Text = dr["Source"].ToString(); lblTestingIdId.Text = dr["TestingId"].ToString(); btnupdate.Visible = true; btnsubmit.Visible = false; }
Next,Write the following code to update the data in database
Updating Procedure
protected void btnupdate_Click(object sender, EventArgs e) { try { com = new SqlCommand("Update Testing set Name='"+txtName.Text+"',txtSource='"+txtSource.Text+"' where lblTextingId='"+TestingId+"'",con); com.ExecuteNonQuery(); lblstatus.Text = "Record Updated"; lblstatus.ForeColor = Color.Green; BindData(); btnupdate.Visible = false; btnsubmit.Visible = true; } catch (Exception ex) { lblstatus.Text = ex.Message; } }
Now, write the following code to delete the record from Testing table
Deletion procedure
protected void GvTesting_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { string id = GvTesting.DataKeys[e.RowIndex].Value.ToString(); SqlCommand cmdDelete = new SqlCommand(); cmdDelete.CommandText = "Delete Testing where TestingId=" + int.Parse(id); cmdDelete.Connection = con; if (con.State == ConnectionState.Closed) con.Open(); cmdDelete.ExecuteNonQuery(); con.Close(); BindData(); lblstatus.Text = "Record Successfully Deleted"; lblstatus.ForeColor = Color.Red; } catch (Exception ex) { lblstatus.Text = "Falied to Delete Record"; lblstatus.ForeColor = Color.Red; } }
Next,write the following code to set the index
protected void GvTestingRowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GvTesting.EditIndex = -1; }
That's it.First method is very easy..Now coming to second Method that is CRUD Operations will be applied inside the Grid.. I have already explained this article check out this link
That's it..Happy Coding.:)
Labels: .Net Tutorials, Aspnet, C#, Gridview, SqlServer, Tanisha
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home