【发布时间】: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方法是什么样的?使用该信息编辑您的问题。 -
更新答案........