Results 1 to 9 of 9

Thread: [RESOLVED] [2008] Insertion error in datagrid

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    20

    Resolved [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

  2. #2
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2008] Insertion error in datagrid

    Try:

    checkStudentOptions(GridView1.DataKeys(i)(0), uid) = 0

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    20

    Re: [2008] Insertion error in datagrid

    thanks for the reply but still get the same error.

  4. #4
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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
    Show Appreciation. Rate Posts.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    20

    Re: [2008] Insertion error in datagrid

    thanks guys for looking into this. I got it working, turns out that I was missing my datakeynames.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    20

    Resolved Re: [2008] Insertion error in datagrid

    closed post

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    20

    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
  •  



Click Here to Expand Forum to Full Width