So, I am trying to save data from a gridview control to an MSSQL database using a stored proc that should loop through the gridview and save all rows. It appears if there is only 1 row in the gridview, all is fine, but multiple rows generate the error. I am seeing 11 arguments in the stored proc and 11 arguments in the gridview save routine. Can anyone see what's up here?
VB code:
And here's the Stored proc:Code:Protected Sub Save2() Dim cmd As New SqlCommand Using conn As New SqlConnection("Data Source=server;Initial Catalog=database;User ID=user;Password=password") cmd.CommandText = "savetons" cmd.CommandType = CommandType.StoredProcedure cmd.Connection = conn conn.Open() For Each row As GridViewRow In GridView2.Rows For i As Integer = 0 To GridView2.Rows.Count - 1 cmd.Parameters.AddWithValue("@intcounty", GridView2.Rows(i).Cells(1).Text) cmd.Parameters.AddWithValue("@uftrs", GridView2.Rows(i).Cells(2).Text) cmd.Parameters.AddWithValue("@dblton", GridView2.Rows(i).Cells(4).Text) cmd.Parameters.AddWithValue("@intuse", GridView2.Rows(i).Cells(5).Text) cmd.Parameters.AddWithValue("@dbln", GridView2.Rows(i).Cells(6).Text) cmd.Parameters.AddWithValue("@dblp", GridView2.Rows(i).Cells(7).Text) cmd.Parameters.AddWithValue("@dblk", GridView2.Rows(i).Cells(8).Text) If GridView2.Rows(i).Cells(3).Text = "Bag" Then cmd.Parameters.AddWithValue("@inttontype", "1") ElseIf GridView2.Rows(i).Cells(3).Text = "Bulk" Then cmd.Parameters.AddWithValue("@inttontype", "2") ElseIf GridView2.Rows(i).Cells(3).Text = "Liq" Then cmd.Parameters.AddWithValue("@inttontype", "3") End If cmd.Parameters.AddWithValue("@lnglicno", licno.Text) cmd.Parameters.AddWithValue("@intqtr", DDLQtr.SelectedValue) cmd.Parameters.AddWithValue("@intyear", DDLYr.SelectedValue) cmd.ExecuteNonQuery() Next Next conn.Close() End Using If Chk1.Checked Then SavePO() Else End If End Sub
Code:ALTER PROCEDURE [dbo].[savetons] -- Add the parameters for the stored procedure here (@lnglicno as int, @intqtr as int, @intyear as int, @intcounty as nvarchar(50), @uftrs as smallint, @dblton as int, @inttontype as smallint, @intuse as smallint, @dbln as int, @dblp as int, @dblK as int) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here insert into temptonnage (lnglicno, intqtr, intyear, intcounty, uftrs, dblton, inttontype, intuse, dbln, dblp, dblk) Values (@lnglicno, @intqtr, @intyear, @intcounty, @uftrs, @dblton, @inttontype, @intuse, @dbln, @dblp, @dblk) END




Reply With Quote
