?
Solved

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Posted on 2005-03-08
14
Medium Priority
?
917 Views
Last Modified: 2012-06-21
I am trying to enable a master detail form to be editable in asp.net. The edit, update and cancel commands I have are listed below:

Private Sub detailDataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles detailDataGrid.EditCommand
        detailDataGrid.EditItemIndex = e.Item.ItemIndex
        detailDataGrid.DataBind()

    End Sub

    Private Sub detailDataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles detailDataGrid.UpdateCommand

        Dim key As String = detailDataGrid.DataKeys(e.Item.ItemIndex).ToString()
        Dim polloption_text As String
        Dim tb As TextBox
        tb = CType(e.Item.Cells(2).Controls(0), TextBox)
        polloption_text = tb.Text
        Dim r As datasetpoll2.tblPollOptionRow
        r = objdatasetpoll2.tblPollOption.FindBypolloption_id(key)
        r.polloption_text = polloption_text
        OleDbDataAdapter2.Update(objdatasetpoll2)
        detailDataGrid.DataBind()
        detailDataGrid.EditItemIndex = -1
        detailDataGrid.DataBind()

    End Sub

    Private Sub detailDataGrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles detailDataGrid.CancelCommand
        detailDataGrid.EditItemIndex = -1
        detailDataGrid.DataBind()
    End Sub
End Class


However, I keep getting the following error which I can't seem to solve:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Source Error:


Line 265:    Private Sub detailDataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles detailDataGrid.UpdateCommand
Line 266:
Line 267:        Dim key As String = detailDataGrid.DataKeys(e.Item.ItemIndex).ToString()
Line 268:        'Dim pollName As String
Line 269:        Dim polloption_text As String
0
Comment
Question by:ladykh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
14 Comments
 
LVL 8

Expert Comment

by:trevorhartman
ID: 13491307
check for a negative ItemIndex:

Private Sub detailDataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles detailDataGrid.UpdateCommand
  If e.Item.ItemIndex > -1 Then
        Dim key As String = detailDataGrid.DataKeys(e.Item.ItemIndex).ToString()
        Dim polloption_text As String
        Dim tb As TextBox
        tb = CType(e.Item.Cells(2).Controls(0), TextBox)
        polloption_text = tb.Text
        Dim r As datasetpoll2.tblPollOptionRow
        r = objdatasetpoll2.tblPollOption.FindBypolloption_id(key)
        r.polloption_text = polloption_text
        OleDbDataAdapter2.Update(objdatasetpoll2)
        detailDataGrid.DataBind()
        detailDataGrid.EditItemIndex = -1
        detailDataGrid.DataBind()
  End If
End Sub
0
 

Author Comment

by:ladykh
ID: 13491450
The same error appears when I add this check.
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 13491461
and the error is on this line?

        Dim key As String = detailDataGrid.DataKeys(e.Item.ItemIndex).ToString()
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Accepted Solution

by:
trevorhartman earned 2000 total points
ID: 13491466
maybe try commenting out all your code and check this value:

Response.Write(e.Item.ItemIndex)
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 13491471
I guess the only other possability is e.Item.ItemIndex exceeding the upper index of the DataKeys collection... Did you set the DataKeyField property in your DataGrid?
0
 

Author Comment

by:ladykh
ID: 13491550
Yes, the error is on:
Dim key As String = detailDataGrid.DataKeys(e.Item.ItemIndex).ToString()

I commented out the code for the update command and if I edit the first row the number returned is 0. If I edit the second row, the number returned is 1 etc.

I didn't set the datakeyfield property in the datagrid.
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 13491564
that's your problem!  If you don't set a DataKeyField then where is your DataKeys collection coming from?
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 13491573
if you do:

Response.Write(detailDataGrid.DataKeys.Count)

it should output 0. which is why you can't access any members of the collection - there aren't any
0
 

Author Comment

by:ladykh
ID: 13491602
When I do this I get the following error:

