每次单击按钮以保存访问数据时出现溢出错误

时间:2012-05-30 19:11:02

标签: vb.net-2010

任何人都可以帮助我 我有一个表单,我通过它在我的ms访问数据库中保存日期。 有时我单击保存按钮数据保存正确但有时它给我错误“溢出”'ExecuteNonQuery'未声明。由于其保护级别,它可能无法访问。 我正在使用此代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If MsgBox("Are you sure you want to Add Data?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then
        Dim OleDBC As New OleDbCommand
        With OleDBC

            .Connection = conn
            .CommandText = "Insert Into tblmaritlistBA_I (form_number,name_of_candidate,fathers_name,mothers_name,category,minority,date_of_birth,gender,mobile,address,board,passed_year,intermediate_marks_obtained,intermediate_total_marks,percentage,normalization_factor,total_percentage)  VALUES ('" & txtformnumber.Text & "','" & txtstuname.Text & "','" & txtfathname.Text & "','" & txtmothname.Text & "','" & cmbcategory.Text & "','" & cmbminority.Text & "','" & dobPicker1.Text & "','" & cmbgender.Text & "','" & txtmobile.Text & "','" & txtaddress.Text & "','" & cmbboard.Text & "','" & cmbpassedyear.Text & "','" & txtintermarks.Text & "','" & txtintertotalmarks.Text & "','" & txtpercentage.Text & "','" & Lblnormalization.Text & "','" & txtpercentageafterN.Text & "')"
            .ExecuteNonQuery()
        End With
        MsgBox("Data Added!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "SUCCESS")
        Me.Hide()

        Call initgrid()
    End If
End Sub 

请帮助我(“_”)

1 个答案:

答案 0 :(得分:1)

根据您的代码,您从未打开数据库连接。

您将conn设置为Command对象的连接,但您从未先实例化它,也不打开它。

请参阅下面的代码。创建Connection对象(将您自己的连接字符串放入),然后打开它。

最后必须在Finally块中关闭该连接,以便即使出现错误也将其关闭

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

    Dim conn As OleDbConnection
    Try
        If MsgBox("Are you sure you want to Add Data?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then

            conn = New OleDbConnection("YOUR_CONNECTIONSTRING_HERE")
            conn.Open()

            Dim OleDBC As New OleDbCommand

            With OleDBC
                .Connection = conn
                .CommandText = "Insert Into tblmaritlistBA_I (form_number,name_of_candidate,fathers_name,mothers_name,category,minority,date_of_birth,gender,mobile,address,board,passed_year,intermediate_marks_obtained,intermediate_total_marks,percentage,normalization_factor,total_percentage)  VALUES ('" & txtformnumber.Text & "','" & txtstuname.Text & "','" & txtfathname.Text & "','" & txtmothname.Text & "','" & cmbcategory.Text & "','" & cmbminority.Text & "','" & dobPicker1.Text & "','" & cmbgender.Text & "','" & txtmobile.Text & "','" & txtaddress.Text & "','" & cmbboard.Text & "','" & cmbpassedyear.Text & "','" & txtintermarks.Text & "','" & txtintertotalmarks.Text & "','" & txtpercentage.Text & "','" & Lblnormalization.Text & "','" & txtpercentageafterN.Text & "')"
                .ExecuteNonQuery()
            End With

            MsgBox("Data Added!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "SUCCESS")
            Me.Hide()

            Call initgrid()
        End If

    Catch ex As Exception
        MsgBox("Error : " & ex.ToString)
    Finally
        If (conn.state and ConnectionState.Open) <>0 Then 
            conn.Close 
        End If
    End Try

End Sub