Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody, i have two table, saleorder,and itemorder, i can insert in both table from on click, but when i want to insert more then one record from gridview, it give me this error,(Procedure or function insertsale has too many arguments specified.) can any body help me please, below is my code

What I have tried:

SqlCommand cmd = new SqlCommand("insertsale");

cmd.Parameters.AddWithValue("@CustomerId", DropDownList3.SelectedValue);
//cmd.Parameters.AddWi
cmd.Parameters.AddWithValue("@SoDate", Convert.ToDateTime(TextBox13.Text));
cmd.Parameters.AddWithValue("@MadeBY", int.Parse(DropDownList4.SelectedValue));
cmd.Parameters.AddWithValue("@SubTotal", TextBox1.Text);
cmd.Connection = mycon;

cmd.CommandType = CommandType.StoredProcedure;

DataTable dt1 = (DataTable)Session["DataTable"];
for (int i = 0; i < dt1.Rows.Count; i++)
{

cmd.Parameters.AddWithValue("@ItemID", dt1.Rows[i]["ItemID"].ToString());
cmd.Parameters.AddWithValue("@Quantity", dt1.Rows[i]["Quantity"].ToString());
cmd.Parameters.AddWithValue("@Rate", dt1.Rows[i]["Rate"].ToString());
cmd.Parameters.AddWithValue("@Total", dt1.Rows[i]["total"].ToString());

}


mycon.Open();
cmd.ExecuteNonQuery();
mycon.Close();

and my storprocdure is
StoredProcedure [dbo].[insertsale] Script Date: 3/31/2016 9:00:27 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[insertsale]
(
@CustomerID int,
@SoDate datetime,
@SubTotal varchar(50),
@MadeBY varchar (50),
@ItemID int,
@Quantity varchar(50),
@Rate varchar(50),
@Total varchar(50)
)
as
begin

insert into SalesOrder(CustomerId,SoDate,SubTotal,MadeBY)
values (@CustomerID,@SoDate,@SubTotal,@MadeBY)

insert into OrderItems(SalesID,ItemID,Quantity,Rate,Total)
values (SCOPE_IDENTITY(),@ItemID,@Quantity,@Rate,@Total)

end
Posted
Updated 31-Mar-16 20:02pm
v2

Few corrections made to your code.

C#
DataTable dt1 = (DataTable)Session["DataTable"];
           for (int i = 0; i < dt1.Rows.Count; i++)
           {

               SqlCommand cmd = new SqlCommand("insertsale");
               cmd.Connection = mycon;
               cmd.CommandType = CommandType.StoredProcedure;

               cmd.Parameters.AddWithValue("@CustomerId", DropDownList3.SelectedValue);
               cmd.Parameters.AddWithValue("@SoDate", Convert.ToDateTime(TextBox13.Text));
               cmd.Parameters.AddWithValue("@MadeBY", int.Parse(DropDownList4.SelectedValue));
               cmd.Parameters.AddWithValue("@SubTotal", TextBox1.Text);
               cmd.Parameters.AddWithValue("@ItemID", dt1.Rows[i]["ItemID"].ToString());
               cmd.Parameters.AddWithValue("@Quantity", dt1.Rows[i]["Quantity"].ToString());
               cmd.Parameters.AddWithValue("@Rate", dt1.Rows[i]["Rate"].ToString());
               cmd.Parameters.AddWithValue("@Total", dt1.Rows[i]["total"].ToString());
               mycon.Open();
               cmd.ExecuteNonQuery();
               mycon.Close();

           }
 
Share this answer
 
v2
Comments
an0ther1 31-Mar-16 0:39am    
This would create multiple Sales records - one for each Order line
Karthik_Mahalingam 31-Mar-16 0:42am    
he is iterating the loop.
an0ther1 31-Mar-16 19:13pm    
Hi Katherik,
The stored procedure the OP has provided will create a new Sales Order record & a new Order Item record each time it is called, therefore if there is 5 order lines in the DataGrid you would end up with 5 SalesOrder records & 5 OrderItems records.
My solution to split the SP into one that creates a SalesOrder record & another for the OrderItems records will avoid duplicating the SalesOrder records.

Kind Regards
Karthik_Mahalingam 1-Apr-16 0:20am    
oh yes, then he has to modify the SP ( create two sp, one for salesorder and another for Itemorder.
btw its KARTHIK :)
Depending on the SQL Version you are using you can insert multiple records but you would need to change the way you are passing your values to the Stored Procedure.
To insert multiple values (requires SQL 2008 or later);
SQL
INSERT INTO MyTable
(FieldNameA, FieldNameB)
VALUES
('Row1Value1', 'Row1Value2'),('Row2Value1', 'Row2Value2')

To achieve this though you would need to pass strings to your Stored procedure and then split the values out - reasonably efficient as it requires a single round trip to the SQL Server.
Alternatively you would need to use 2 stored procedures. The first returns your new Order Id and the second inserts each Order Line as a single record and is called multiple times within your loop

Kind Regards
 
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