Insert data using stored procedure in C# asp.net

Database:-
--Create Table with Fields

CREATE table  reg
(
Name varchar(50),
Phone varchar(50),
Email varchar(50),
DateOfBirth varchar(max),
Address varchar(max)
)
-----------------------------------------------------
--Create store procedurde with insert query

CREATE PROCEDURE SP_reg
(
@Name varchar(50),
@Phone varchar(50),

@Email varchar(50),
@DateOfBirth varchar(max),
@Address varchar(max)
)
AS
BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
SET NOCOUNT ON;
INSERT INTO reg (Name, Phone,Email,DateOfBirth, Address)
VALUES (@Name, @Phone,@Email, @DateOfBirth, @Address)

END

Source code :-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="registrationform.aspx.cs" Inherits="registrationform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
      <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #C0C0C0;
        }
        .style2
        {
            height: 31px;
        }
          .style3
          {
              width: 611px;
          }
          .style4
          {
              height: 31px;
              width: 611px;
          }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td colspan="2" align="center">
                    <asp:Label ID="lbreg" runat="server" Text="Registration Form"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="right" class="style3">
                    <asp:Label ID="lbname" runat="server" Text="Name"></asp:Label>
                </td>
                <td align="left">
                    <asp:TextBox ID="txtname" runat="server" Height="27px" Width="170px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right" class="style3">
                    <asp:Label ID="lbphoneno" runat="server" Text="Phone no"></asp:Label>
                </td>
                <td align="left">
                    <asp:TextBox ID="txtphoneno" runat="server" Height="27px" Width="170px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right" class="style3">
                    <asp:Label ID="lbemail" runat="server" Text="Email"></asp:Label>
                </td>
                <td align="left">
                    <asp:TextBox ID="txtemail" runat="server" Height="27px" Width="170px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right" class="style4">
                    <asp:Label ID="lbdob" runat="server" Text="Date of birth"></asp:Label>
                </td>
                <td align="left" class="style2">
                    <asp:TextBox ID="txtdob" runat="server" Height="27px" Width="170px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right" class="style3">
                    <asp:Label ID="lbaddress" runat="server" Text="Address"></asp:Label>
                </td>
                <td align="left">
                    <asp:TextBox ID="txtaddress" runat="server" Height="64px" TextMode="MultiLine"
                        Width="176px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right" class="style3">
                    <asp:Label ID="lboutput" runat="server"></asp:Label>
                </td>
                <td>
                    <asp:Button ID="btnsubmit" runat="server" Height="27px" Text="Submit"
                        Width="98px" onclick="btnsubmit_Click" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>


Design:-


C# Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class registrationform : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
       
        SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC\\Neeraj; Initial Catalog =registrationform ;User ID=sa; Password=12345678");
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "SP_reg"; // Store procediure name
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtname.Text.Trim();
        cmd.Parameters.Add("@Phone", SqlDbType.VarChar).Value = txtphoneno.Text.Trim();
        cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = txtemail.Text.Trim();
        cmd.Parameters.Add("@DateOfBirth", SqlDbType.VarChar).Value = txtdob.Text.Trim();
       
        cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = txtaddress.Text.Trim();
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            lboutput.Text = "Record inserted successfully";
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
Insert data using stored procedure in C# asp.net Insert data using stored procedure in C# asp.net Reviewed by NEERAJ SRIVASTAVA on 12:18:00 PM Rating: 5

2 comments:

Powered by Blogger.