Hi All.
I have just registered, I appologise that my first thread is a question but I am at my witts end with this problem. I am sure it is a simple fix and I am missing something obvious but any help would be very welcome!
I have a form which has a number of Textboxes (approx 30) bound to a recordset ('EquipData') table called "ThisEquipment".
I am trying to loop through each Textbox in the Form and, if the textbox is populated, change the dataset to its value.
At the end I want to update the original table in the database.
Each textbox is name 'txt_###' where ### is the name of the field. So I loop through all textboxes, remove the 'txt_' and populate the field of that name with the textboxes .text value.
My problem is this.. it doesnt work! If i remove the entire 'For each Textbox.. Next' loop and manually set each field in the database individually (still using the '.Name.Remove(0, 4)') it works. If I put the loop back in though it does not throw up an error, it just doesnt change the original data.
Any help (or alternative methods) would be gratefully appreciated. Thanks.
I have just registered, I appologise that my first thread is a question but I am at my witts end with this problem. I am sure it is a simple fix and I am missing something obvious but any help would be very welcome!
I have a form which has a number of Textboxes (approx 30) bound to a recordset ('EquipData') table called "ThisEquipment".
I am trying to loop through each Textbox in the Form and, if the textbox is populated, change the dataset to its value.
At the end I want to update the original table in the database.
Each textbox is name 'txt_###' where ### is the name of the field. So I loop through all textboxes, remove the 'txt_' and populate the field of that name with the textboxes .text value.
My problem is this.. it doesnt work! If i remove the entire 'For each Textbox.. Next' loop and manually set each field in the database individually (still using the '.Name.Remove(0, 4)') it works. If I put the loop back in though it does not throw up an error, it just doesnt change the original data.
Any help (or alternative methods) would be gratefully appreciated. Thanks.
VB.NET:
Dim EquipData As New DataSet
Dim EquipID As Integer
Public Sub New(ByVal EquipID As Integer)
' This call is required by the designer.
InitializeComponent()
' Create the adaptor
Dim EquipAdapter As New SqlDataAdapter("Select * from Equipment where EquipID=" & EquipID & ";", ServiceDBConn) ' This item of equipment
Dim AllCountriesAdapter As New SqlDataAdapter("Select distinct Country from Equipment;", ServiceDBConn) ' DISTINCT list of all countries
Dim AllEquipAdapter As New SqlDataAdapter("Select distinct Equipment from Equipment;", ServiceDBConn) ' DISTINCT list of all equipment
Dim Builder As New SqlCommandBuilder(EquipAdapter)
' Open Database
ServiceDBConn.Open()
' Use adapters to fill Equipdata Dataset with tables
EquipAdapter.Fill(EquipData, "ThisEquipment")
AllCountriesAdapter.Fill(EquipData, "DistinctCountries")
AllEquipAdapter.Fill(EquipData, "DistinctEquipment")
' Close database and tidy up
ServiceDBConn.Close()
EquipAdapter = Nothing
AllCountriesAdapter = Nothing
AllEquipAdapter = Nothing
' If more than record set to show the 1st
Me.BindingContext(EquipData.Tables("ThisEquipment")).Position = 1
' Set form title
Me.Text = EquipData.Tables("ThisEquipment").Rows(0).Item("SerNo") & " Equipment Information"
' Populateform
BindData() ' All this does is set binding for each textbox
' Set to be subform of Main
Me.MdiParent = Main
End Sub
VB.NET:
Private Sub tsb_Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb_Ok.Click
Dim EquipAdapter As New SqlDataAdapter("Select * from Equipment where EquipID=" & EquipID & ";", ServiceDBConn) ' This item of equipment
Dim Builder As New SqlCommandBuilder(EquipAdapter)
Dim NewEquipData As New DataSet
Try
' For each textbox, check empty and if not, write to database
For Each C As Control In Me.Controls
If TypeOf C Is GroupBox Then
For Each C2 As Control In C.Controls.OfType(Of TextBox)()
If Not (C2.Text = vbNullString) Then
EquipData.Tables("ThisEquipment").Rows(0).Item(C2.Name.Remove(0, 4)) = C2.Text
End If
Next
End If
Next
' Open Database and update dataset
ServiceDBConn.Open()
EquipAdapter.Update(EquipData, "ThisEquipment")
' Close Database
ServiceDBConn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
' Close form
Me.Close()
End Sub