Brendyll Narvas
Brendyll Narvas

Reputation: 1

Insert data into 2 tables

This is my code for enrolling students:

First I will like to store student info into tblStudentRecord and I would like to store it in another table tblSection1.

This is my code to do this:

If (MsgBox("The records you entered will be saved to the database. Do you want to proceed to the enrolment?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
      Dim cmd As New OleDb.OleDbCommand
      If Not cnn.State = ConnectionState.Open Then
           cnn.Open()
      End If
      Dim converter As New ImageConverter
      Dim iByte As Byte()
      iByte = converter.ConvertTo(picStud.Image, GetType(Byte()))
      cmd.Connection = cnn
      Try
           With cnn
           If .State <> ConnectionState.Open Then
                .Open()
           End If
           strsql = "INSERT INTO tblStudRecord(LName,FName,MName,BMonth,BDay,BYear,StudAge,StudGender,StudNationality,StudReligion,TownCity,Province,StudPNum,StudAddress,FatherName,FOccupation,FContact,FAddress,MotherName,MOccupation,MContact, MAddress, GuardianName, GContact, GAddress, StudStatus, LastSchAtt, sPlace, GrdYr, DateofReg, ForGrdYrLvl,studPic,sStatus)" & _
                     "VALUES (@LName,@FName,@MName,@BMonth,@BDay,@BYear,@StudAge,@StudGender,@StudNationality, @StudReligion , @TownCity , @Province , @StudPNum,@studAddress , @FatherName , @FOccupation , @FContact , @FAddress , @MotherName , @MOccupation, @MContact , @MAddress , @GuardianName , @GContact , @GAddress , @StudStatus , @LastSchAtt , @sPlace , @GrdYr , @DateofReg , @ForGrdYr,@picStud,'Enrolled')"

          cmd.CommandText = strsql
          cmd.Connection = cnn

          cmd.Parameters.AddWithValue("@LName", txtLName.Text) '2
          cmd.Parameters.AddWithValue("@FName", txtFName.Text) '3
          cmd.Parameters.AddWithValue("@MName", txtMName.Text) '4
          cmd.Parameters.AddWithValue("@BMonth", cboBMonth.SelectedItem) '5
          cmd.Parameters.AddWithValue("@BDay", cboBDay.SelectedItem) '6
          cmd.Parameters.AddWithValue("@BYear", cboBYear.SelectedItem) '7
          cmd.Parameters.AddWithValue("@StudAge", txtAge.Text) '8
          cmd.Parameters.AddWithValue("@StudGender", cboGender.SelectedItem) '9
          cmd.Parameters.AddWithValue("@StudNationality", txtNationality.Text) '10
          cmd.Parameters.AddWithValue("@StudReligion", txtReligion.Text) '11
          cmd.Parameters.AddWithValue("@TownCity", txtTownCity.Text) '12
          cmd.Parameters.AddWithValue("@Province", txtProvince.Text) '13
          cmd.Parameters.AddWithValue("@StudPNum", txtMobileNumber.Text) '14
          cmd.Parameters.AddWithValue("@studAddress", txtHomeAddress.Text) '15
          cmd.Parameters.AddWithValue("@FatherName", txtFatherName.Text) '16
          cmd.Parameters.AddWithValue("@FOccupation", txtFatherOccupation.Text) '17
          cmd.Parameters.AddWithValue("@FContact", txtFatherContact.Text) '18
          cmd.Parameters.AddWithValue("@FAddress", txtFatherAddress.Text) '19
          cmd.Parameters.AddWithValue("@MotherName", txtMotherName.Text) '20
          cmd.Parameters.AddWithValue("@MOccupation", txtMotherOccupation.Text) '21
          cmd.Parameters.AddWithValue("@MContact", txtMotherContact.Text) '22
          cmd.Parameters.AddWithValue("@MAddress", txtMotherAddress.Text) '23
          cmd.Parameters.AddWithValue("@GuardianName", txtGuardianName.Text) '24
          cmd.Parameters.AddWithValue("@GContact", txtGuardianContact.Text) '25
          cmd.Parameters.AddWithValue("@GAddress", txtGuardianAddress.Text) '26
          cmd.Parameters.AddWithValue("@StudStatus", cboStatus.SelectedItem) '27
          cmd.Parameters.AddWithValue("@LastSchAtt", txtLastSchoolAttended.Text) '28
          cmd.Parameters.AddWithValue("@sPlace", txtPlace.Text) '29
          cmd.Parameters.AddWithValue("@GrdYr", txtGradeYearLvl.Text) '30
          cmd.Parameters.AddWithValue("@DateofReg", txtDateofRegistration.Text) '31
          cmd.Parameters.AddWithValue("@ForGrdYr", cboYearLvl.SelectedItem) '32
          cmd.Parameters.AddWithValue("@picStud", iByte)

Upvotes: 0

Views: 62

Answers (2)

Sarjit Delivala
Sarjit Delivala

Reputation: 567

Call cmd.ExecuteNonQuery() function at the last.

Upvotes: 1

chris_techno25
chris_techno25

Reputation: 2477

If tables tblStudentRecord and tblSection1 have the same fields or columns, try this...

strsql = "INSERT INTO tblStudRecord(LName,FName,MName,BMonth,BDay,BYear,StudAge,StudGender,StudNationality,StudReligion,TownCity,Province,StudPNum,StudAddress,FatherName,FOccupation,FContact,FAddress,MotherName,MOccupation,MContact, MAddress, GuardianName, GContact, GAddress, StudStatus, LastSchAtt, sPlace, GrdYr, DateofReg, ForGrdYrLvl,studPic,sStatus)" & _
         "VALUES (@LName,@FName,@MName,@BMonth,@BDay,@BYear,@StudAge,@StudGender,@StudNationality, @StudReligion , @TownCity , @Province , @StudPNum,@studAddress , @FatherName , @FOccupation , @FContact , @FAddress , @MotherName , @MOccupation, @MContact , @MAddress , @GuardianName , @GContact , @GAddress , @StudStatus , @LastSchAtt , @sPlace , @GrdYr , @DateofReg , @ForGrdYr,@picStud,'Enrolled')" & _
         ";" & _
         "INSERT INTO tblSection1(LName,FName,MName,BMonth,BDay,BYear,StudAge,StudGender,StudNationality,StudReligion,TownCity,Province,StudPNum,StudAddress,FatherName,FOccupation,FContact,FAddress,MotherName,MOccupation,MContact, MAddress, GuardianName, GContact, GAddress, StudStatus, LastSchAtt, sPlace, GrdYr, DateofReg, ForGrdYrLvl,studPic,sStatus)" & _
         "VALUES (@LName,@FName,@MName,@BMonth,@BDay,@BYear,@StudAge,@StudGender,@StudNationality, @StudReligion , @TownCity , @Province , @StudPNum,@studAddress , @FatherName , @FOccupation , @FContact , @FAddress , @MotherName , @MOccupation, @MContact , @MAddress , @GuardianName , @GContact , @GAddress , @StudStatus , @LastSchAtt , @sPlace , @GrdYr , @DateofReg , @ForGrdYr,@picStud,'Enrolled')" & _
         ";"

Upvotes: 0

Related Questions