|
-
Sep 17th, 2008, 12:58 PM
#1
Thread Starter
Junior Member
[RESOLVED] [2008] Insertion error in datagrid
Hi,
I am having problems inserting data to my database. This is the error I get when i click on the insert button:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Here is my code: The error is generated in this line:
If checkStudentOptions(GridView1.DataKeys(i).Value.ToString, uid) = 0 Then
Any help would be great. Thanks
Code:
Private Function checkStudentOptions(ByVal optionID As Int32, ByVal studentID As Int32) As Int32
Dim dbconn As SqlConnection
Dim sql As String
Dim dbcomm As SqlCommand
Dim dbread As Int32
dbconn = New SqlConnection("Persist Security Info=True; User ID=sa; Password=password; Data Source=serverName; Initial Catalog=database")
dbconn.Open()
sql = "SELECT count(option_id) FROM pe_student_options WHERE option_id = " & optionID & " AND student_id=" & studentID
dbcomm = New SqlCommand(sql, dbconn)
dbread = dbcomm.ExecuteScalar
dbconn.Close()
Return dbread
End Function
Code:
Protected Sub InsertSelectedOptions_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles InsertSelectedOptions.Click
Dim i As Int16
Dim dbconn As SqlConnection
Dim sql As String
Dim dbcomm As SqlCommand
dbconn = New SqlConnection("Persist Security Info=True; User ID=sa; Password=password; Data Source=serverName; Initial Catalog=database")
dbconn.Open()
Dim uid As Int32
Dim blnAdd As Boolean = False
lblCheck.Text = "Can't add"
For i = 0 To GridView1.Rows.Count - 1
If CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked() Then
uid = getStudentID(Request.QueryString("student_id"))
If checkStudentOptions(GridView1.DataKeys(i).Value.ToString, uid) = 0 Then
sql = "INSERT INTO [pe_student_options] ([option_id],student_id) VALUES (" & GridView1.DataKeys(i).Value.ToString & "," & uid & ")"
dbcomm = New SqlCommand(sql, dbconn)
dbcomm.ExecuteNonQuery()
blnAdd = True
Else
If lblCheck.Text = "Can't add" Then
lblCheck.Text += " " & GridView1.Rows(i).Cells.Item(1).Text
Else
lblCheck.Text += ", " & GridView1.Rows(i).Cells.Item(1).Text
End If
lblCheck.ForeColor = Drawing.Color.Red
End If
End If
Next
If blnAdd = False Then
lblCheck.Text += " - already exists"
Else
lblCheck.Text = String.Empty
End If
'GridView2.DataBind()
dbconn.Close()
End Sub
-
Sep 17th, 2008, 01:03 PM
#2
Frenzied Member
Re: [2008] Insertion error in datagrid
Try:
checkStudentOptions(GridView1.DataKeys(i)(0), uid) = 0
-
Sep 17th, 2008, 01:09 PM
#3
Thread Starter
Junior Member
Re: [2008] Insertion error in datagrid
thanks for the reply but still get the same error.
-
Sep 17th, 2008, 02:14 PM
#4
Re: [2008] Insertion error in datagrid
OK, this is just a guess though as something strikes to my mind.
When you count Rows in a GridView or any other similar control, it also takes Header and Footer Rows into account. So DataKeys(i) when i=0 means Header rows which definitely doesn't have any DataKeys ID attached to it. Put a breakpoint on start of the loop, and check for what value of i it is throwing an error. Also check when i=0, the RowType (I mean the type of Row) in the QuickWatch window.
The reason for my doubt is, if I am right, then how can it get pass through the following line?
Code:
If CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked() Then
-
Sep 17th, 2008, 02:18 PM
#5
Re: [2008] Insertion error in datagrid
You're creating a loop based on the number of rows of the gridview, but then you attempt to access the datakeys collection. EIther base the loop on the datakeys count, and then use the datakeys, or base it on rows and then use the rows.
-
Sep 17th, 2008, 09:16 PM
#6
Thread Starter
Junior Member
Re: [2008] Insertion error in datagrid
thanks guys for looking into this. I got it working, turns out that I was missing my datakeynames.
-
Sep 17th, 2008, 09:18 PM
#7
Thread Starter
Junior Member
Re: [2008] Insertion error in datagrid
-
Sep 17th, 2008, 09:44 PM
#8
Re: [2008] Insertion error in datagrid
Hey,
If you want to mark your thread as resolved, go to the top of the thread, then within the Thread Tools Menu, select "Mark Thread Resolved".
If you are still unsure, see here.
Gary
-
Sep 17th, 2008, 09:56 PM
#9
Thread Starter
Junior Member
Re: [RESOLVED] [2008] Insertion error in datagrid
thanks, i was looking for that. much appreciated
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
|