2016-08-14 48 views
-3

我正在使用Microsoft Visual Studio 2010 Express,並試圖使用VB.NET製作註冊表單。這是我的代碼到目前爲止:查詢值和目標字段的數量不相同。錯誤在vb.net

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 
    Dim cardStr As String 
    Dim formStr As String 
    Dim birthStr As String 
    Dim goodmoralStr As String 

    If cbcard.Checked Then 
     cardStr = "OK" 
    Else 
     cardStr = "" 
    End If 

    If cbform.Checked Then 
     formStr = "OK" 
    Else 
     formStr = "" 
    End If 

    If cbbirth.Checked Then 
     birthStr = "OK" 
    Else 
     birthStr = "" 
    End If 

    If cbgoodmoral.Checked Then 
     goodmoralStr = "OK" 
    Else 
     goodmoralStr = "" 
    End If 

    Dim cmd As New OleDbCommand 
    Dim conn As New OleDbConnection(conStr) 
    conn.Open() 
    cmd.Connection = conn 
    cmd.CommandType = CommandType.Text 
    cmd.CommandText = "insert into tblEnroll(StudID,StudLevel,StudFName,StudMName,StudLName,StudGender,StudBirthday,StudNationality,StudContact,StudPOB,StudCitizenship,StudReligion,MomName,MomContact,MomOccupation,DadName,DadContact,DadOccupation,PrevSchool,Card,F137,BirthCertificate) values ('" & txtID.Text & "','" & lbGrade.Text & "','" & txtFName.Text & "','" & txtMName.Text & "','" & txtLName.Text & "','" & lbGender.Text & "','" & dtpBirthDate.Text & "','" & txtNationality.Text & "','" & txtStudContact.Text & "','" & txtPOB.Text & "','" & txtCitizen.Text & "','" & txtReligion.Text & "','" & txtMom.Text & "','" & txtMomContact.Text & "','" & txtMomOccupation.Text & "','" & txtDad.Text & "','" & txtDadContact.Text & "','" & txtDadOccupation.Text & "','" & txtPrevSchool.Text & "','" & cardStr & "','" & formStr & "','" & birthStr & "','" & goodmoralStr & "')" 
    cmd.ExecuteNonQuery() 
    conn.Close() 
    MessageBox.Show("Student Successfully Enrolled!")` 

什麼可能是解決方案在這裏?

回答

1

您的INSERT語句值中有一列太多。值goodmoralStr沒有要插入的相應列。作爲一個微不足道,你真的應該在你的代碼中使用參數化的SQL來避免SQL注入的問題。

相關問題