insert data into table by asp.net c#
Insert data into table using stored procedure in asp.net ( c#)
At first we need any table like
Here is the sql database table creating code:
CREATE TABLE tblCustomerAlso we need a stored procedure to insert:
(
[id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Email] [nvarchar](200) NULL,
[Phone] [nvarchar](50) NULL
)
create proc [dbo].[sp_insert_tblCustomer]Then we need a web form to insert data which design will be look below:
(
@name nvarchar(50),
@last nvarchar(50),
@email nvarchar(50),
@phone nvarchar(50)
)
as
begin
insert into tblCustomer
(
FirstName,LastName,Email,Phone
)
values(@name ,@last ,@email ,@phone )
end
Here is the HTML code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="inserCustomer.aspx.cs" Inherits="inserCustomer" %>Here is the code behind:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<thead>
<h3>Insert Customer </h3>
</thead>
<tr>
<td>
First Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name
</td>
<td>
<asp:TextBox ID="txtLast" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> Email </td>
<td>
<asp:TextBox ID="txtEmail" runat="server" type="email"></asp:TextBox>
</td>
</tr>
<tr>
<td>Phone</td>
<td>
<asp:TextBox ID="txtPhone" runat="server" type="number"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" Width="90px" OnClick="Button1_OnClick"/>
</td>
</tr>
<tr>
<p><asp:Label runat="server" ID="lblmsg"></asp:Label></p>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;Finally here is the output look like
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class inserCustomer : System.Web.UI.Page
{
private readonly SqlConnection con =
new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=AjaxTestProject;Integrated Security=True;");
private string name, last, email, phone;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_OnClick(object sender, EventArgs e)
{
name = txtName.Text;
last = txtLast.Text;
email = txtEmail.Text;
phone = txtPhone.Text;
var cmd = new SqlCommand("sp_insert_tblCustomer", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@last", last);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@phone", phone);
con.Open();
int m = cmd.ExecuteNonQuery();
if (m != 0)
{
lblmsg.Text = "Record Inserted Succesfully";
lblmsg.ForeColor = Color.Green;
}
con.Close();
}
}
Hope it will be helpful.
No comments