insert into datbase using stored procedure and .net code by standard form control | YourSite

insert into datbase using stored procedure and .net code by standard form control

MS Dot NET 938 views

In this blog we are going to show that how you will store the data inside a data base from a simple form using the store procedure

Database coding

<>SQL SERVER CREATE PROCEDURE WITH OUTPUT PARAMETER

Use the below code to use a particular database


// USE name_of_the_database

USE DB02TEST01  

Create a table to insert data using the precedure


 CREATE TABLE EtaxiSystem_1937935(
 BookingReferenceNumber INT PRIMARY KEY IDENTITY(100000,1),
 PassengerName VARCHAR(50),
 FromLocation VARCHAR(50),
 ToLocation VARCHAR(50),
 DepartureTime DATETIME,
 DistanceinKm INT,
 EstimatedAmount INT,
 CabType VARCHAR(50)
 )  

Use the below code to see the table data It is present


 select * from EtaxiSystem_1937935

Execute the below code to create a procedure in sql server

Below is the code for stored procedure



CREATE PROCEDURE ETaxiProcedure
 ( 
 @PassengerName VARCHAR(50),
 @FromLocation VARCHAR(50),
 @ToLocation VARCHAR(50),
 @DepartureTime DATETIME,
 @DistanceinKm INT,
 @EstimatedAmount INT,
 @CabType VARCHAR(50),
 @BookingReferenceNumber INT OUT
 )
 AS
 BEGIN

 INSERT INTO EtaxiSystem_1937935 VALUES 
 (
 @PassengerName,
 @FromLocation,
 @ToLocation,
 @DepartureTime,
 @DistanceinKm,
 @EstimatedAmount,
 @CabType)
 SET @BookingReferenceNumber = @@IDENTITY
 END

Execute the code to see your procedure is working or not

You can use the below code for checking your stored procedure


DECLARE @BookingReferenceNumber1 INT
EXEC ETaxiProcedure 'Rumman', 'Kolkata', 'Kerala', '12.02.1996', 1, 10, 'indigo', @BookingReferenceNumber1 OUT
PRINT @BookingReferenceNumber1 

Now see the table details using the below code again


SELECT * FROM EtaxiSystem_1937935

.NET Coding

File Name: TravelDetailsInsert.aspx



 <form runat="server">

<div class="container">

    <div class="row">
  <div class="col-sm-3"> </div>
  <div class="col-sm-6" style="background-color:lavenderblush;">



    <table class="table table-hover">
        <tr>
            <td>
                <asp:Label ID="Label1" runat="server" Text="Passenger Name"></asp:Label> </td>
            <td> <asp:TextBox ID="PassengerName" runat="server"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="* Please Enter Characters and space only" ControlToValidate="PassengerName" ValidationExpression="[a-zA-Z ]*$"></asp:RegularExpressionValidator>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="PassengerName" ErrorMessage="Enter passenger name"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="From Location"></asp:Label> </td>
            <td>
                <asp:DropDownList ID="FromLocation" runat="server" AutoPostBack="true">
                    <asp:ListItem>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> 
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Select the Base location" ControlToValidate="FromLocation"></asp:RequiredFieldValidator></td>
            
        </tr>
         <tr>
            <td> <asp:Label ID="Label3" runat="server" Text="To Location"></asp:Label> </td>
            <td> <asp:DropDownList ID="ToLocation" runat="server"  AutoPostBack="true"  OnSelectedIndexChanged="ToLocation_SelectedIndexChanged1">
                <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>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Select Your Destination Location" ControlToValidate="ToLocation"></asp:RequiredFieldValidator>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="TO and FROM is same" ControlToValidate="ToLocation"></asp:CustomValidator>
                <asp:Label ID="Label6" runat="server" Text="Label" ForeColor="#FF3399"></asp:Label>
            </td>
        </tr>
         <tr>
            <td>  <asp:Label ID="Label4" runat="server" Text="DepartureTime"></asp:Label> </td>
            <td> <asp:TextBox ID="DepartureTime" runat="server" Type="date"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Select a Date" ControlToValidate="DepartureTime"></asp:RequiredFieldValidator>
            </td>
        </tr>
         <tr>
            <td> <asp:Label ID="Label5" runat="server" Text="Distance in Km "></asp:Label>  </td>
            <td> <asp:TextBox ID="DistanceinKm" runat="server" onTextChanged="DistanceinKm_TextChanged" AutoPostBack="true"  ></asp:TextBox>
                <%-- <asp:Button ID="Button2" runat="server" Text="See Car Rent" OnClick="TextBox2_TextChanged"  />--%>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Select the distance" ControlToValidate="DistanceinKm"></asp:RequiredFieldValidator>
            </td>
        </tr>
         <tr>
            <td> <asp:Label ID="EstimatedAmountText" runat="server" Text="EstimatedAmount"></asp:Label> </td>
            <td>  <asp:TextBox ID="EstimatedAmount" runat="server"></asp:TextBox> </td>
        </tr>
         <tr>
            <td> <asp:Label ID="Label7" runat="server" Text="CabType"></asp:Label> </td>
            <td> <asp:RadioButtonList ID="CabType" runat="server">
                <asp:ListItem>Indigo</asp:ListItem>
                <asp:ListItem>Innova </asp:ListItem>
                </asp:RadioButtonList></td>
        </tr>
        <tr>
         <td colspan="1"> </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Button" CssClass="button button-info" OnClick="Button1_Click" /> </td>
        </tr>

        </table>
    </form>
     </div>
      <div class="col-sm-3"> 



      </div> 
  </div>



