Tweet
SIGN UP
MEMBER LOGIN:
TECHNOLOGIES
.NET 4.5
.NET Remoting in C#
Active Directory C#
ADO.NET in C#
AJAX in C#
Algorithms in C#
Android Programming
Articles C#
ASP, JavaScript, CSS
ASP.NET Controls in C#
ASP.NET MVC with C#
ASP.NET Programming
BizTalk Server
C# Assemblies
C# Language
C# Tutorials
C, C++, MFC
Career Advice
Chapters
Cloud Computing
COBOL.NET
Coding Best Practices
COM Interop
Compact Framework
Cryptography C#
Crystal Reports C#
Current Affairs
Custom Controls C#
Databases & DBA
Deployment
Design & Architecture
DirectX C#
Enterprise Development
Error Zone
Exception Handling C#
Expression Studio
F#
Files, Directories in C#
Financial Applications
Games Programming C#
GDI+ & Graphics
Hardware
How do I
HTML 5
Internet & Web
iPhone/iPad
Java
Java and .NET
JQuery
JSP
Leadership
Learn .NET
LINQ with C#
Metro Style Apps in C#
Mobile & Embedded
MonoDevelop
MSMQ in C#
Multithreading in C#
Networking
Office Development
OOP/OOD
Operating Systems
PHP
Printing in C#
Products
Project Management
Reports using C#
Robotics & Hardware
Security in .NET
SharePoint
Silverlight with C#
Smart Devices
Speech in C#
SQL
SQL Server 2012
String in C#
Team Foundation & VSS
Testing
Visual Basic .NET
Visual C#
Visual Studio .NET
Visual Studio 11
Visual Studio 2010
VS LightSwitch 2011
WCF with C#
Web Forms C#
Web Services in C#
WebForms Controls
Windows 8 in C#
Windows Controls C#
Windows Forms C#
Windows Phone in C#
Windows PowerShell
Windows Services in C#
Workflow Foundation in C#
WPF with C#
XAML with C#
XML in C#
XNA with C#
FORUMS
BLOGS
VIDEOS
INTERVIEWS
CERTIFICATIONS
DOWNLOADS
BOOKS
LINKS
NEWS
Learn .NET in 60 days – Part 1 (13 Labs)
Learn MVC (Model view controller) Step by Step ...
Learn C# Corner - Home
Using Border Radius and Gradients in CSS3: Part I
Learn C# Corner - Footer
Learn .NET and C# in 60 Days Lab13(Day 5): - C ...
iPhone 5 First Look
Samsung Galaxy Note Review
WCF - Authentication and Authorization in Ente ...
How to write a good article on C# Corner
Blog
Insert data in two tables in single click
Posted by
Satyapriya Nayak
in
Blogs
|
ASP.NET Controls in C#
on
Sep 13, 2011
In this article we will know how to insert records in two different tables simultaneously using single button click.
Tweet
1020
0
0
Download Files:
Insertdata.rar
Table structure
Create two stored procedure as below
ALTER PROCEDURE insert1
(@custid varchar(50),@custname varchar(50),@custaddress
varchar(50),@prodid varchar (50))
AS
insert Customer(custid,custname,custaddress,prodid) values
(@custid,@custname,@custaddress,@prodid)
ALTER PROCEDURE insert2
(@prodid varchar(50),@prodname varchar(50),@price int)
AS
insert Product(prodid,prodname,price) values
(@prodid,@prodname,@price)
Default.aspx code
<%
@
Page
Language
="C#"
AutoEventWireup
="true"
CodeFile
="Default.aspx.cs"
Inherits
="_Default"
%>
<!
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
>
Untitled Page
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server">
<
div
>
</
div
>
<
asp
:
Label
ID
="Label1"
runat
="server"
Text
="Customer Id"
Width
="120px"></
asp
:
Label
>
<
asp
:
TextBox
ID
="TextBox1"
runat
="server"></
asp
:
TextBox
><
br
/>
<
asp
:
Label
ID
="Label2"
runat
="server"
Text
="Customer Name"
Width
="120px"></
asp
:
Label
>
<
asp
:
TextBox
ID
="TextBox2"
runat
="server"></
asp
:
TextBox
><
br
/>
<
asp
:
Label
ID
="Label3"
runat
="server"
Text
="Customer Address"
Width
="120px"></
asp
:
Label
>
<
asp
:
TextBox
ID
="TextBox3"
runat
="server"></
asp
:
TextBox
><
br
/>
<
asp
:
Label
ID
="Label4"
runat
="server"
Text
="Product Id"
Width
="120px"></
asp
:
Label
>
<
asp
:
TextBox
ID
="TextBox4"
runat
="server"></
asp
:
TextBox
><
br
/>
<
asp
:
Label
ID
="Label5"
runat
="server"
Text
="Product Name"
Width
="120px"></
asp
:
Label
>
<
asp
:
TextBox
ID
="TextBox5"
runat
="server"></
asp
:
TextBox
><
br
/>
<
asp
:
Label
ID
="Label6"
runat
="server"
Text
="Price"
Width
="120px"></
asp
:
Label
>
<
asp
:
TextBox
ID
="TextBox6"
runat
="server"></
asp
:
TextBox
><
br
/>
<
asp
:
Button
ID
="btn_insert"
runat
="server"
Text
="Insert Data"
onclick
="btn_insert_Click"
/><
br
/>
<
asp
:
Label
ID
="Label7"
runat
="server"
Text
=""></
asp
:
Label
>
</
form
>
</
body
>
</
html
>
Default.aspx.cs code
using
System;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data.SqlClient;
public
partial
class
_Default
: System.Web.UI.
Page
{
string
strConnString =
ConfigurationManager
.ConnectionStrings[
"ConnectionString"
].ConnectionString;
SqlCommand
com;
protected
void
btn_insert_Click(
object
sender,
EventArgs
e)
{
SqlConnection
con =
new
SqlConnection
(strConnString);
com =
new
SqlCommand
(
"insert1"
, con);
com.CommandType =
CommandType
.StoredProcedure;
com.Parameters.Add(
"@custid"
,
SqlDbType
.VarChar).Value = TextBox1.Text;
com.Parameters.Add(
"@custname"
,
SqlDbType
.VarChar).Value = TextBox2.Text;
com.Parameters.Add(
"@custaddress"
,
SqlDbType
.VarChar).Value = TextBox3.Text;
com.Parameters.Add(
"@prodid"
,
SqlDbType
.VarChar).Value = TextBox4.Text;
con.Open();
com.ExecuteNonQuery();
con.Close();
com =
new
SqlCommand
(
"insert2"
, con);
com.CommandType =
CommandType
.StoredProcedure;
com.Parameters.AddWithValue(
"@prodid"
,
SqlDbType
.VarChar).Value = TextBox4.Text;
com.Parameters.Add(
"@prodname"
,
SqlDbType
.VarChar).Value = TextBox5.Text;
com.Parameters.Add(
"@price"
,
SqlDbType
.Int).Value = TextBox6.Text;
con.Open();
com.ExecuteNonQuery();
con.Close();
Label7.Text =
"Record Successfully Inserted"
;
TextBox1.Text=
""
;
TextBox2.Text=
""
;
TextBox3.Text=
""
;
TextBox4.Text=
""
;
TextBox5.Text=
""
;
TextBox6.Text=
""
;
}
}
Output
Thanks for reading
share this blog :
Dropdown Menu Code Snippet ASP.N..
Binding in an ASP.NET DropDownList
Related Blogs
Search and show GridView data in popup Window
Select gridview AutoGenerateSelectButton display data in label
AdRotator control in ASP.NET
Random display of records in gridview
PopUp window using JavaScript in asp.net
Invalid postback or callback argument in GridView ImageButton
post comment
Sponsored by
Become a Sponsor
More Blogs from this Blogger
Display records in a list view (web) from the database.
Display records in a listview (windows) from the database
Edit multiple records in a Grid view
TreeView in ASP.NET
Show success after inserting a value without using label (using java script)
Calendar control DayRender event
Insert data from Gridview to database
Change old password to new password in Asp.net
Export gridview records to word file
Insert Value from Checkbox in MySql Database in PHP
View All
Latest Blogs
WebPart Collector or WebPart Finder
Free Ride is Over for Desktop Developers in Visual Studio 11
DotNet developers most used application/tools launching through 'Run'
80-inch Windows 8 Tablet
Data encapsulation
Option to access the Column Names in Data table
Open multiple windows in browser startup. (Multiple homepages option)
I'm Sorry
const and readonly
Address, Binding and Contract in WCF
View All
Sponsored by
Become a Sponsor