How to insert data into database through procedure using c#?

............................processor for insertion.............................

CREATE proc usp_ins1(@name varchar(50),@salary varchar(50),@address varchar(50),@error varchar(50) out)
as
begin
SET NOCOUNT ON;
if not exists(select*from registration where name=@name)
begin
insert into registration(name,salary,address) values(@name,@salary,@address)
 
set @error=@name+' Registered successfully '
end
 
else
begin
set @error=@name+' Already exists '
end
 
end

...........................end procesure..................................................

............................source code.......................

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

<!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: #FFCCFF;
        }
        .style2
        {
            width: 71px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Salary</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" ViewStateMode="Enabled"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" ViewStateMode="Inherit"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"  Text="Button" />
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
..............................end source code....................
..................end code behind...........................
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.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
public partial class myreg : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    DataSet ds = new DataSet();
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        con.Open();
        cmd = new SqlCommand("usp_ins1", con);
        cmd.CommandType = CommandType.StoredProcedure;
        
        cmd.Parameters.AddWithValue("@name",TextBox1.Text);
        cmd.Parameters.AddWithValue("@salary", TextBox2.Text);
        cmd.Parameters.AddWithValue("@address",TextBox3.Text);
        cmd.Parameters.Add("@ERROR", SqlDbType.Char, 50);
        cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
        cmd.ExecuteNonQuery();
        string message = (string)cmd.Parameters["@ERROR"].Value;
        Label1.Text = message.ToString();
        con.Close();
        Response.Write("Regristered successfully!");
    }
}
..........................end code behind.........................

Share:

No comments:

Post a Comment

Sunday, 16 December 2012

How to insert data into database through procedure using c#?

............................processor for insertion.............................

CREATE proc usp_ins1(@name varchar(50),@salary varchar(50),@address varchar(50),@error varchar(50) out)
as
begin
SET NOCOUNT ON;
if not exists(select*from registration where name=@name)
begin
insert into registration(name,salary,address) values(@name,@salary,@address)
 
set @error=@name+' Registered successfully '
end
 
else
begin
set @error=@name+' Already exists '
end
 
end

...........................end procesure..................................................

............................source code.......................

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

<!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: #FFCCFF;
        }
        .style2
        {
            width: 71px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Salary</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" ViewStateMode="Enabled"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" ViewStateMode="Inherit"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"  Text="Button" />
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
..............................end source code....................
..................end code behind...........................
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.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
public partial class myreg : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    DataSet ds = new DataSet();
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        con.Open();
        cmd = new SqlCommand("usp_ins1", con);
        cmd.CommandType = CommandType.StoredProcedure;
        
        cmd.Parameters.AddWithValue("@name",TextBox1.Text);
        cmd.Parameters.AddWithValue("@salary", TextBox2.Text);
        cmd.Parameters.AddWithValue("@address",TextBox3.Text);
        cmd.Parameters.Add("@ERROR", SqlDbType.Char, 50);
        cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
        cmd.ExecuteNonQuery();
        string message = (string)cmd.Parameters["@ERROR"].Value;
        Label1.Text = message.ToString();
        con.Close();
        Response.Write("Regristered successfully!");
    }
}
..........................end code behind.........................

No comments:

Post a Comment

Popular

Blog Archive

Total Pageviews

Archive