|
-
Sep 14th, 2016, 09:34 AM
#1
Thread Starter
Member
Noobie question
I fully admit to being a novice, and I know a lot of what I am asking is basic, but I am under a time crunch and could use some guidance. I have a web application that I am writing that collects information for insertion into a database table. A part of this is having the user fill out some information that is placed into a gridview control, and could be multiple rows in that gridview. I have no problem saving the data IF there is only one row in the gridview, but when I loop through the rows to save multiple row values, the SQL stored proc seems not to handle the rows beyond row 1. It returns the stored proc error of too many arguments. I feel certain this is due to one of 2 things: (1) the stored proc is looking for various values, and from the gridview it is collecting the row values, then executing the stored proc, and when multiple rows are populated, there are more values than the stored proc has or (2) being a novice, I am approaching this wrong from the get-go.
My question is how to save these multiple row values to the database. The code to save follows:
Code:
Protected Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
Dim conn As New SqlConnection("Data Source=server;Initial Catalog=database;User ID=user;Password=password")
Dim cmd As New SqlCommand
Dim County As String
Dim UFTRS As String
Dim Use As String
Dim N As Int32
Dim P As Int32
Dim K As Int32
Dim ToCo As String
Dim uftrs1 As String
Dim tons As Int32
cmd.CommandText = "tempsave"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
Try
conn.Open()
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@lnglicno", licno.SelectedValue)
cmd.Parameters.AddWithValue("@dtrcvd", Date.Today)
cmd.Parameters.AddWithValue("@intqtr", DDLQtr.SelectedValue)
cmd.Parameters.AddWithValue("@intyear", DDLYr.SelectedValue)
cmd.Parameters.AddWithValue("@amount", Txtpayment.Text)
cmd.Parameters.AddWithValue("@tonfees", (totTon.Text * 0.25))
cmd.Parameters.AddWithValue("@penalty", ((totTon.Text * 0.25) * 0.1))
For Each row As GridViewRow In GridView2.Rows
For i As Integer = 0 To GridView2.Rows.Count - 1
County = GridView2.Rows(i).Cells(1).Text
UFTRS = GridView2.Rows(i).Cells(2).Text
tons = GridView2.Rows(i).Cells(4).Text
Use = GridView2.Rows(i).Cells(5).Text
N = GridView2.Rows(i).Cells(6).Text
P = GridView2.Rows(i).Cells(7).Text
K = GridView2.Rows(i).Cells(8).Text
cmd.Parameters.AddWithValue("@intcounty", County)
cmd.Parameters.AddWithValue("@dblton", totTon.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("@intuse", Use)
cmd.Parameters.AddWithValue("@dtproc", Date.Today)
cmd.Parameters.AddWithValue("@uftrs", UFTRS)
cmd.Parameters.AddWithValue("@dblN", N)
cmd.Parameters.AddWithValue("@dblP", P)
cmd.Parameters.AddWithValue("@dblK", K)
cmd.Parameters.AddWithValue("@dtDateTime", Date.Today)
Next
If Chk1.Checked Then
For Each row1 As GridViewRow In GridView1.Rows
For x As Integer = 0 To GridView1.Rows.Count - 1
ToCo = GridView1.Rows(x).Cells(1).Text
uftrs1 = GridView1.Rows(x).Cells(3).Text
cmd.Parameters.AddWithValue("@toco", ToCo)
cmd.Parameters.AddWithValue("@dbltontotal", potons.Text)
If GridView1.Rows(x).Cells(4).Text = "Bag" Then
cmd.Parameters.AddWithValue("@inttontype1", "1")
ElseIf GridView1.Rows(x).Cells(5).Text = "Bulk" Then
cmd.Parameters.AddWithValue("@inttontype1", "2")
ElseIf GridView1.Rows(x).Cells(6).Text = "Liquid" Then
cmd.Parameters.AddWithValue("@inttontype1", "3")
End If
cmd.Parameters.AddWithValue("@intcode1", uftrs1)
Next
Next
End If
Next
cmd.ExecuteNonQuery()
Finally
conn.Close()
End Try
End Sub
The stored proc is a simple insert statement using the variables indicated in the code above.
Any help available?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|