The Datagrid: How does one fill text boxes in edit mode with their original content?

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2005
Posts: 41
Reputation: npasma is an unknown quantity at this point 
Solved Threads: 0
npasma's Avatar
npasma npasma is offline Offline
Light Poster

The Datagrid: How does one fill text boxes in edit mode with their original content?

 
0
  #1
Jul 20th, 2005
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:

  1. Public Sub softwareGrid_EditCommand(ByVal source As Object, _
  2. ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
  3. Handles softwareGrid.EditCommand
  4. softwareGrid.EditItemIndex = e.Item.ItemIndex
  5.  
  6. Dim test As String
  7. test = softwareGrid.EditItemIndex
  8. Debug.WriteLine(test)
  9. Me.BindDataGrid()
  10. 'Dim txtSoftNum As String
  11. 'txtSoftNum = CType(e.Item.Cells(0).Controls(0), TextBox).Text
  12. 'Debug.WriteLine("this is the content of the software number text box:")
  13. 'Debug.WriteLine(txtSoftNum)
  14. End Sub
  15.  
  16. Public Sub softwareGrid_CancelCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
  17. softwareGrid.EditItemIndex = -1
  18. BindDataGrid()
  19. End Sub
  20.  
  21. Private Sub BindDataGrid()
  22. softwareGrid.DataSource = softwareDS
  23. softwareGrid.DataMember = "SOFTWARE DATABASE"
  24. softwareGrid.DataKeyField = "Software #"
  25. End Sub
  26.  
  27. Public Sub softwareGrid_UpdateCommand(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
  28. Dim tbSoftwareNum As TextBox = E.Item.Cells(0).Controls(0)
  29. Dim tbSoftwareName As TextBox = E.Item.Cells(1).Controls(0)
  30. Dim tbVersion As TextBox = E.Item.Cells(2).Controls(0)
  31. Dim tbLocation As TextBox = E.Item.Cells(3).Controls(0)
  32. Dim tbSoftBrand As TextBox = E.Item.Cells(4).Controls(0)
  33. Dim tbDatePurchased As TextBox = E.Item.Cells(5).Controls(0)
  34. Dim tbFirstName As TextBox = E.Item.Cells(6).Controls(0)
  35. Dim tbLastName As TextBox = E.Item.Cells(7).Controls(0)
  36. Dim tbSerialNumber As TextBox = E.Item.Cells(8).Controls(0)
  37. Dim tbModel As TextBox = E.Item.Cells(9).Controls(0)
  38. Dim txtSoftwareNum As String
  39. txtSoftwareNum = CType(E.Item.Cells(0).Controls(0), TextBox).Text
  40. Debug.WriteLine("Soft Num")
  41. Debug.WriteLine(txtSoftwareNum)
  42. Dim txtModel As String
  43. txtModel = CType(E.Item.Cells(9).Controls(0), TextBox).Text
  44. Debug.WriteLine("Model")
  45. Debug.WriteLine(txtModel)
  46. 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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 41
Reputation: npasma is an unknown quantity at this point 
Solved Threads: 0
npasma's Avatar
npasma npasma is offline Offline
Light Poster

Re: The Datagrid: How does one fill text boxes in edit mode with their original content?

 
0
  #2
Jul 20th, 2005
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. :-|
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Re: The Datagrid: How does one fill text boxes in edit mode with their original content?

 
0
  #3
Jul 22nd, 2005
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/art...71002-1.2.aspx

HOPE IT HELPS
Save White Tiger
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 41
Reputation: npasma is an unknown quantity at this point 
Solved Threads: 0
npasma's Avatar
npasma npasma is offline Offline
Light Poster

Re: The Datagrid: How does one fill text boxes in edit mode with their original content?

 
0
  #4
Jul 22nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 41
Reputation: npasma is an unknown quantity at this point 
Solved Threads: 0
npasma's Avatar
npasma npasma is offline Offline
Light Poster

Re: The Datagrid: How does one fill text boxes in edit mode with their original content?

 
0
  #5
Jul 22nd, 2005
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:


  1. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal E As System.EventArgs) Handles btnSearch.Click
  2.  
  3. Dim Connect As OleDbConnection = New OleDbConnection()
  4. Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter()
  5. Dim SelectStatement, ConnectString As String
  6. Dim WhereClause As String
  7.  
  8. WhereClause = "Where "
  9. If txtSoftwareNum.Text <> "" Then
  10. WhereClause = WhereClause & "InStr([Software #],'" & _
  11. txtSoftwareNum.Text & "')>0 AND "
  12. End If
  13. If txtSoftwareName.Text <> "" Then
  14. WhereClause = WhereClause & "InStr([Software Name],'" & _
  15. txtSoftwareName.Text & "')>0 AND "
  16. End If
  17. If txtVersion.Text <> "" Then
  18. WhereClause = WhereClause & "InStr(Version,'" & _
  19. txtVersion.Text & "')>0 AND "
  20. End If
  21. If txtLocation.Text <> "" Then
  22. WhereClause = WhereClause & "InStr(Location,'" & _
  23. txtLocation.Text & "')>0 AND "
  24. End If
  25. If txtSoftwareBrand.Text <> "" Then
  26. WhereClause = WhereClause & "InStr([Soft Brand],'" & _
  27. txtSoftwareBrand.Text & "')>0 AND "
  28. End If
  29. If txtDatePurchased.Text <> "" Then
  30. WhereClause = WhereClause & "InStr(DatePurchased,'" & _
  31. txtDatePurchased.Text & "')>0 AND "
  32. End If
  33. If txtFirstName.Text <> "" Then
  34. WhereClause = WhereClause & "InStr(FirstName,'" & _
  35. txtFirstName.Text & "')>0 AND "
  36. End If
  37. If txtLastName.Text <> "" Then
  38. WhereClause = WhereClause & "InStr(LastName,'" & _
  39. txtLastName.Text & "')>0 AND "
  40. End If
  41. If txtSerialNumber.Text <> "" Then
  42. WhereClause = WhereClause & "InStr([Serial Number],'" & _
  43. txtSerialNumber.Text & "')>0 AND "
  44. End If
  45. If txtModel.Text <> "" Then
  46. WhereClause = WhereClause & "InStr(Model,'" & _
  47. txtModel.Text & "')>0 AND "
  48. End If
  49. If Right(WhereClause, 4) = "AND " Then
  50. WhereClause = Left(WhereClause, Len(WhereClause) - 4)
  51. End If
  52.  
  53. SelectStatement = "Select * From [SOFTWARE DATABASE] " & WhereClause
  54.  
  55. ' If they didnt enter anything in the textboxes:
  56. If txtSoftwareNum.Text = "" And txtSoftwareName.Text = "" And _
  57. txtVersion.Text = "" And txtLocation.Text = "" And _
  58. txtSoftwareBrand.Text = "" And txtDatePurchased.Text = "" And _
  59. txtFirstName.Text = "" And txtLastName.Text = "" And _
  60. txtSerialNumber.Text = "" And txtModel.Text = "" Then
  61. SelectStatement = "Select * From [SOFTWARE DATABASE]"
  62. Else
  63. End If
  64. ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  65. "Data Source=c:\Inetpub\wwwroot\ASPproject\WebApplication2\software\softwaredb.mdb"
  66.  
  67. Connect.ConnectionString = ConnectString
  68. Adapter.SelectCommand = _
  69. New OleDbCommand(SelectStatement, Connect)
  70. Adapter.SelectCommand.Connection.Open()
  71. Adapter.Fill(softwareDS, "[SOFTWARE DATABASE]")
  72. softwareGrid.DataSource = softwareDS.Tables("[SOFTWARE DATABASE]")
  73. Page.DataBind()
  74. End Sub
  75.  
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 41
Reputation: npasma is an unknown quantity at this point 
Solved Threads: 0
npasma's Avatar
npasma npasma is offline Offline
Light Poster

Re: The Datagrid: How does one fill text boxes in edit mode with their original content?

 
0
  #6
Jul 22nd, 2005
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()



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

  1. Public Sub softwareGrid_UpdateCommand(ByVal source As Object, ByVal E As DataGridCommandEventArgs) Handles softwareGrid.UpdateCommand
  2. Dim tbSoftwareNum As String
  3. tbSoftwareNum = E.Item.Cells(0).Text
  4. Dim tbSoftwareName As String
  5. tbSoftwareName = CType(E.Item.Cells(1).Controls(0), TextBox).Text
  6. Dim tbVersion As String
  7. tbVersion = CType(E.Item.Cells(2).Controls(0), TextBox).Text
  8. Dim tbLocation As String
  9. tbLocation = CType(E.Item.Cells(3).Controls(0), TextBox).Text
  10. Dim tbSoftBrand As String
  11. tbSoftBrand = CType(E.Item.Cells(4).Controls(0), TextBox).Text
  12. Dim tbDatePurchased As String
  13. tbDatePurchased = CType(E.Item.Cells(5).Controls(0), TextBox).Text
  14. Dim tbFirstName As String
  15. tbFirstName = CType(E.Item.Cells(6).Controls(0), TextBox).Text
  16. Dim tbLastName As String
  17. tbLastName = CType(E.Item.Cells(7).Controls(0), TextBox).Text
  18. Dim tbSerialNumber As String
  19. tbSerialNumber = CType(E.Item.Cells(8).Controls(0), TextBox).Text
  20. Dim tbModel As String
  21. tbModel = CType(E.Item.Cells(9).Controls(0), TextBox).Text
  22.  
  23. Debug.WriteLine("Soft Num")
  24. Debug.WriteLine(tbSoftwareNum)
  25. Debug.WriteLine("Software Name")
  26. Debug.WriteLine(tbSoftwareName)
  27. Debug.WriteLine("Version")
  28. Debug.WriteLine(tbVersion)
  29. Debug.WriteLine("Location")
  30. Debug.WriteLine(tbLocation)
  31. Debug.WriteLine("Soft Brand")
  32. Debug.WriteLine(tbSoftBrand)
  33. Debug.WriteLine("DatePurchased")
  34. Debug.WriteLine(tbDatePurchased)
  35. Debug.WriteLine("First Name")
  36. Debug.WriteLine(tbFirstName)
  37. Debug.WriteLine("Last Name")
  38. Debug.WriteLine(tbLastName)
  39. Debug.WriteLine("Serial Number")
  40. Debug.WriteLine(tbSerialNumber)
  41. Debug.WriteLine("Model")
  42. Debug.WriteLine(tbModel)
  43.  
  44. If ValidEntry(tbSoftwareNum, tbSoftwareName, tbLocation) Then
  45. Dim dr As DataRow = dsSoftware.Tables("[SOFTWARE DATABASE]").Rows(E.Item.ItemIndex)
  46. Try
  47. dr("Software #") = tbSoftwareNum
  48. dr("Software Name") = tbSoftwareName
  49. dr("Version") = tbVersion
  50. dr("Location") = tbLocation
  51. dr("Soft Brand") = tbSoftBrand
  52. dr("DatePurchased") = tbDatePurchased
  53. dr("FirstName") = tbFirstName
  54. dr("LastName") = tbLastName
  55. dr("Serial Number") = tbSerialNumber
  56. dr("Model") = tbModel
  57. Me.UpdateDataBase()
  58. softwareGrid.EditItemIndex = -1
  59. Me.BindDataGrid()
  60. Catch eConstraint As ConstraintException
  61. lblMessage.Text = "A category with that ID already exists."
  62. End Try
  63. End If
  64. 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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Re: The Datagrid: How does one fill text boxes in edit mode with their original content?

 
0
  #7
Jul 24th, 2005
  1. 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.
Save White Tiger
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 41
Reputation: npasma is an unknown quantity at this point 
Solved Threads: 0
npasma's Avatar
npasma npasma is offline Offline
Light Poster

Re: The Datagrid: How does one fill text boxes in edit mode with their original content?

 
0
  #8
Jul 25th, 2005
Originally Posted by Letscode
  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 41
Reputation: npasma is an unknown quantity at this point 
Solved Threads: 0
npasma's Avatar
npasma npasma is offline Offline
Light Poster

Re: The Datagrid: How does one fill text boxes in edit mode with their original content?

 
0
  #9
Jul 26th, 2005
Solved my own problem. I had 2 separate instances of the same data set. Thank you for those who assisted me in this.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC