Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to insert data from gridview to database?

Author  Topic 

headshot
Starting Member

9 Posts

Posted - 2012-05-14 : 21:53:37
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

9 Posts

Posted - 2012-05-15 : 05:32:10
Can you try it
Share for me
Go to Top of Page

headshot
Starting Member

9 Posts

Posted - 2012-05-16 : 00:36:35
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
   

- Advertisement -