Update database table data using .NET using the form and database stored procedure
File Name: ViewCabDetails.aspx
This code is important CommandName="EditEmployee"
<asp:TemplateField HeaderText="Edit Detalis"> <ItemTemplate > <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("BookingReferenceNumber1") %>' runat="server" CommandName="EditEmployee">Edit</asp:LinkButton> </ItemTemplate> </asp:TemplateField>
File Name: ViewCabDetails.aspx
<div id="divEdit" runat="server"> <h1>Edit Information</h1> <table class="table table-hover table-bordered"> <tr> <td>Booking Id </td> <td> <asp:TextBox ID="bookRefNo" runat="server"></asp:TextBox> </td> </tr> <tr> <td>Passenger Name </td> <td> <asp:TextBox ID="PassengerName11" runat="server"></asp:TextBox> </td> </tr> <tr> <td> From Location </td> <td> <asp:DropDownList ID="FromLocation" runat="server"> <asp:ListItem Selected="True" Value="">Please Select</asp:ListItem> <asp:ListItem>Thiruvananthapuram</asp:ListItem> <asp:ListItem>Kochi</asp:ListItem> <asp:ListItem>Kozhikode</asp:ListItem> <asp:ListItem>Kollam</asp:ListItem> <asp:ListItem>Thrissur</asp:ListItem> <asp:ListItem>Kannur</asp:ListItem> <asp:ListItem>Alappuzha</asp:ListItem> </asp:DropDownList></td> </tr> <tr> <td> To Location </td> <td> <asp:DropDownList ID="ToLocation" runat="server"> <asp:ListItem Selected="True" Value="">Please Select</asp:ListItem> <asp:ListItem>Thiruvananthapuram</asp:ListItem> <asp:ListItem>Kochi</asp:ListItem> <asp:ListItem>Kozhikode</asp:ListItem> <asp:ListItem>Kollam</asp:ListItem> <asp:ListItem>Thrissur</asp:ListItem> <asp:ListItem>Kannur</asp:ListItem> <asp:ListItem>Alappuzha</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td> Departure Time </td> <td> <asp:TextBox ID="DepartureTime" runat="server"></asp:TextBox></td> </tr> <tr> <td> Distance in Km </td> <td> <asp:TextBox ID="DistanceinKm" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Estimated Amount </td> <td> <asp:TextBox ID="EstimatedAmountText" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Cab Type </td> <td> <asp:RadioButtonList ID="CabType1" runat="server" > <asp:ListItem>Indigo</asp:ListItem> <asp:ListItem>Innova </asp:ListItem> </asp:RadioButtonList></td> </tr> <tr> <td colspan="2"> <asp:Button ID="Button1Edit" runat="server" Text="Button" CssClass="button button-info" OnClick="Button1Edit_Click"/> </td> </tr> </table> </div>
File Name: ViewCabDetails.aspx.cs
RowCommand event from ViewCabDetails.aspx page
protected void GridView1Awesome_RowCommand(object sender, GridViewCommandEventArgs e) { int BookingReferenceNumberTemp = Convert.ToInt32(e.CommandArgument); if (e.CommandName == "DeleteEmployee") { bindControlsDelete(BookingReferenceNumberTemp); // this code is for delete } else if (e.CommandName == "EditEmployee") { bindControls(BookingReferenceNumberTemp); // this code for update } }
File Name: ViewCabDetails.aspx.cs
private void bindControls(int BookingReferenceNumberTemp) { BookCabDBOperation cabBookDB = new BookCabDBOperation(); cabDetails = cabBookDB.GetCabDetailsByBookingID(BookingReferenceNumberTemp); // the above line is a function calling, which is much more important bookRefNo.Text = Convert.ToString(cabDetails.BookingReferenceNumber1); PassengerName11.Text = cabDetails.PassengerName1; FromLocation.Text = cabDetails.FromLocation1; ToLocation.Text = cabDetails.ToLocation1 ; DepartureTime.Text = cabDetails.DepartureTime1; DistanceinKm.Text = cabDetails.DistanceinKm1; EstimatedAmountText.Text = cabDetails.EstimatedAmount1; CabType1.Text = cabDetails.CabType1; divEdit.Visible = true; }
File Name: BookCabDBOperation.cs
Database Operation
public BookCabClass GetCabDetailsByBookingID(int bookID) { SqlConnection con = new SqlConnection(conString); con.Open(); SqlCommand cmd = new SqlCommand("sp_select_byId", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@BookingReferenceNumber1", bookID); SqlDataReader reader = cmd.ExecuteReader(); BookCabClass BookCabClasObj= new BookCabClass(); while (reader.Read()) { BookCabClasObj.BookingReferenceNumber1 = Convert.ToInt32(reader["BookingReferenceNumber"]); BookCabClasObj.PassengerName1 = reader["PassengerName"].ToString(); BookCabClasObj.FromLocation1 = reader["FromLocation"].ToString(); BookCabClasObj.ToLocation1 = reader["ToLocation"].ToString(); BookCabClasObj.DepartureTime1 = reader["DepartureTime"].ToString(); BookCabClasObj.DistanceinKm1 = reader["DistanceinKm"].ToString(); BookCabClasObj.EstimatedAmount1 = reader["EstimatedAmount"].ToString(); BookCabClasObj.CabType1 = reader["CabType"].ToString(); } con.Close(); return BookCabClasObj; }
File Name: ViewCabDetails.aspx.cs
OnClick="Button1Edit_Click"
After Getting the values in the textbox from the database we will update
that value inside database again
protected void Button1Edit_Click(object sender, EventArgs e) { cabDetails.PassengerName1 = PassengerName11.Text; cabDetails.ToLocation1 = ToLocation.Text; cabDetails.DepartureTime1 = DepartureTime.Text; cabDetails.DistanceinKm1 = DistanceinKm.Text; cabDetails.EstimatedAmount1 = EstimatedAmountText.Text; cabDetails.CabType1 = CabType1.Text; cabDetails.BookingReferenceNumber1 = Convert.ToInt32(bookRefNo.Text); string message = cabBookDB.updateCabDetails(cabDetails); // THE ABOVE FUNCTION CALL UPDATES THE VALUE bindData(); Response.Write(String.Format("<script>alert('{0}')</script>", message)); bookRefNo.Text = ""; PassengerName11.Text = ""; FromLocation.Text = ""; ToLocation.Text = ""; DepartureTime.Text = ""; DistanceinKm.Text = ""; EstimatedAmountText.Text = ""; CabType1.SelectedValue ="" ; }
File Name: BookCabDBOperation.cs
public string updateCabDetails(BookCabClass bookCabClassObj) { SqlConnection con = new SqlConnection(conString); con.Open(); SqlCommand cmd = new SqlCommand("sp_edit_ByBookId", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@PassengerName", bookCabClassObj.PassengerName1); cmd.Parameters.AddWithValue("@FromLocation", bookCabClassObj.FromLocation1); cmd.Parameters.AddWithValue("@ToLocation", bookCabClassObj.ToLocation1); cmd.Parameters.AddWithValue("@DepartureTime", bookCabClassObj.DepartureTime1); cmd.Parameters.AddWithValue("@DistanceinKm", bookCabClassObj.DistanceinKm1); cmd.Parameters.AddWithValue("@EstimatedAmount", bookCabClassObj.EstimatedAmount1); cmd.Parameters.AddWithValue("@CabType", bookCabClassObj.CabType1); cmd.Parameters.AddWithValue("@BookingReferenceNumber", bookCabClassObj.BookingReferenceNumber1); int rowsAffected = cmd.ExecuteNonQuery(); string message; if (rowsAffected > 0) message = "Updated Successfully"; else message = "Some Error occured."; return message; }
DATABASE SQL PROCEDURE
Store Procedure Name: sp_select_byId
CREATE PROCEDURE sp_select_byId @BookingReferenceNumber1 INT AS BEGIN SELECT * FROM EtaxiSystem_1937935 WHERE BookingReferenceNumber = @BookingReferenceNumber1 END
Store Procedure Name: sp_edit_ByBookId
CREATE PROCEDURE sp_edit_ByBookId( @PassengerName VARCHAR(50), @FromLocation VARCHAR(50), @ToLocation VARCHAR(50), @DepartureTime DATETIME, @DistanceinKm INT, @EstimatedAmount INT, @CabType VARCHAR(50), @BookingReferenceNumber INT ) AS BEGIN UPDATE EtaxiSystem_1937935 SET PassengerName = @PassengerName, FromLocation = @FromLocation, ToLocation = @ToLocation, DepartureTime = @DepartureTime, DistanceinKm = @DistanceinKm, EstimatedAmount = @EstimatedAmount, CabType = @CabType WHERE BookingReferenceNumber = @BookingReferenceNumber END