Hey everyone, I'm going to do my best to explain this easily, even though the code might not be the easiest to follow.
I have an "Edit" form made out of a TabContainer and TabPanels that I am populating with results from a few different datasets, after a user clicks the "Next" or "Submit" buttons on the form, I iterate through the control set of that TabPanel, and compare them to the original values, if they are different than I store that in an ArrayList and later sieve through it to add records to the Update Database Table.
I have this functionality working for all the controls inside a TabPanel that are not enclosed in another "Panel" control on the site. The code below is what I am using to jump into the panel inside a TabPanel that contains only checkboxes and textboxes, which are directly related to one another (AKA if a user clicks the checkbox, it enables the textbox for text entry).
What I am trying to do is loop through these checkbox controls and then compare them to what is already in the database, if the record exists in the database (AKA the checkbox is already checked) than compare the textbox values, and update if the values are different.
If a new checkbox has been checked (AKA checkbox text value doesn't exist in the database) I would like to store the new value in an arrayList and later iterate through it and store that in a subsequent table, which I have already coded.
My problem is that when I iterate through the controls, if the control is checked, I store the text value of the new control that has been checked to be compared to the DataSet results.
What I want to do is basically, if the new checkbox text isn't apparent in the dataset, than store that information as a New addition and later i will purge the arrayList and store all of the info.
The problem I am having is that once I am in the control loop and access the loop through the dataset, the code will compare the Control to every record in the DataSet result, even if it has already discovered that it is stored in the Database.
I have elaborated even more in comments within my code below.
For Each innerctrl As Object In c.Controls
Dim id As String = innerctrl.ID
Dim ctrlValue As String
Dim ctrlDesc As String
'If the control is a panel, than it contains other controls, mainly checkboxes and textbox, check through that control and store these results as well
If TypeOf innerctrl is Panel Then
'If the control inside the tabpanel is a "Panel" then continue
Dim pnlControls As Control = TryCast(innerctrl, Control)
'use variable pnlControls as a reference to the controls inside this panel
For Each pnlCtrl As Control In pnlControls.Controls
'for each object/control inside this panel control set do the followin
id = Nothing
id = pnlCtrl.ID
MsgBox(id)
'clear the id variable to be filled depending on control type
If TypeOf pnlCtrl Is CheckBox Then
'If control is a checkbox, which most likely is
If DirectCast(pnlCtrl, CheckBox).Checked = True Then
'If the checkbox is checked inside the panel
Dim idText As String = DirectCast(pnlCtrl, CheckBox).Text
Dim txtValue As String
'Set the id equal to the text of the checkbox such as "Present Suicide Ideation"
Select Case idText
'Check for the different possible values of id
Case "Harm to Others"
txtValue = OthersHarmText.Text
Case "Present Suicide Ideation"
txtValue = PresSuiText.Text
Case "History of Suicide Attempts"
txtValue = SuiHistText.Text
Case "Self-Harm"
txtValue = SelfHarmText.Text
Case "Present Eating Disorder"
txtValue = EatDisText.Text
Case "Fire-Setting"
txtValue = FireStartText.Text
Case "Access to Firearms"
txtValue = GunAccessText.Text
End Select
'After seeing the control was a checkbox and storing the coinciding textbox value check against values in db
'Here are where my issues begin.......
'Basically if idText = "Harm to Others" and the first Row Value = "Harm to Others" and the txtValue = DBCheckVal (the textbox value is the same now as what is stored in the db)
'then I want to jump back into the control loop until the next Checkbox that is checked is found, but the DataSet continues to be looped through before jumping back into the Controls.
'How could I fix that?
Dim dt As System.Data.DataTable
dt = Session("CopySafety").tables(0)
If idText IsNot Nothing Then
For i = 0 To dt.Rows.Count - 1
'if id contains a value
'while iterating through the original data compare values from DB to values submitted
Dim DBCheckVal As String = dt.Rows(i).Item("SafChecked").ToString
Dim DBTextVal As String = dt.Rows(i).Item("SafText").ToString
If idText = DBCheckVal Then
'if the id stored from submitted control equals ID in the database
If txtValue <> DBTextVal Then
'If value in the textbox not equal to value in the database change occurred
_UpdateItemsList.Add("Changed " & idText & " From " & DBTextVal & " to " & txtValue)
End If
Else
'if id is not equal to the stored id, than it is a new insert and update
_UpdateItemsList.Add("Added " & idText & " : " & txtValue)
MsgBox("Added " & idText & " : " & txtValue)
End If
DBCheckVal = Nothing
DBTextVal = Nothing
Next
End If
End If
End If
Next
'After this isn't involved in where I am having the problems, all the basic controls are working great.
ElseIf TypeOf innerctrl Is TextBox Then
ctrlValue = DirectCast(innerctrl, TextBox).Text
ctrlDesc = DirectCast(innerctrl, TextBox).ToolTip
ElseIf TypeOf innerctrl Is CheckBox Then
ctrlValue = DirectCast(innerctrl, CheckBox).Text
ctrlDesc = DirectCast(innerctrl, CheckBox).ToolTip
ElseIf TypeOf innerctrl Is DropDownList Then
ctrlValue = DirectCast(innerctrl, DropDownList).SelectedValue
ctrlDesc = DirectCast(innerctrl, DropDownList).ToolTip
ElseIf TypeOf innerctrl Is RadioButtonList Then
ctrlValue = DirectCast(innerctrl, RadioButtonList).SelectedValue
ctrlDesc = DirectCast(innerctrl, RadioButtonList).ToolTip
End If
If id IsNot Nothing And ctrlValue IsNot Nothing Then
Dim dt As System.Data.DataTable
dt = Session("CopyDS").Tables(0)
For i = 0 To dt.Rows(0).ItemArray.Count - 1
Dim ColName As String = dt.Columns(i).ToString
Dim RowVal As String = dt.Rows(0).Item(i).ToString
'copyData.Text += dt.Columns(i).ToString & " : "
'copyData.Text += dt.Rows(0).Item(i).ToString & "<br/>"
If ColName = id Then
'MsgBox("Colname and ID are equal to : " & ColName)
If RowVal <> ctrlValue Then
_UpdateItemsList.Add("Changed " & ctrlDesc & " From " & RowVal & " to " & ctrlValue)
MsgBox("Changed " & ctrlDesc & " from " & RowVal & " to " & ctrlValue)
End If
End If
ColName = Nothing
RowVal = Nothing
Next
End If
ctrlValue = Nothing
ctrlDesc = Nothing
Next