【问题标题】:Error: An exception of type 'System.Data.SqlClient.SqlException' occured in System.Data.dll but was not handled in user code错误:System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理
【发布时间】:2014-01-12 21:53:29
【问题描述】:

我是 ASP.NET 的新手,正在为销售部门构建一个小的动态网站,以便为销售竞赛注册他们的销售。

我有一个页面,在登录后,它由几个组合框/下拉列表和底部的“提交”按钮组成,我想用所有选定的数据触发数据库中的新记录。一切似乎都很好,但最终出现以下错误消息:

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 中选择

【问题讨论】:

  • 您正在使用您尝试插入的相同变量来告诉 sql 要插入哪些列,这与列名不同。
  • 您好,谢谢。我摆脱了 Invalid Columns names 错误,但仍然缺少一件事。猜测它与您关于 AddWithValue 的答案的第二部分有关。我阅读了链接并在这里尝试了不同的东西,但似乎没有任何效果。我真的不明白那部分:(
  • 'System.Data.dll 中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理附加信息:必须声明标量变量“@KunderID”。
  • KunderID 数据类型是什么? MBExec 方法是什么样的?使用该信息编辑您的问题。
  • 更新答案........

标签: asp.net vb.net


【解决方案1】:

试试这个方法:

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

使用参数化查询添加值:

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

AddWithValue

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

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

    • 嗨,DonA 和 Austine,非常感谢。 Austines 的回答在金钱上是正确的,并且成功了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多