Sunday 2 August 2015

how to create stored procedure using asp.net for beginner.



for example: first of all we have to create table.

Table- StudentInfo

SID
int
Name
Varchar(100)
Class
Varchar(100)
FatherName
Varchar(100)
Address
Varchar(200)
ContactNo
bigint

In which table  SID(columns) is an identity .
syntax here..

create table StudentInfo
(
SID                 int identity(1,1),
Name              varchar(100),
Class               varchar(100),
FatherName    varchar(100),
Address           varchar(200),
ContactNo.      bigint
)

execute in sql...

now we learn how to insert data using stored procedure.

first of all we should be know what is stored procedure.  please read my previous article in which i have written about procedure.

Now we  have to create stored procedure. when you create stored procedure you can use these two words proc/procedure for creating the procedure

Create proc sp_studentinfo
@Name      varchar(100),
@Class       varchar(100),
@FatherName  varchar(100),
@Address         varchar(200),
@Contact          bigint
as
      begin
             
          insert into StudentInfo(Name,Class,FatherName,Address,Contact) values(@Name,@Class,@FatherName,@Address,@Contact)

end

here studentinfo.aspx.cs page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class student : System.Web.UI.Page
{
    SqlConnection con = new           SqlConnection(ConfigurationManager.AppSettings["ConString"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("sp_studentinfo", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Name",txtname.Text);
        cmd.Parameters.AddWithValue("@Class", txtclass.Text);
        cmd.Parameters.AddWithValue("@Address",txtaddress.Text);
        cmd.Parameters.AddWithValue("@FatherName",txtefname.Text);
        cmd.Parameters.AddWithValue("@Contact", txtmob.Text);
        con.Open();
        int i=cmd.ExecuteNonQuery();
        if (i == 1)
        {
            Label1.Text = "Data Successfully inserted...";
        }
        else
        {
            Label1.Text = "Try Again";
        }
        con.Close();

    }
}

here studentinfo.aspx



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="student.aspx.cs" Inherits="student" %>


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1
        {
            width: 100%;
        }
        .auto-style2
        {
            height: 23px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="auto-style1">
            <tr>
                <td>Name</td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">Class</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtclass" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style2"></td>
            </tr>
            <tr>
                <td class="auto-style2">Father Name</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style2"></td>
            </tr>

            <tr>

                <td class="auto-style2">Address</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style2"></td>
            </tr>
            <tr>
                <td class="auto-style2">Contact</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtmob" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style2"></td>
            </tr>
            <tr>
                <td class="auto-style2"></td>
                <td class="auto-style2"></td>
                <td class="auto-style2"></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>



No comments:

Post a Comment