Hi there, I'm having a problem with updating and/or inserting rows in a table in my database. I have a form with a combobox, 10 textboxes, and save button. Here, the combobox if filled with illnesses from the diagnose table in my database. Diagnose table's structure is: f_id, illness, symptoms, so an illness can have many symptoms. If an item is selected from the combobox, the symptoms will be displayed on the textboxes. I already got this working. My problem is that I should make it able to insert or update the rows in the table. Here's my code for reading the illness and symptoms:
and here's my code for inserting or updating. I get error "Parameter 'ill' has already been defined."
The codes marked as comment is my code for updating, but that's not working so I set it aside for the mean time. The table looks like this:
f_id| illness | symptom
1 | fever | fever
2 | fever | hot temperature
3 | fever | dizziness
4 | fever | headache
so in that case, say I chose fever in the combobox then it will display the symptoms on the 4 textboxes. If the user made changes, the Duplicate() function checks if the combobox value already have a record. If true then it will update. Say the user added another symptom, so if save button is clicked, fever will add another row with the added symptom. If false, then a new record will be added, which means new row or rows will be added in the table dependeng on the number of symptoms inputted on the textboxes. So, say 'cold' is to be added as a new record and I entered 2 symptoms, this means that I used 2 of the 10 textboxes in the form, then 2 rows will be added on the table. I already spent a day trying to make this work but failed. Please help me make this work. Any help will be greatly appreciated. Thanks in advance, Godbless
Code:
Call Connect()
Dim str As String
str = "Select sname from diagnose where first_aid = @ill"
cmd.Parameters.AddWithValue("ill", cmbRecord.Text)
cmd.Connection = myConn
cmd.CommandText = str
dtr = cmd.ExecuteReader
Dim symptoms As New List(Of String)
While dtr.Read()
symptoms.Add(dtr("sname"))
End While
'set available symptoms
Dim arrayOfTextboxes() As TextBox = {symp0, symp1, symp2, symp3, symp4, symp5, symp6, symp7, symp8, symp9}
Dim i As Integer = 0
For i = 0 To symptoms.Count - 1
arrayOfTextboxes(i).Text = symptoms(i)
Next
'clear other textboxes
For j = i To UBound(arrayOfTextboxes)
arrayOfTextboxes(j).Text = String.Empty
Next
myConn.Close()
Code:
Call Connect()
If Duplicate() = False Then
Dim dt As New DataTable("diagnose")
Dim row As DataRow
Dim arrayOfTextboxes() As TextBox = {symp0, symp1, symp2, symp3, symp4, symp5, symp6, symp7, symp8, symp9}
Dim symptoms As New List(Of String)
STRSQL = "insert into diagnose values (@ill, @sym)"
Using myCmd = New MySqlCommand(STRSQL, myConn)
myConn.Open()
Dim i As Integer = 0
For i = 0 To arrayOfTextboxes.Count - 1
If String.IsNullOrEmpty(arrayOfTextboxes(i).Text) Then Continue For
If arrayOfTextboxes(i).Text <> "" Then
myCmd.Parameters.AddWithValue("ill", cmbRecord.Text)
myCmd.Parameters.AddWithValue("sym", arrayOfTextboxes(i).Text)
row = dt.NewRow()
myCmd.ExecuteNonQuery()
End If
Next
MsgBox("Record Added")
myConn.Close()
End Using
'Else
' STRSQL = "Update diagnose set first_aid = @ill, sname = @symp where first_aid = @ill"
' Using myCmd = New MySqlCommand(STRSQL, myConn)
' myConn.Open()
' myCmd.Parameters.AddWithValue("ill", cmbRecord.Text)
' myCmd.Parameters.AddWithValue("sym", symp0.Text)
' myCmd.Parameters.AddWithValue("sym", symp1.Text)
' myCmd.Parameters.AddWithValue("sym", symp2.Text)
' myCmd.Parameters.AddWithValue("sym", symp3.Text)
' myCmd.Parameters.AddWithValue("sym", symp4.Text)
' myCmd.Parameters.AddWithValue("sym", symp5.Text)
' myCmd.Parameters.AddWithValue("sym", symp6.Text)
' myCmd.Parameters.AddWithValue("sym", symp7.Text)
' myCmd.Parameters.AddWithValue("sym", symp8.Text)
' myCmd.Parameters.AddWithValue("sym", symp9.Text)
' myCmd.ExecuteNonQuery()
' End Using
' MsgBox("Record Updated")
' myConn.Close()
End If
f_id| illness | symptom
1 | fever | fever
2 | fever | hot temperature
3 | fever | dizziness
4 | fever | headache
so in that case, say I chose fever in the combobox then it will display the symptoms on the 4 textboxes. If the user made changes, the Duplicate() function checks if the combobox value already have a record. If true then it will update. Say the user added another symptom, so if save button is clicked, fever will add another row with the added symptom. If false, then a new record will be added, which means new row or rows will be added in the table dependeng on the number of symptoms inputted on the textboxes. So, say 'cold' is to be added as a new record and I entered 2 symptoms, this means that I used 2 of the 10 textboxes in the form, then 2 rows will be added on the table. I already spent a day trying to make this work but failed. Please help me make this work. Any help will be greatly appreciated. Thanks in advance, Godbless
Comment