Object reference not set to an instance of an object.
Line 276:        r.polloption_text = pollName
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 13491621
if you set your DataKeyField to the primaryKey of the table that populates your datagrid, everything will work better :)
0
 

Author Comment

by:ladykh
ID: 13491677
I have done this but unfortuanately am still getting:

Object reference not set to an instance of an object.
Line 276:        r.polloption_text = pollName

This is the backend code for the whole form:

Public Class DataWebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection
        Me.OleDbInsertCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbUpdateCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbDeleteCommand1 = New System.Data.OleDb.OleDbCommand
        Me.OleDbSelectCommand2 = New System.Data.OleDb.OleDbCommand
        Me.OleDbInsertCommand2 = New System.Data.OleDb.OleDbCommand
        Me.OleDbUpdateCommand2 = New System.Data.OleDb.OleDbCommand
        Me.OleDbDeleteCommand2 = New System.Data.OleDb.OleDbCommand
        Me.OleDbDataAdapter1 = New System.Data.OleDb.OleDbDataAdapter
        Me.OleDbDataAdapter2 = New System.Data.OleDb.OleDbDataAdapter
        Me.objdatasetpoll2 = New Polls.datasetpoll2
        CType(Me.objdatasetpoll2, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'OleDbSelectCommand1
        '
        Me.OleDbSelectCommand1.CommandText = "SELECT polloption_id, polloption_text, pollquestion_id FROM tblPollOption"
        Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
        '
        'OleDbConnection1
        '
        Me.OleDbConnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
        "ocking Mode=1;Jet OLEDB:Database Password=;Data Source=""C:\Databases\poll.mdb"";P" & _
        "assword=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=""" & _
        "Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended" & _
        " Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Cre" & _
        "ate System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB" & _
        ":Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=F" & _
        "alse"
        '
        'OleDbInsertCommand1
        '
        Me.OleDbInsertCommand1.CommandText = "INSERT INTO tblPollOption(polloption_text, pollquestion_id) VALUES (?, ?)"
        Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1
        Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("polloption_text", System.Data.OleDb.OleDbType.VarWChar, 50, "polloption_text"))
        Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("pollquestion_id", System.Data.OleDb.OleDbType.Integer, 0, "pollquestion_id"))
        '
        'OleDbUpdateCommand1
        '
        Me.OleDbUpdateCommand1.CommandText = "UPDATE tblPollOption SET polloption_text = ?, pollquestion_id = ? WHERE (pollopti" & _
        "on_id = ?) AND (polloption_text = ? OR ? IS NULL AND polloption_text IS NULL) AN" & _
        "D (pollquestion_id = ? OR ? IS NULL AND pollquestion_id IS NULL)"
        Me.OleDbUpdateCommand1.Connection = Me.OleDbConnection1
        Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("polloption_text", System.Data.OleDb.OleDbType.VarWChar, 50, "polloption_text"))
        Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("pollquestion_id", System.Data.OleDb.OleDbType.Integer, 0, "pollquestion_id"))
        Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_polloption_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "polloption_id", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_polloption_text", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "polloption_text", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_polloption_text1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "polloption_text", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_id", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_id1", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_id", System.Data.DataRowVersion.Original, Nothing))
        '
        'OleDbDeleteCommand1
        '
        Me.OleDbDeleteCommand1.CommandText = "DELETE FROM tblPollOption WHERE (polloption_id = ?) AND (polloption_text = ? OR ?" & _
        " IS NULL AND polloption_text IS NULL) AND (pollquestion_id = ? OR ? IS NULL AND " & _
        "pollquestion_id IS NULL)"
        Me.OleDbDeleteCommand1.Connection = Me.OleDbConnection1
        Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_polloption_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "polloption_id", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_polloption_text", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "polloption_text", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_polloption_text1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "polloption_text", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_id", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_id1", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_id", System.Data.DataRowVersion.Original, Nothing))
        '
        'OleDbSelectCommand2
        '
        Me.OleDbSelectCommand2.CommandText = "SELECT pollquestion_description, pollquestion_id, pollquestion_text FROM tblPollQ" & _
        "uestion"
        Me.OleDbSelectCommand2.Connection = Me.OleDbConnection1
        '
        'OleDbInsertCommand2
        '
        Me.OleDbInsertCommand2.CommandText = "INSERT INTO tblPollQuestion(pollquestion_description, pollquestion_text) VALUES (" & _
        "?, ?)"
        Me.OleDbInsertCommand2.Connection = Me.OleDbConnection1
        Me.OleDbInsertCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("pollquestion_description", System.Data.OleDb.OleDbType.VarWChar, 100, "pollquestion_description"))
        Me.OleDbInsertCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("pollquestion_text", System.Data.OleDb.OleDbType.VarWChar, 50, "pollquestion_text"))
        '
        'OleDbUpdateCommand2
        '
        Me.OleDbUpdateCommand2.CommandText = "UPDATE tblPollQuestion SET pollquestion_description = ?, pollquestion_text = ? WH" & _
        "ERE (pollquestion_id = ?) AND (pollquestion_description = ? OR ? IS NULL AND pol" & _
        "lquestion_description IS NULL) AND (pollquestion_text = ? OR ? IS NULL AND pollq" & _
        "uestion_text IS NULL)"
        Me.OleDbUpdateCommand2.Connection = Me.OleDbConnection1
        Me.OleDbUpdateCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("pollquestion_description", System.Data.OleDb.OleDbType.VarWChar, 100, "pollquestion_description"))
        Me.OleDbUpdateCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("pollquestion_text", System.Data.OleDb.OleDbType.VarWChar, 50, "pollquestion_text"))
        Me.OleDbUpdateCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_id", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbUpdateCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_description", System.Data.OleDb.OleDbType.VarWChar, 100, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_description", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbUpdateCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_description1", System.Data.OleDb.OleDbType.VarWChar, 100, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_description", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbUpdateCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_text", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_text", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbUpdateCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_text1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_text", System.Data.DataRowVersion.Original, Nothing))
        '
        'OleDbDeleteCommand2
        '
        Me.OleDbDeleteCommand2.CommandText = "DELETE FROM tblPollQuestion WHERE (pollquestion_id = ?) AND (pollquestion_descrip" & _
        "tion = ? OR ? IS NULL AND pollquestion_description IS NULL) AND (pollquestion_te" & _
        "xt = ? OR ? IS NULL AND pollquestion_text IS NULL)"
        Me.OleDbDeleteCommand2.Connection = Me.OleDbConnection1
        Me.OleDbDeleteCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_id", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbDeleteCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_description", System.Data.OleDb.OleDbType.VarWChar, 100, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_description", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbDeleteCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_description1", System.Data.OleDb.OleDbType.VarWChar, 100, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_description", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbDeleteCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_text", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_text", System.Data.DataRowVersion.Original, Nothing))
        Me.OleDbDeleteCommand2.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_pollquestion_text1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "pollquestion_text", System.Data.DataRowVersion.Original, Nothing))
        '
        'OleDbDataAdapter1
        '
        Me.OleDbDataAdapter1.DeleteCommand = Me.OleDbDeleteCommand1
        Me.OleDbDataAdapter1.InsertCommand = Me.OleDbInsertCommand1
        Me.OleDbDataAdapter1.SelectCommand = Me.OleDbSelectCommand1
        Me.OleDbDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "tblPollOption", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("polloption_id", "polloption_id"), New System.Data.Common.DataColumnMapping("polloption_text", "polloption_text"), New System.Data.Common.DataColumnMapping("pollquestion_id", "pollquestion_id")})})
        Me.OleDbDataAdapter1.UpdateCommand = Me.OleDbUpdateCommand1
        '
        'OleDbDataAdapter2
        '
        Me.OleDbDataAdapter2.DeleteCommand = Me.OleDbDeleteCommand2
        Me.OleDbDataAdapter2.InsertCommand = Me.OleDbInsertCommand2
        Me.OleDbDataAdapter2.SelectCommand = Me.OleDbSelectCommand2
        Me.OleDbDataAdapter2.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "tblPollQuestion", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("pollquestion_description", "pollquestion_description"), New System.Data.Common.DataColumnMapping("pollquestion_id", "pollquestion_id"), New System.Data.Common.DataColumnMapping("pollquestion_text", "pollquestion_text")})})
        Me.OleDbDataAdapter2.UpdateCommand = Me.OleDbUpdateCommand2
        '
        'objdatasetpoll2
        '
        Me.objdatasetpoll2.DataSetName = "datasetpoll2"
        Me.objdatasetpoll2.Locale = New System.Globalization.CultureInfo("en-GB")
        CType(Me.objdatasetpoll2, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub
    Protected WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
    Protected WithEvents OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand
    Protected WithEvents OleDbUpdateCommand1 As System.Data.OleDb.OleDbCommand
    Protected WithEvents OleDbDeleteCommand1 As System.Data.OleDb.OleDbCommand
    Protected WithEvents OleDbSelectCommand2 As System.Data.OleDb.OleDbCommand
    Protected WithEvents OleDbInsertCommand2 As System.Data.OleDb.OleDbCommand
    Protected WithEvents OleDbUpdateCommand2 As System.Data.OleDb.OleDbCommand
    Protected WithEvents OleDbDeleteCommand2 As System.Data.OleDb.OleDbCommand
    Protected WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
    Protected WithEvents OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter
    Protected WithEvents OleDbDataAdapter2 As System.Data.OleDb.OleDbDataAdapter
    Protected WithEvents objdatasetpoll2 As Polls.datasetpoll2
    Protected WithEvents buttonLoad As System.Web.UI.WebControls.Button
    Protected WithEvents masterDataGrid As System.Web.UI.WebControls.DataGrid
    Protected WithEvents detailDataGrid As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here


    End Sub

    Private Sub ShowDetailGrid()
        If (Me.masterDataGrid.SelectedIndex <> -1) Then
            Dim parentRows As System.Data.DataView
            Dim childRows As System.Data.DataView
            Dim currentParentRow As System.Data.DataRowView
            Dim sr As System.IO.StringReader = New System.IO.StringReader(CType(Me.ViewState("objdatasetpoll2"), String))
            Me.objdatasetpoll2.ReadXml(sr)
            parentRows = New DataView
            parentRows.Table = Me.objdatasetpoll2.Tables("tblPollQuestion")
            currentParentRow = parentRows(Me.masterDataGrid.SelectedIndex)
            childRows = currentParentRow.CreateChildView("relationPoll2")
            Me.detailDataGrid.DataSource = childRows
            Me.detailDataGrid.DataBind()
            Me.detailDataGrid.Visible = True
        Else
            Me.detailDataGrid.Visible = False
        End If

    End Sub
    Private Sub masterDataGrid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles masterDataGrid.SelectedIndexChanged
        Me.ShowDetailGrid()

    End Sub
    Private Sub buttonLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonLoad.Click
        Try
            Me.LoadDataSet()
            Me.masterDataGrid.SelectedIndex = -1
            Me.masterDataGrid.DataBind()
            Me.detailDataGrid.Visible = False
            Dim sw As System.IO.StringWriter = New System.IO.StringWriter
            Me.objdatasetpoll2.WriteXml(sw)
            Me.ViewState("objdatasetpoll2") = sw.ToString
        Catch eLoad As System.Exception
            Me.Response.Write(eLoad.Message)
        End Try

    End Sub
    Public Sub LoadDataSet()
        'Create a new dataset to hold the records returned from the call to FillDataSet.
        'A temporary dataset is used because filling the existing dataset would
        'require the databindings to be rebound.
        Dim objDataSetTemp As Polls.datasetpoll2
        objDataSetTemp = New Polls.datasetpoll2
        Try
            'Attempt to fill the temporary dataset.
            Me.FillDataSet(objDataSetTemp)
        Catch eFillDataSet As System.Exception
            'Add your error handling code here.
            Throw eFillDataSet
        End Try
        Try
            'Empty the old records from the dataset.
            objdatasetpoll2.Clear()
            'Merge the records into the main dataset.
            objdatasetpoll2.Merge(objDataSetTemp)
        Catch eLoadMerge As System.Exception
            'Add your error handling code here.
            Throw eLoadMerge
        End Try

    End Sub
    Public Sub FillDataSet(ByVal dataSet As Polls.datasetpoll2)
        'Turn off constraint checking before the dataset is filled.
        'This allows the adapters to fill the dataset without concern
        'for dependencies between the tables.
        dataSet.EnforceConstraints = False
        Try
            'Open the connection.
            Me.OleDbConnection1.Open()
            'Attempt to fill the dataset through the OleDbDataAdapter1.
            Me.OleDbDataAdapter1.Fill(dataSet)
            Me.OleDbDataAdapter2.Fill(dataSet)
        Catch fillException As System.Exception
            'Add your error handling code here.
            Throw fillException
        Finally
            'Turn constraint checking back on.
            dataSet.EnforceConstraints = True
            'Close the connection whether or not the exception was thrown.
            Me.OleDbConnection1.Close()
        End Try

    End Sub

    Private Sub detailDataGrid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles detailDataGrid.SelectedIndexChanged

    End Sub

    Private Sub detailDataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles detailDataGrid.EditCommand
        detailDataGrid.EditItemIndex = e.Item.ItemIndex
        detailDataGrid.DataBind()

    End Sub

   

    Private Sub detailDataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles detailDataGrid.UpdateCommand
        If e.Item.ItemIndex > -1 Then

            Dim key As String = detailDataGrid.DataKeys(e.Item.ItemIndex).ToString()
            Dim pollName As String
            Dim tb As TextBox
            tb = CType(e.Item.Cells(2).Controls(0), TextBox)
            pollName = tb.Text
            Dim r As datasetpoll2.tblPollOptionRow
            r = objdatasetpoll2.tblPollOption.FindBypolloption_id(key)
            r.polloption_text = pollName

            OleDbDataAdapter2.Update(objdatasetpoll2)
            detailDataGrid.DataBind()
            detailDataGrid.EditItemIndex = -1
            detailDataGrid.DataBind()

        End If
        Response.Write(e.Item.ItemIndex)
        Response.Write(detailDataGrid.DataKeys.Count)

   
    End Sub




    Private Sub detailDataGrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles detailDataGrid.CancelCommand
        detailDataGrid.EditItemIndex = -1
        detailDataGrid.DataBind()
    End Sub
End Class
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 13497660
i think this line is the culprit:

r = objdatasetpoll2.tblPollOption.FindBypolloption_id(key)

to test, see if r = nothing, right after you assign it

r = objdatasetpoll2.tblPollOption.FindBypolloption_id(key)
If r = Nothing Then Response.Write("r is nothing!")

i can't see this function:

objdatasetpoll2.tblPollOption.FindBypolloption_id(key)

so I wouldn't know what is going on with that..
0
 

Author Comment

by:ladykh
ID: 13512680
I can't get this to work either. The form won't complie with If r = Nothing Then Response.Write("r is nothing!").

The following code is what the form has generated automatically about objdatasetpoll2

'objdatasetpoll2
        '
        Me.objdatasetpoll2.DataSetName = "datasetpoll2"
        Me.objdatasetpoll2.Locale = New System.Globalization.CultureInfo("en-GB")
        CType(Me.objdatasetpoll2, System.ComponentModel.ISupportInitialize).EndInit()

I might try and start again!
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 13521064
did you get it working?  i'm kind of at a loss as far as helping out goes
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question