Link to home
Start Free TrialLog in
Avatar of isdc
isdc

asked on

Updating an arraylist to SQL Database

I had posted here last week regarding an issue "VeryNiceMan" had helped me.

https://www.experts-exchange.com/questions/21864141/Updating-dynamic-controls-to-a-database-VB-NET.html 

NOW i'm stuck trying to get this update a database.  For some reason my eyes just aren't seeing where to begin.

I've tried (and successfully i think) gotten things into an arraylist, but it's not updating the database as i would like to see it.  

Here's some of my "updated" code.

-- This is where i dynamically create controls based on a checkboxlist of 13 items.  This part seems to be working as intended.

Private Sub CreateMyControls()
        Dim i As Integer
        For i = 0 To cblPatientGoals.Items.Count - 1
            If cblPatientGoals.Items(i).Selected Then
                Dim myLabel As New Label
                myLabel.Text &= cblPatientGoals.Items(i).Text

                Dim myRadioButton As New RadioButtonList
                myRadioButton.Items.Add(New ListItem("Met", "Met"))
                myRadioButton.Items.Add(New ListItem("Unmet", "Unmet"))
                myRadioButton.RepeatDirection = RepeatDirection.Horizontal
                Dim myTextBox As New TextBox
                myTextBox.Style("Width") = "350px"

                myRadioButton.ID = "cGoals" & (i+1)
                myTextBox.ID = "tGoals" & (i+1)
                myLabel.ID = "lGoals" & (i+1)

                Panel1.Controls.Add(New LiteralControl("<tr><td>"))
                Panel1.Controls.Add(myLabel)
                Panel1.Controls.Add(New LiteralControl("</td>"))
                Panel1.Controls.Add(New LiteralControl("<td>"))
                Panel1.Controls.Add(myRadioButton)
                Panel1.Controls.Add(New LiteralControl("</td>"))
                Panel1.Controls.Add(New LiteralControl("<td>"))
                Panel1.Controls.Add(myTextBox)
                Panel1.Controls.Add(New LiteralControl("</td></tr>"))
            End If
        Next
    End Sub



--This is the code that (obviously) executes when the Submit Button is pushed.  This is where i need some direction.

In my arraylist where i write out "Response.Write(CStr(al.Item(x)))" i can see the items i want to update, but it's only updating one of the 2 items in the checkboxlist 6 times.  I'll paste more info as needed.  

Sub Button1_Click(Sender as Object, E as Eventargs)
    Dim i As Integer
    Dim t As TextBox
    Dim l As Label
    Dim c As RadioButtonList
      Dim al As New ArrayList()

    'For x = 0 To cblPatientGoals.Items.Count - 1
      'If cblPatientGoals.Items(x).Selected Then
      
      For i = 0 To Panel1.Controls.Count - 1        
            
            If Panel1.Controls(i).GetType Is GetType(Label) Then
        l = CType(Panel1.Controls(i), Label)
        'Response.Write(l.ID & "-->" & l.Text.Trim & "<BR>")
            al.Insert(0, l.text.trim)
        End If
            
            If Panel1.Controls(i).GetType Is GetType(RadioButtonList) Then
        c = CType(Panel1.Controls(i), RadiobuttonList)
        'Response.Write(c.ID & "-->" & c.SelectedItem.Text & "<BR>")
            al.Insert(1, c.SelectedItem.Text)
            'Response.Write(c.ID & "-->" & c.Items(0).Selected & "-->" & c.Items(1).Selected & "<BR>")
        End If
            
            If Panel1.Controls(i).GetType Is GetType(TextBox) Then
        t = CType(Panel1.Controls(i), TextBox)
        'Response.Write(t.ID & "-->" & t.Text.Trim & "<BR>")
            al.Insert(2, t.text.trim)
        End If        
        
       Next
      
      Dim x As Integer  
      For x = 0 To al.Count - 1
       Response.Write(CStr(al.Item(x)))
      
      Dim myConnection as New SQLConnection(ConfigurationSettings.appSettings("connString"))
    myConnection.Open()
      Dim myInsert as String = "INSERT INTO tblEducationGoals (goal_id, goal_MetUnmet, goal_text)" & _
      "VALUES ('" & al.item(0) & "', '" & al.item(1) & "', '" & al.item(2) & "')"
      Dim Cmd as New SQLCommand(MyInsert, myConnection)
      cmd.ExecuteNonQuery
      MyConnection.Close()
      
      
      Next
End Sub


Thanks again.

dc.
ASKER CERTIFIED SOLUTION
Avatar of lojk
lojk
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry guess that shoudl say....

public class clsResult
GoalID as STRING '''' at no point did you say it was an integer, just assumed!
GoalMet as String
GoalText as String
End Class
Avatar of isdc
isdc

ASKER

Yes, apologies on my lack of attentiveness to this. Involvement in other things prevented me from trying this.  I accept this as well and i thank everyone for the assistance with this.  I am happy to see a board that is monitored so closely.