What's new

Closed (I Need Help Here) How to save a Data in mysql using two tables in Visual Studio and How to debug my

Status
Not open for further replies.

kje999

Forum Veteran
Elite
Joined
May 21, 2013
Posts
1,977
Reaction
617
Points
631
I Need Help, i have my Codes here, the first table(tblreg) that data to be inserted, I used only textbox and combobox to retrieve to save on my First table database. The second table(tblsisterbrother) is I used DataGridview to insert the data on my Second table database. My Problem is when I click save, all the data are saved on my database in correct tables, but it has an error says: Object reference not set to an instance of an object. And my if-else statement in result does not displaying the appropriate Messagebox, or it does not display after the error. Here is my Code and I hope for your response guys:

Private Sub cmdRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRegister.Click

con.ConnectionString = ("server=127.0.0.1;user id=root;password=12345;database=dbsis3bkenth") Try con.Open()

With cmd
.Connection = con
.CommandText = "INSERT INTO tblreg (IDNo, LName, FName, MI, NName, Course, Yr, Div1, Gender, Age, Hght, Wght, DOB, POB, Religion, Address, Tel, AddWSudying, Scholarship, NFather, OccFather, AIncome, AgencyFather, NMother, OccMother, AgencyMother, PNEmergency, PNEAddress, Relation, PNETel, Hobbies, WEmploy, Elem, YTElem, Secondary, YTSecondary, College, YTCollege) " & _
"VALUES ('" & vcbIDNo & "','" & vtxtLName & "','" & vtxtFName & "','" & vtxtMI & "','" & vcbNName & "','" & vcbCourse & "','" & vcbYr & "','" & vcbDiv & "','" & vcbGender & "','" & vtxtAge & "','" & vtxtHght & "','" & vtxtWght & "','" & vdtpDOB & "','" & vtxtPOB & "','" & vcbReligion & "','" & vtxtAddress & "','" & vtxtTel & "','" & vtxtAddWSudying & "','" & vcbScholarship & "','" & vtxtNFather & "','" & vtxtOccFather & "','" & vtxtAIncome & "','" & vtxtAgencyFather & "','" & vtxtNMother & "','" & vtxtOccMother & "','" & vtxtAgencyMother & "','" & vtxtPNEmergency & "','" & vtxtPNEAddress & "','" & vtxtRelation & "','" & vtxtPNETel & "','" & vtxtHobbies & "','" & vtxtWEmploy & "','" & vtxtElem & "','" & vtxtYTElem & "','" & vtxtSecondary & "','" & vtxtYTSecondary & "','" & vtxtCollege & "','" & vtxtYTCollege & "');"
result = cmd.ExecuteNonQuery

For i As Integer = 0 To DataGridView1.Rows.Count
.Connection = con
.CommandText = "INSERT INTO tblsisterbrother (LName, FName, MI,IDNoBrodSis) " & _
"VALUES ('" & DataGridView1.Rows(i).Cells("LN").Value.ToString & "', '" & DataGridView1.Rows(i).Cells("FN").Value.ToString & "', '" & DataGridView1.Rows(i).Cells("MI").Value.ToString & "','" & vcbIDNo & "');"
result = cmd.ExecuteNonQuery
Next

If result = 0 Then
MsgBox("Data has been Inserted!")
Else
MsgBox("Successfully saved!")

End If
End With

Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
 
Dear kje999,

Since 2 years have passed since the last reply in this thread, I am locking it to prevent necroposting. Feel free to start a new thread or contact any forum staff if you want this to be reopened.

Thread closed.
 
Status
Not open for further replies.
Back
Top