Hello, all!

I'm working with a project to search a Microsoft Access database, show the results in a datagrid, create text boxes to edit the entries found, and then update them.

So far, the search works great, and I have a typical datagrid with an "edit" button on the end, when you click on edit to switch the datagrid into edit mode, the options to update or cancel appear as I have intended, but empty textboxes replace the old data there. I would like the original data present until the user changes something and clicks "update."

I have been trying to get a feel for how to reference these text boxes and fill them, but so far I only get errors. Here is my most recent attempt and the given error:

Public Sub softwareGrid_EditCommand(ByVal source As Object, _
        ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
        Handles softwareGrid.EditCommand
        softwareGrid.EditItemIndex = e.Item.ItemIndex

        Dim test As String
        test = softwareGrid.EditItemIndex
        Debug.WriteLine(test)
        Me.BindDataGrid()
        'Dim txtSoftNum As String
        'txtSoftNum = CType(e.Item.Cells(0).Controls(0), TextBox).Text
        'Debug.WriteLine("this is the content of the software number text box:")
        'Debug.WriteLine(txtSoftNum)
    End Sub

    Public Sub softwareGrid_CancelCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        softwareGrid.EditItemIndex = -1
        BindDataGrid()
    End Sub

    Private Sub BindDataGrid()
        softwareGrid.DataSource = softwareDS
        softwareGrid.DataMember = "SOFTWARE DATABASE"
        softwareGrid.DataKeyField = "Software #"
    End Sub

    Public Sub softwareGrid_UpdateCommand(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
        Dim tbSoftwareNum As TextBox = E.Item.Cells(0).Controls(0)
        Dim tbSoftwareName As TextBox = E.Item.Cells(1).Controls(0)
        Dim tbVersion As TextBox = E.Item.Cells(2).Controls(0)
        Dim tbLocation As TextBox = E.Item.Cells(3).Controls(0)
        Dim tbSoftBrand As TextBox = E.Item.Cells(4).Controls(0)
        Dim tbDatePurchased As TextBox = E.Item.Cells(5).Controls(0)
        Dim tbFirstName As TextBox = E.Item.Cells(6).Controls(0)
        Dim tbLastName As TextBox = E.Item.Cells(7).Controls(0)
        Dim tbSerialNumber As TextBox = E.Item.Cells(8).Controls(0)
        Dim tbModel As TextBox = E.Item.Cells(9).Controls(0)
        Dim txtSoftwareNum As String
        txtSoftwareNum = CType(E.Item.Cells(0).Controls(0), TextBox).Text
        Debug.WriteLine("Soft Num")
        Debug.WriteLine(txtSoftwareNum)
        Dim txtModel As String
        txtModel = CType(E.Item.Cells(9).Controls(0), TextBox).Text
        Debug.WriteLine("Model")
        Debug.WriteLine(txtModel)
    End Sub

Feel free to comment on the update method as well, but the edit one is what's giving me the trouble right now. If I uncomment the second set of debug lines in the edit method, I get the following error:

Error message:

Specified argument was out of the range of valid values. 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: Specified argument was out of the range of valid values. Parameter name: index

Source Error:


Line 262: Me.BindDataGrid()
Line 263: Dim txtSoftNum As String
Line 264: txtSoftNum = CType(e.Item.Cells(0).Controls(0), TextBox).Text
Line 265: Debug.WriteLine("test")
Line 266: Debug.WriteLine(txtSoftNum)


Source File: c:\inetpub\wwwroot\ASPproject\WebApplication2\WebForm1.aspx.vb Line: 264

Stack Trace:


[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index]
System.Web.UI.ControlCollection.get_Item(Int32 index)
WebApplication2.WebForm1.softwareGrid_EditCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\ASPproject\WebApplication2\WebForm1.aspx.vb:264
System.Web.UI.WebControls.DataGrid.OnEditCommand(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


So yeah, the debugger shoots out a '0' for the editItemIndex, which I believe should be correct, and then errors out on the next debug request. The idea there was to see if the text box really had anything in it.

So thats it, I need to know how to fill those text boxes, and how to reference them properly with index values. Of course, any and all help would be greatly a-pree-key-8-ed. Thanks! :D

Recommended Answers

All 8 Replies

One more wierd thing: when you click on edit, nothing happens, then if you click it again, the empty text boxes appear. Hope that helps, maybe. :-|

Buddy,
I tried following your code but could not come into any conclusion.There is insufficient information,your client side datagrid isnt posted.

I have done something like this in the past.

This article does EXACTLY WHAT YOUR TRYING TO DO
Check it out.

http://aspnet.4guysfromrolla.com/articles/071002-1.2.aspx

HOPE IT HELPS

Thank you for your time, and yes, the resource you gave me has been a great help. My apologies for not including enough information in my former post. This project is getting rather large, and sometimes I briefly lose track of relevant code. :o Although, I've kept it well organized, be assured! I will post the results of my efforts soon. Thanks again.

I'd like to give the Sub that creates the datagrid as LetsCode requested.
Hope this isn't too much code for y'all to handle! Believe me there's a lot more in the project as a whole! Here we go:

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal E As System.EventArgs) Handles btnSearch.Click

        Dim Connect As OleDbConnection = New OleDbConnection()
        Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter()
        Dim SelectStatement, ConnectString As String
        Dim WhereClause As String

        WhereClause = "Where "
        If txtSoftwareNum.Text <> "" Then
            WhereClause = WhereClause & "InStr([Software #],'" & _
            txtSoftwareNum.Text & "')>0 AND "
        End If
        If txtSoftwareName.Text <> "" Then
            WhereClause = WhereClause & "InStr([Software Name],'" & _
            txtSoftwareName.Text & "')>0 AND "
        End If
        If txtVersion.Text <> "" Then
            WhereClause = WhereClause & "InStr(Version,'" & _
            txtVersion.Text & "')>0 AND "
        End If
        If txtLocation.Text <> "" Then
            WhereClause = WhereClause & "InStr(Location,'" & _
            txtLocation.Text & "')>0 AND "
        End If
        If txtSoftwareBrand.Text <> "" Then
            WhereClause = WhereClause & "InStr([Soft Brand],'" & _
            txtSoftwareBrand.Text & "')>0 AND "
        End If
        If txtDatePurchased.Text <> "" Then
            WhereClause = WhereClause & "InStr(DatePurchased,'" & _
            txtDatePurchased.Text & "')>0 AND "
        End If
        If txtFirstName.Text <> "" Then
            WhereClause = WhereClause & "InStr(FirstName,'" & _
            txtFirstName.Text & "')>0 AND "
        End If
        If txtLastName.Text <> "" Then
            WhereClause = WhereClause & "InStr(LastName,'" & _
            txtLastName.Text & "')>0 AND "
        End If
        If txtSerialNumber.Text <> "" Then
            WhereClause = WhereClause & "InStr([Serial Number],'" & _
            txtSerialNumber.Text & "')>0 AND "
        End If
        If txtModel.Text <> "" Then
            WhereClause = WhereClause & "InStr(Model,'" & _
            txtModel.Text & "')>0 AND "
        End If
        If Right(WhereClause, 4) = "AND " Then
            WhereClause = Left(WhereClause, Len(WhereClause) - 4)
        End If

        SelectStatement = "Select * From [SOFTWARE DATABASE] " & WhereClause

        ' If they didnt enter anything in the textboxes:
        If txtSoftwareNum.Text = "" And txtSoftwareName.Text = "" And _
        txtVersion.Text = "" And txtLocation.Text = "" And _
        txtSoftwareBrand.Text = "" And txtDatePurchased.Text = "" And _
        txtFirstName.Text = "" And txtLastName.Text = "" And _
        txtSerialNumber.Text = "" And txtModel.Text = "" Then
            SelectStatement = "Select * From [SOFTWARE DATABASE]"
        Else
        End If
        ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=c:\Inetpub\wwwroot\ASPproject\WebApplication2\software\softwaredb.mdb"

        Connect.ConnectionString = ConnectString
        Adapter.SelectCommand = _
        New OleDbCommand(SelectStatement, Connect)
        Adapter.SelectCommand.Connection.Open()
        Adapter.Fill(softwareDS, "[SOFTWARE DATABASE]")
        softwareGrid.DataSource = softwareDS.Tables("[SOFTWARE DATABASE]")
        Page.DataBind()
    End Sub

Since LetsCode's last post I have reevaluated the values of my datagrid textboxes and believe I have solved the pervious indexing error. I won't know for sure until I address the other major error I keep returning to, given as follows:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 245:
Line 246: If ValidEntry(tbSoftwareNum, tbSoftwareName, tbLocation) Then
Line 247: Dim dr As DataRow = dsSoftware.Tables("[SOFTWARE DATABASE]").Rows(E.Item.ItemIndex)
Line 248: Try
Line 249: dr("Software #") = tbSoftwareNum


Source File: c:\inetpub\wwwroot\ASPproject\WebApplication2\WebForm1.aspx.vb Line: 247

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
WebApplication2.WebForm1.softwareGrid_UpdateCommand(Object source, DataGridCommandEventArgs E) in c:\inetpub\wwwroot\ASPproject\WebApplication2\WebForm1.aspx.vb:247
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()

:idea: :idea: :idea:

This is part of the new and improved Sub softwareGrid_UpdateCommand, which is as follows:

Public Sub softwareGrid_UpdateCommand(ByVal source As Object, ByVal E As DataGridCommandEventArgs) Handles softwareGrid.UpdateCommand
        Dim tbSoftwareNum As String
        tbSoftwareNum = E.Item.Cells(0).Text
        Dim tbSoftwareName As String
        tbSoftwareName = CType(E.Item.Cells(1).Controls(0), TextBox).Text
        Dim tbVersion As String
        tbVersion = CType(E.Item.Cells(2).Controls(0), TextBox).Text
        Dim tbLocation As String
        tbLocation = CType(E.Item.Cells(3).Controls(0), TextBox).Text
        Dim tbSoftBrand As String
        tbSoftBrand = CType(E.Item.Cells(4).Controls(0), TextBox).Text
        Dim tbDatePurchased As String
        tbDatePurchased = CType(E.Item.Cells(5).Controls(0), TextBox).Text
        Dim tbFirstName As String
        tbFirstName = CType(E.Item.Cells(6).Controls(0), TextBox).Text
        Dim tbLastName As String
        tbLastName = CType(E.Item.Cells(7).Controls(0), TextBox).Text
        Dim tbSerialNumber As String
        tbSerialNumber = CType(E.Item.Cells(8).Controls(0), TextBox).Text
        Dim tbModel As String
        tbModel = CType(E.Item.Cells(9).Controls(0), TextBox).Text

        Debug.WriteLine("Soft Num")
        Debug.WriteLine(tbSoftwareNum)
        Debug.WriteLine("Software Name")
        Debug.WriteLine(tbSoftwareName)
        Debug.WriteLine("Version")
        Debug.WriteLine(tbVersion)
        Debug.WriteLine("Location")
        Debug.WriteLine(tbLocation)
        Debug.WriteLine("Soft Brand")
        Debug.WriteLine(tbSoftBrand)
        Debug.WriteLine("DatePurchased")
        Debug.WriteLine(tbDatePurchased)
        Debug.WriteLine("First Name")
        Debug.WriteLine(tbFirstName)
        Debug.WriteLine("Last Name")
        Debug.WriteLine(tbLastName)
        Debug.WriteLine("Serial Number")
        Debug.WriteLine(tbSerialNumber)
        Debug.WriteLine("Model")
        Debug.WriteLine(tbModel)
       
        If ValidEntry(tbSoftwareNum, tbSoftwareName, tbLocation) Then
            Dim dr As DataRow = dsSoftware.Tables("[SOFTWARE DATABASE]").Rows(E.Item.ItemIndex)
            Try
                dr("Software #") = tbSoftwareNum
                dr("Software Name") = tbSoftwareName
                dr("Version") = tbVersion
                dr("Location") = tbLocation
                dr("Soft Brand") = tbSoftBrand
                dr("DatePurchased") = tbDatePurchased
                dr("FirstName") = tbFirstName
                dr("LastName") = tbLastName
                dr("Serial Number") = tbSerialNumber
                dr("Model") = tbModel
                Me.UpdateDataBase()
                softwareGrid.EditItemIndex = -1
                Me.BindDataGrid()
            Catch eConstraint As ConstraintException
                lblMessage.Text = "A category with that ID already exists."
            End Try
        End If
    End Sub

Anyone know how to check the validity of the line:

Dim dr As DataRow = dsSoftware.Tables("[SOFTWARE DATABASE]").Rows(E.Item.ItemIndex)

Thank you for troubling through all this code. Any and all help will be welcomed with open arms! :mrgreen: I refuse to give up! Optimism thrives still!

dsSoftware.Tables("[SOFTWARE DATABASE]").Rows(E.Item.ItemIndex)

Is [SOFTWARE DATABASE] the table name or database name?
It should be the table to execute properly.

dsSoftware.Tables("[SOFTWARE DATABASE]").Rows(E.Item.ItemIndex)

Is [SOFTWARE DATABASE] the table name or database name?
It should be the table to execute properly.

[SOFTWARE DATABASE] is the table name. In brackets so it can recognize the name with the space. Terrible name, but hey, I didn't pick it. Any ideas on this?

Solved my own problem. I had 2 separate instances of the same data set. Thank you for those who assisted me in this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.