Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 6 > C# 6 General Discussion
|
C# 6 General Discussion Discussions about the C# 6 language and tools not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 6 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2016, 10:47 AM
Registered User
 
Join Date: Jul 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Insert, Delete, Update in GridView in ASP.Net using C#

Hi guys

I am learing asp.net programming please help me i want to insert ,delete,update in asp.net grid view using c#.

thanks
 
Old July 8th, 2016, 04:17 AM
Registered User
 
Join Date: Dec 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default insert, delete, update in gridView in asp.net using C#

Hi Check this artciles.I hope it will solve your problem.

http://www.onlinebuff.com/article_st...adonet_32.html

https://www.mindstick.com/Articles/9...-using-c-sharp


http://www.aspneto.com/add-insert-ed...-net-c-vb.html
 
Old July 14th, 2016, 05:21 AM
katieswift
Guest
 
Posts: n/a
Default

Delete:
protected void GridViewSample_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

Label CustID = (Label)GridViewSample.Rows[e.RowIndex].FindControl("lblCustID");

using (SqlConnection Sqlcon = new SqlConnection(strCon))

{

using (SqlCommand cmd = new SqlCommand())

{

Sqlcon.Open();

cmd.Connection = Sqlcon;

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "SP_Customer";

cmd.Parameters.Add(new SqlParameter("@pvchCustId", SqlDbType.Int));

cmd.Parameters.Add(new SqlParameter("@pvchAction", SqlDbType.VarChar, 50));

cmd.Parameters.Add(new SqlParameter("@pvchCreatedBy", SqlDbType.VarChar, 50));

cmd.Parameters.Add("@pIntErrDescOut", SqlDbType.Int).Direction = ParameterDirection.Output;

cmd.Parameters["@pvchAction"].Value = "delete";

cmd.Parameters["@pvchCustId"].Value = Convert.ToInt32(CustID.Text.ToString());

cmd.Parameters["@pvchCreatedBy"].Value = "Admin";

cmd.ExecuteNonQuery();

int retVal = (int)cmd.Parameters["@pIntErrDescOut"].Value;

}

}

GridViewSample.EditIndex = -1;

LoadData();

lblMessage.Text = "Record deleted successfully!";

}

insert:
protected void GridViewSample_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName.Equals("Insert"))

{

int retVal=0;

TextBox Name = (TextBox)GridViewSample.FooterRow.FindControl("txt AddCustName");

TextBox Address1 = (TextBox)GridViewSample.FooterRow.FindControl("txt AddCustAdres1");

TextBox Address2 = (TextBox)GridViewSample.FooterRow.FindControl("txt AddCustAdres2");

TextBox Phone = (TextBox)GridViewSample.FooterRow.FindControl("txt AddCustPhone");

TextBox Email = (TextBox)GridViewSample.FooterRow.FindControl("txt AddEmail");

using (SqlConnection Sqlcon = new SqlConnection(strCon))

{

using (SqlCommand cmd = new SqlCommand())

{

Sqlcon.Open();

cmd.Connection = Sqlcon;

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "SP_Customer";

cmd.Parameters.Add(new SqlParameter("@pvchAction", SqlDbType.VarChar, 50));

cmd.Parameters.Add(new SqlParameter("@pvchCustName", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchustaddress1", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchustaddress2", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchCustPhone", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchCustEmail", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchCreatedBy", SqlDbType.VarChar, 100));

cmd.Parameters.Add("@pIntErrDescOut", SqlDbType.Int).Direction = ParameterDirection.Output;

cmd.Parameters["@pvchAction"].Value = "insert";

cmd.Parameters["@pvchCustName"].Value = Name.Text.Trim();

cmd.Parameters["@pvchustaddress1"].Value = Address1.Text.Trim();

cmd.Parameters["@pvchustaddress2"].Value = Address2.Text.Trim();

cmd.Parameters["@pvchCustPhone"].Value = Phone.Text.Trim();

cmd.Parameters["@pvchCustEmail"].Value = Email.Text.Trim();

cmd.Parameters["@pvchCreatedBy"].Value = "Admin";

cmd.ExecuteNonQuery();

retVal = (int)cmd.Parameters["@pIntErrDescOut"].Value;

}

}

if (retVal == 2)

{

lblMessage.Text = "Customer name or Email ID aready exist!";

}

else

{

GridViewSample.EditIndex = -1;

LoadData();

lblMessage.Text = "Record inserted successfully!";

}

}

}

update:
protected void GridViewSample_RowEditing(object sender, GridViewEditEventArgs e)

{

GridViewSample.EditIndex = e.NewEditIndex;

LoadData();

}

protected void GridViewSample_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

Label Custid = (Label)GridViewSample.Rows[e.RowIndex].FindControl("lblCustID");

TextBox Name = (TextBox)GridViewSample.Rows[e.RowIndex].FindControl("txtCustName");

TextBox Address1 = (TextBox)GridViewSample.Rows[e.RowIndex].FindControl("txtCustAddres1");

TextBox Address2 = (TextBox)GridViewSample.Rows[e.RowIndex].FindControl("txtCustAddres2");

TextBox Phone = (TextBox)GridViewSample.Rows[e.RowIndex].FindControl("txtCustPhone");

TextBox Email = (TextBox)GridViewSample.Rows[e.RowIndex].FindControl("txtEmail");

using (SqlConnection Sqlcon = new SqlConnection(strCon))

{

using (SqlCommand cmd = new SqlCommand())

{

Sqlcon.Open();

cmd.Connection = Sqlcon;

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "SP_Customer";

cmd.Parameters.Add(new SqlParameter("@pvchAction", SqlDbType.VarChar, 50));

cmd.Parameters.Add(new SqlParameter("@pvchCustId", SqlDbType.Int));

cmd.Parameters.Add(new SqlParameter("@pvchCustName", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchustaddress1", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchustaddress2", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchCustPhone", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchCustEmail", SqlDbType.VarChar, 100));

cmd.Parameters.Add(new SqlParameter("@pvchCreatedBy", SqlDbType.VarChar, 100));

cmd.Parameters.Add("@pIntErrDescOut", SqlDbType.Int).Direction = ParameterDirection.Output;

cmd.Parameters["@pvchAction"].Value = "update";

cmd.Parameters["@pvchCustId"].Value = Convert.ToInt32(Custid.Text.ToString());

cmd.Parameters["@pvchCustName"].Value = Name.Text.Trim();

cmd.Parameters["@pvchustaddress1"].Value = Address1.Text.Trim();

cmd.Parameters["@pvchustaddress2"].Value = Address2.Text.Trim();

cmd.Parameters["@pvchCustPhone"].Value = Phone.Text.Trim();

cmd.Parameters["@pvchCustEmail"].Value = Email.Text.Trim();

cmd.Parameters["@pvchCreatedBy"].Value = "Admin";

cmd.ExecuteNonQuery();

int retVal = (int)cmd.Parameters["@pIntErrDescOut"].Value;

}

}

GridViewSample.EditIndex = -1;

LoadData();

lblMessage.Text = "Record updated successfully!";

}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Update/delete using gridview dhall ASP.NET 4 General Discussion 0 March 23rd, 2016 03:44 PM
data gridview update, delete and insert to multi database khoonng BOOK: Beginning ASP.NET 4 : in C# and VB 1 December 13th, 2013 04:17 AM
Insert, update, delete in gridview-!!! URGENT !!! dagad ASP.NET 2.0 Basics 0 September 27th, 2006 06:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.