Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
THIS IS THE TABLE


C#
CREATE TABLE tblbilldetails(
   sno int IDENTITY(1,1)
   billno INT,
   itemcode INT,
   qty INT,
   price MONEY,
   total MONEY)


I HAVE CREATED STORED PROCEUDRE spaddtogrid


SQL
CREATE PROCEDURE [dbo].[spaddtogrid] 
@sno INT,	
@billno int,
	@itemcode int,
	@qty int,
	@price money,
	@total money
	
AS
BEGIN
IF @sno IS NULL
	-- SET NOCOUNT ON added to prevent extra result sets from
	
	-- interfering with SELECT statements.
	
	SET NOCOUNT ON;

   INSERT INTO  tblbilldetails(
   
   sno,   
   billno,
   itemcode,
   qty,
   price,
   total)
   VALUES(
   @sno,
   @billno,
   @itemcode,
   @qty,
   @price,
   @total)
   
   
END



WHAT CODE SHOULD I WRITE IN ORDER TO INSERT VALUES INTO THE TABLE IN ASP.NET
Posted
Updated 7-Oct-14 23:45pm
v3

I tried your query and found successful solution.
Only you have to remove @sno.

SQL
CREATE PROC insproc 
(@billno int,@itemcode int,@qty int,@price money,@total money)

AS
BEGIN

INSERT INTO tblbilldetails(billno,itemcode,qty,price,total)
VALUES(@billno,@itemcode,@qty,@price,@total)

END
 
Share this answer
 
Comments
sunnykvinod 8-Oct-14 8:25am    
your code doesn't work because when we use the stored procedure to insert in the table it returns error because column "sno" doesn't accept NULL as it is a identity column.
i am struck there,something has to be supplied from stored procedure.

try using STORED PROCEDURE you may get error
Change Your Code Like this ................

TO Create Table

CREATE TABLE tblbilldetails(
sno int IDENTITY(1,1),
billno INT,
itemcode INT,
qty INT,
price MONEY,
total MONEY)

SPC

SQL
  CREATE PROCEDURE [dbo].[spaddtogrid] 
	
@billno int,
	@itemcode int,
	@qty int,
	@price money,
	@total money
	
AS
BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from
	
	-- interfering with SELECT statements.
	
	SET NOCOUNT ON;
 
   INSERT INTO  tblbilldetails(
   
  
   billno,
   itemcode,
   qty,
   price,
   total)
   VALUES(  
   @billno,
   @itemcode,
   @qty,
   @price,
   @total)
 
Share this answer
 
Comments
sunnykvinod 8-Oct-14 8:24am    
your code doesn't work because when we use the stored procedure to insert in the table it returns error because column "sno" doesn't accept NULL as it is a identity column.
i am struck there,something has to be supplied from stored procedure.
[no name] 8-Oct-14 8:28am    
Hello dude u r not having problem in Stored Procedure you have porble in Table creation Do your table creation like this it will work fine

CREATE TABLE tblbilldetails(
sno int IDENTITY(1,1),
billno INT,
itemcode INT,
qty INT,
price MONEY,
total MONEY)
sunnykvinod 8-Oct-14 8:48am    
thanks dude. i'm done.
If you really wanted to specify the sno value for an insert, you can use IDENTITY_INSERT.
http://technet.microsoft.com/en-us/library/aa259221(v=sql.80).aspx[^]

Here is a working example:
SQL
--setup table and sp
create table tblbilldetails (sno int identity(1,1), billno int, itemcode int, qty int, price money, total money);
go
create procedure spaddtogrid
(@sno int, @billno int,@itemcode int,@qty int,@price money,@total money)
as
begin
	if @sno is not null begin
		SET IDENTITY_INSERT tblbilldetails ON;
			insert into tblbilldetails
			(sno,billno,itemcode,qty,price,total)
			values(@sno,@billno,@itemcode,@qty,@price,@total)
		SET IDENTITY_INSERT tblbilldetails OFF;
	end
	else if @sno is null begin
		insert into tblbilldetails
		values(@billno,@itemcode,@qty,@price,@total)
	end
end
go

--add without sno
exec spaddtogrid null, 1, 1, 1, 1, 1;
exec spaddtogrid null, 2, 1, 2, 1, 2;
exec spaddtogrid null, 3, 1, 3, 1, 3;

--add with sno
exec spaddtogrid 11, 4, 1, 1, 1, 1;
exec spaddtogrid 12, 5, 1, 2, 1, 2;
exec spaddtogrid 13, 6, 1, 3, 1, 3;

--add without sno
exec spaddtogrid null, 7, 1, 1, 1, 1;
exec spaddtogrid null, 8, 1, 2, 1, 2;
exec spaddtogrid null, 9, 1, 3, 1, 3;

--note: using this technique can produce duplicate records
--if you run the following, it will get inserted
exec spaddtogrid 2, 2, 1, 2, 1, 2;

--get records
select * from tblbilldetails;

--remove table and sp
drop table tblbilldetails;
drop procedure spaddtogrid;
go

Ordinarily I would not create a stored procedure to perform an insert in this manner.
But rather create an insert and update to the table like so:
SQL
create procedure SaveToGrid
(@sno int, @billno int,@itemcode int,@qty int,@price money,@total money)
as
begin
    if @sno is not null begin
        --Update Statement Here
    end
    else if @sno is null begin
        --Insert Statement Here
    end
end
go
 
Share this answer
 
v2
drop the table you had created. and fire these queries
SQL
CREATE TABLE tblbilldetails(
   sno int IDENTITY(1,1)
   billno INT,
   itemcode INT,
   qty INT,
   price MONEY,
   total MONEY);

CREATE PROC insproc 
(@billno int,@itemcode int,@qty int,@price money,@total money)
 
AS
BEGIN
 
INSERT INTO tblbilldetails(billno,itemcode,qty,price,total)
VALUES(@billno,@itemcode,@qty,@price,@total)
 
END
Now call the procedure with right number of parameters.
 
Share this answer
 
Remove @sno from query because it's identity column,

Run this query

CREATE PROCEDURE [dbo].[spaddtogrid]
@billno int,
@itemcode int,
@qty int,
@price money,
@total money

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

INSERT INTO tblbilldetails(

billno,
itemcode,
qty,
price,
total)
VALUES(
@billno,
@itemcode,
@qty,
@price,
@total)


END
 
Share this answer
 
v2
Comments
[no name] 8-Oct-14 5:55am    
This solution is helpful..
sunnykvinod 8-Oct-14 6:01am    
NOPE
i cant insert because identity column doesnt allow nulls
here is the error

Server Error in '/' Application.

Cannot insert the value NULL into column 'sno', table 'billdb1.dbo.tblbilldetails'; column does not allow nulls. INSERT fails.
The statement has been terminated.
dipak _kansara 8-Oct-14 6:15am    
your table script is wrong you forgot to put semicolon after
"sno int IDENTITY(1,1)" run below script

CREATE TABLE tblbilldetails(
sno int IDENTITY(1,1) PRIMARY KEY,
billno INT,
itemcode INT,
qty INT,
price MONEY,
total MONEY)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900