1. Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as:

    All that and more! Registration is quick, simple and absolutely free. Join our community today!

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

Discussion in 'Coding, Programming' started by kje999, Jan 24, 2016.

  1. 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()
     
Tags / Keywords: