C# code for inserting data into sql database using stored procedure


Stored Procedures in Asp.Net C# with example


The below example will help you how to call stored procedure in asp.net and insert data in database using store procedure.



 C# code for inserting data into sql database using stored procedure


STEP 1:Create table and Store Procdure in SQL server

Create table user_registration
(
 UserID int identity(1,1),
 Name varchar(50),
 UserAddress varchar(50),
 Gender varchar(6),
 UserPassword varchar(50)
)


Create procedure user_regprocedure

 @UName varchar(50),
 @UAddress varchar(50),
 @Gender varchar(6),
 @U_Password varchar(50)
  AS
  BEGIN
     INSERT INTO user_registration VALUES (@UName,@UAddress,@Gender,@U_Password)
  END

STEP 2:Add a asp.net webform.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="Registration" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="bordersolid 1px blackpadding45pxpositionrelativetop50px;" align="center">
                <tr>
                    <td>Name:
                    </td>
                    <td>
                        <asp:TextBox ID="txt_name" runat="server" Width="200px" Height="20px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Address :
                    </td>
                    <td>
                        <asp:TextBox ID="txt_address" runat="server" Width="200px" Height="20px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Gender:
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList1" runat="server" Width="205px" Height="25px">
                            <asp:ListItem Value="">Please Select</asp:ListItem>
                            <asp:ListItem>Male </asp:ListItem>
                            <asp:ListItem>Female</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td>Password:
                    </td>
                    <td>
                        <asp:TextBox ID="txt_pwd" runat="server" TextMode="Password" Width="200px" Height="20px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btnRegiser" runat="server" Text="Register" OnClick="btnRegiser_Click" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

STEP 3:Put this code in "Registration.aspx.cs" file



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 Registration : System.Web.UI.Page
    {
        SqlConnection con;
        SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnRegiser_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(@"initial catalog=project; data source=SANJAY\SQL2012; integrated security=true");
            con.Open();
            SqlCommand cmd = new SqlCommand("user_regprocedure", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UName", txt_name.Text);
            cmd.Parameters.AddWithValue("@UAddress", txt_address.Text);
            cmd.Parameters.AddWithValue("@Gender", DropDownList1.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@U_Password", txt_pwd.Text);
            cmd.ExecuteNonQuery();
            con.Close();
            lbl_msg.Text = "Record Inserted";
        }
    }




Previous
Next Post »