错误:System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码中处理

时间:2014-01-12 21:53:30

标签: asp.net vb.net

我是ASP.NET的新手,为销售部门建立一个小型动态网站,以便为销售竞争注册销售。

我有一个页面,一个登录后,由几个组合框/下拉列表组成,并在按钮上有一个'SUBMIT'按钮,我想在数据库中触发包含所有选定数据的新记录。一切似乎都很好,但最终会出现以下错误消息:

System.Data.dll中发生了'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码中处理 附加信息:无效的列名称“KunderID”。 列名称“KundeTypeID”无效。 列名称“MachineModellID”无效。 列名称'AntallID'无效。 列名称“BrukerID”无效。

它指向DBConnection.vb文件中的以下部分(以MBExec =开头的行):

Public Shared Function MBExec(ByVal SQL As String) As String
    Dim cmd As New SqlCommand(SQL, MBConn)
    MBExec = Convert.ToString(cmd.ExecuteScalar())
    cmd.Connection.Close()
End Function    

在源代码和相关页面上,相关部分如下(从MBExec开始的底线),我看不到列名是错误的:

 Protected Sub RegisterSale(sender As Object, e As EventArgs)

    Dim KundeNavn As DropDownList = DropDownListKundeNavn
    Dim TypeKunde As DropDownList = DropDownListTypeKunde
    Dim MachineModell As DropDownList = DropDownListMachineModell
    Dim Antall As DropDownList = DropDownListAntall
    Dim Bruker As DropDownList = DropDownListBruker


    If KundeNavn.SelectedItem.Text = "Velg" Then
        Dim msg = "Select or add a new customer"
        Dim msgTittle = "Missing Customer Name"
        MsgBox(msg, MsgBoxStyle.Critical, msgTittle)
        Exit Sub
    Else
        Dim msg1 = "Are you sure to continue?"
        Dim title = "Confirm Sale Registration"
        Dim style = MsgBoxStyle.YesNo
        Dim responce = MsgBox(msg1, style, title)
        If responce = MsgBoxResult.Yes Then
            Dim msg = "Thank you for your efforts, you are closer to becoming a sales champion!"
            Dim msgTittle = "Your Sale has been recorded"
            MsgBox(msg, MsgBoxStyle.Information, msgTittle)

            'Varibles to hold the DataValueField from the dropboxes
            Dim KundeID As Integer
            Dim TypeKundeID As Integer
            Dim MachineModellID As Integer
            Dim AntallID As Integer
            Dim BrukerID As Integer

            'Converts the DataValueField to an Integer 

            KundeID = Convert.ToInt32(KundeNavn.SelectedValue.ToString())
            TypeKundeID = Convert.ToInt32(TypeKunde.SelectedValue.ToString())
            MachineModellID = Convert.ToInt32(MachineModell.SelectedValue.ToString())
            AntallID = Convert.ToInt32(Antall.SelectedValue.ToString())
            BrukerID = Convert.ToInt32(Bruker.SelectedValue.ToString())

            MBExec("INSERT INTO KyoceraSalgReg(KunderID, KundeTypeID, MachineModellID, AntallID, BrukerID) Values (KunderID, KundeTypeID, MachineModellID, AntallID, BrukerID)")

            Exit Sub
        Else
            Exit Sub
        End If
    End If

End Sub    

如果有人能帮助我朝着正确的方向前进,我将非常感激。如果我理解正确,不知何时列名称无法识别,我只是不明白为什么。

干杯:)

更新1:

MBExec看起来像这样:

    Public Shared Function MBExec(ByVal SQL As String) As String
    Dim cmd As New SqlCommand(SQL, MBConn)
    MBExec = Convert.ToString(cmd.ExecuteScalar())
    cmd.Connection.Close()
End Function    

KunderID数据类型是字符串,选自DropDownList

2 个答案:

答案 0 :(得分:2)

尝试这种方法:

MBExec("INSERT INTO KyoceraSalgReg(KunderID, KundeTypeID, MachineModellID, AntallID, BrukerID) Values (@KunderID, @KundeTypeID, @MachineModellID, @AntallID, @BrukerID)")

使用参数化查询添加值:

cmd.Parameter.AddWithValue("@KunderID", KunderID)

AddWithValue

您可能需要创建SqlParameter的单独实例 - Example

答案 1 :(得分:0)

Protected Sub RegisterSale(sender As Object, e As EventArgs)

    Dim KundeNavn As DropDownList = DropDownListKundeNavn
    Dim TypeKunde As DropDownList = DropDownListTypeKunde
    Dim MachineModell As DropDownList = DropDownListMachineModell
    Dim Antall As DropDownList = DropDownListAntall
    Dim Bruker As DropDownList = DropDownListBruker

    'Varibles to hold the DataValueField from the dropboxes
    Dim KunderID As Integer = Convert.ToInt32(KundeNavn.SelectedValue.ToString())
    Dim TypeKundeID As Integer = Convert.ToInt32(TypeKunde.SelectedValue.ToString())
    Dim MachineModellID As Integer = Convert.ToInt32(MachineModell.SelectedValue.ToString())
    Dim AntallID As Integer = Convert.ToInt32(Antall.SelectedValue.ToString())
    Dim BrukerID As Integer = Convert.ToInt32(Bruker.SelectedValue.ToString())


    'Sets the Selected values from dropdownlist
    Dim ParamKunderID = New SqlParameter()
    ParamKunderID.ParameterName = "@KunderID"
    ParamKunderID.Value = KunderID

    Dim ParamTypeID = New SqlParameter
    ParamTypeID.ParameterName = "@KundeTypeID"
    ParamTypeID.Value = TypeKundeID

    Dim ParamMachineModellID = New SqlParameter()
    ParamMachineModellID.ParameterName = "@MachineModellID"
    ParamMachineModellID.Value = MachineModellID

    Dim ParamAntallID = New SqlParameter
    ParamAntallID.ParameterName = "@AntallID"
    ParamAntallID.Value = AntallID

    Dim ParamBrukerID = New SqlParameter
    ParamBrukerID.ParameterName = "@BrukerID"
    ParamBrukerID.Value = BrukerID

    If KundeNavn.SelectedItem.Text = "Velg" Then
        Dim msg = "Velg eller legge til en ny kunde"
        Dim msgTittle = "Mangler Kundenavn"
        MsgBox(msg, MsgBoxStyle.Critical, msgTittle)
        Exit Sub
    Else
        Dim msg1 = "Er du sikker på at du vil fortsette?"
        Dim title = "Bekrefte salg registrering"
        Dim style = MsgBoxStyle.YesNo
        Dim responce = MsgBox(msg1, style, title)

        If responce = MsgBoxResult.Yes Then
            MBExec("INSERT INTO KyoceraSalgReg(KunderID, KundeTypeID, MachineModellID, AntallID, BrukerID)" & " Values " & "(" & KunderID & "," & TypeKundeID & "," & MachineModellID & "," & AntallID & "," & BrukerID & ")")
            Dim msg = "Takk for din innsats, du er nærmere å bli et Salg Mester!"
            Dim msgtittle = "Din salget er registrert"
            MsgBox(msg, MsgBoxStyle.Information, msgtittle)

        End If


    End If
End Sub