TravelDetailsInsert.aspx.cs

Button event


        protected void Button1_Click(object sender, EventArgs e)
        {

            // int BookingReferenceNumber = 100000;
            string PassengerName1 = PassengerName.Text;
            string FromLocation1 = FromLocation.Text;
            string ToLocation1 = ToLocation.Text;
            string DepartureTime1 = DepartureTime.Text;
            string DistanceinKm1 = DistanceinKm.Text;
            string EstimatedAmount1 = EstimatedAmount.Text;
            string CabType1 = CabType.Text;


            BookCabClass carBookDetailsobj = new BookCabClass(PassengerName1, FromLocation1, ToLocation1, DepartureTime1, DistanceinKm1, EstimatedAmount1, CabType1);
            BookCabDBOperation dbObj = new BookCabDBOperation();
            int returnValue = dbObj.InsertBooksCarData(carBookDetailsobj);
            string message = "Data Saved Successfully with ID:" + returnValue;
            if (Convert.ToInt32(returnValue) > 0)
            {
                Response.Write(String.Format("<script>alert('{0}') </script>",message));
            }
        }


File name: BookCabClass.cs


    public class BookCabClass
    {

        int BookingReferenceNumber;
        string PassengerName;
        string FromLocation;
        string ToLocation;
        string DepartureTime;
        string DistanceinKm;
        string EstimatedAmount;
        string CabType;

        public BookCabClass(string passengerName, string fromLocation, string toLocation, string departureTime, string distanceinKm, string estimatedAmount, string cabType)
        {
          // BookingReferenceNumber1 = BookingReferenceNumber;
            PassengerName = passengerName;
            FromLocation = fromLocation;
            ToLocation = toLocation;
            DepartureTime = departureTime;
            DistanceinKm = distanceinKm;
            EstimatedAmount = estimatedAmount;
            CabType = cabType;
        }

        public int BookingReferenceNumber1 { get => BookingReferenceNumber; set => BookingReferenceNumber = value; }
        public string PassengerName1 { get => PassengerName; set => PassengerName = value; }
        public string FromLocation1 { get => FromLocation; set => FromLocation = value; }
        public string ToLocation1 { get => ToLocation; set => ToLocation = value; }
        public string DepartureTime1 { get => DepartureTime; set => DepartureTime = value; }
        public string DistanceinKm1 { get => DistanceinKm; set => DistanceinKm = value; }
        public string EstimatedAmount1 { get => EstimatedAmount; set => EstimatedAmount = value; }
        public string CabType1 { get => CabType; set => CabType = value; }
    }


File name: BookCabDBOperation.cs

To work with date base we created a new class



public class BookCabDBOperation
    {
        string conString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        public int InsertBooksCarData(BookCabClass carBookObj)
        {
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand("ETaxiProcedure", con); 
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PassengerName", carBookObj.PassengerName1);
            cmd.Parameters.AddWithValue("@FromLocation", carBookObj.FromLocation1);
            cmd.Parameters.AddWithValue("@ToLocation", carBookObj.ToLocation1);
            cmd.Parameters.AddWithValue("@DepartureTime", carBookObj.DepartureTime1);
            cmd.Parameters.AddWithValue("@DistanceinKm", carBookObj.DistanceinKm1);
            cmd.Parameters.AddWithValue("@EstimatedAmount", carBookObj.EstimatedAmount1);
            cmd.Parameters.AddWithValue("@CabType", carBookObj.CabType1);
            cmd.Parameters.Add("@BookingReferenceNumber",SqlDbType.Int);
            cmd.Parameters["@BookingReferenceNumber"].Direction = ParameterDirection.Output;
            
            int rowsAffected = cmd.ExecuteNonQuery();
            if(rowsAffected >0)
            {
                carBookObj.BookingReferenceNumber1 = Convert.ToInt32(cmd.Parameters["@BookingReferenceNumber"].Value);
            }

            return carBookObj.BookingReferenceNumber1; 

        }

🚀 More Blogs You Might Like

Explore more articles and keep learning

What is Bounce Rate in SEO? Complete Guide for Beginners
search-engine-optimization
What is Bounce Rate in SEO? Complete Guide for Beginners

Learn what bounce rate is in SEO, how it is calculated, why it matters, common causes of high bounce rates, an...

👁 28 2026-05-24
Read More →
Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 43 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 38 2026-05-19
Read More →
How to Grow Your Business Mindset Step by Step
skill
How to Grow Your Business Mindset Step by Step

Learn how to develop and grow a successful business mindset step by step. Discover entrepreneurial thinking, p...

👁 56 2026-05-09
Read More →