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