Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Frds
i have problem that to add multiple record in database using store procedure. try many but only last record save in database. i want to save all the record which is in gridview.
here is code for savebutton :-

C#
if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentTable"];

                foreach (GridViewRow gr in GrdProduct.Rows)
                {
                    TextBox box1 = (TextBox)gr.FindControl("txtitemname");
                    TextBox box2 = (TextBox)gr.FindControl("txtqty");
                    TextBox box3 = (TextBox)gr.FindControl("txtprice");

                    string ProductName = box1.Text.ToString();
                    int Quanity = Convert.ToInt32(box2.Text);
                    double Price = Convert.ToDouble(box3.Text);

                    db.InsertUpdateOrderProduct(OrderNo, ProductName, Quanity, Price);
                }


here is storeprocedure :-
SQL
public void InsertUpdateOrderProduct(int OrderID, string ProductName, int Quantity, double Price)
    {
        Database db = new SqlDatabase(this.ConnectionString);
        using (DbCommand objcmd = db.GetStoredProcCommand("dbo.InsertUpdateOrderProduct"))
        {
            db.AddInParameter(objcmd, "@OrderID", DbType.Int32, OrderID);
            db.AddInParameter(objcmd, "@ProductName", DbType.String, ProductName);
            db.AddInParameter(objcmd, "@Quantity", DbType.Int32, Quantity);
            db.AddInParameter(objcmd, "@Price", DbType.Double, Price);
            db.ExecuteNonQuery(objcmd);
        }
    }

both are execute well but there is only last record save in database..
Where i am wrong in this?????
Please help me...!!!

Thank You All..
Posted
Updated 20-Aug-12 4:04am
v2
Comments
I.explore.code 20-Aug-12 10:05am    
is there any error?
Yatin chauhan 20-Aug-12 10:14am    
There is no erroer... i have insert 3 record in gridview all are found while debuge but only last insert in database. @Gladiatron
Zoltán Zörgő 20-Aug-12 10:11am    
And how the stored procedure looks like?
Yatin chauhan 20-Aug-12 10:15am    
here is my Store Procedure For insert:-
USE [DomesticRates]
GO
/****** Object: StoredProcedure [dbo].[InsertUpdateOrderProduct] Script Date: 08/20/2012 18:36:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[InsertUpdateOrderProduct]
@OrderID INT,
@ProductName VARCHAR(100),
@Quantity INT,
@Price FLOAT

AS
BEGIN

IF EXISTS (SELECT 1 From dbo.OrderProduct OP where @OrderID=OrderID)
BEGIN
UPDATE
dbo.OrderProduct
SET
OrderID=@OrderID,
ProductName=@ProductName,
Quantity=@Quantity,
Price=@Price
Where
OrderID=@OrderID
END
ELSE
BEGIN
INSERT INTO dbo.OrderProduct
(OrderID,ProductName,Quantity,Price)
SELECT
@OrderID,@ProductName,@Quantity,@Price
END
END
ZurdoDev 20-Aug-12 10:46am    
So, is @OrderID different every time?

Debug the values returned by db.ExecuteNonQuery method calls. You should get three times 1.
This is not the best implementation for an INSERT OR UPDATE replacement. Read the posts here: http://stackoverflow.com/questions/639854/check-if-a-row-exists-otherwise-insert[^]. You should use the MERGE statement[^] or apply proper locks.
And don't use SELECT 1, use SELECT * instead for EXISTS statement.
 
Share this answer
 
Comments
Zoltán Zörgő 21-Aug-12 9:11am    
I would be really interested why I got the downvote.
It seems, the problem is in sp.

Here is the right sp,
ALTER PROCEDURE [dbo].[InsertUpdateOrderProduct]
	@OrderID INT,
	@ProductName VARCHAR(100),
	@Quantity INT,
	@Price FLOAT
	
AS
	BEGIN
	
	IF EXISTS (SELECT orderid From dbo.OrderProduct where OrderID=@OrderID)
		BEGIN
			UPDATE 
				dbo.OrderProduct
			SET
				OrderID=@OrderID,
				ProductName=@ProductName,
				Quantity=@Quantity,
				Price=@Price
			Where
				OrderID=@OrderID						
		END
	ELSE
		BEGIN
			INSERT INTO dbo.OrderProduct
				(OrderID,ProductName,Quantity,Price)
			SELECT
				@OrderID,@ProductName,@Quantity,@Price
		END		
	END


I assume the code behind codes are executed well without error.

Best of luck.
cheers
 
Share this answer
 

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