SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to insert data from gridview to database?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

headshot
Starting Member

8 Posts

Posted - 05/14/2012 :  21:53:37  Show Profile  Reply with Quote
Hello everybody
I have two tables

CREATE TABLE [dbo].[Bill](
	[BillID] [int] IDENTITY(1,1) NOT NULL,
	[CusRecipient] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_Bill] PRIMARY KEY CLUSTERED 
(
	[BillID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[BillDetail](
	[BillID] [int] NOT NULL,
	[ProID] [int] NOT NULL,
	[Number] [int] NULL,
	[Price] [float] NULL,
 CONSTRAINT [PK_BillDetail] PRIMARY KEY CLUSTERED 
(
	[BillID] ASC,
	[ProID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[BillDetail]  WITH CHECK ADD  CONSTRAINT [FK_BillDetail_Bill] FOREIGN KEY([BillID])
REFERENCES [dbo].[Bill] ([BillID])
GO
ALTER TABLE [dbo].[BillDetail] CHECK CONSTRAINT [FK_BillDetail_Bill]


I want to insert data from Gridview to database , that table Bill and table DetailBill , but the GridView displays a lot of data. For example one of my GridView
ProID ProName ProNumber ProPrice ProToTal
23 Book 2 15 30
12 Candy 1 12 12
9 Food 3 10 30

I try this procedure following :

ALTER proc [dbo].[sp_InsertBill_BillDetail]
@cus_recipient as nvarchar(50),
@pro_id as int,@number as int,@price as float
as
	Begin
		insert into Bill(CusRecipient)values(@cus_recipient)
		insert into BillDetail(ProID,Number,Price)values(@pro_id,@number,@price)
	End

If @@rowcount =0
	select errcode =1 ,errmsg=N'Not data insert'
else
	select errcode=0,errmsg=N'Insert Success'


Can you try it.Thank you so much.

headshot
Starting Member

8 Posts

Posted - 05/15/2012 :  05:32:10  Show Profile  Reply with Quote
Can you try it
Share for me
Go to Top of Page

headshot
Starting Member

8 Posts

Posted - 05/16/2012 :  00:36:35  Show Profile  Reply with Quote
ha ha

Problem is soluted

The key of the problem, @@identity, it will hold a single encoding same BillID

Demo video of my code: http://www.youtube.com/watch?v=628Fb7Y-WS8

Thank for your watching.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000