Sql异常:列名无效 [英] Sql exception: invalid column name
问题描述
Quote:System.Data.SqlClient.SqlException:'无效的列名'Mugs'。'
我不确定错误,因为我从一段代码中复制了另一个完美无缺的表格。
我使用按钮上的文本从数据库中提取信息。按钮标记为Mugs,项目描述也标记为Mugs。列名是ItemDescription。
所有变量都是从Form1公开共享的。
代码:
私人 Sub Button1_Click(发件人作为 对象,e 作为 EventArgs)句柄 Button1.Click
如果 Form1.lst_Receipt.Items .Count = 0 然后
Form1.noitemcost = 0
Form1.noitemtax = 0
Form1.noitemtotal = 0
Form1.lst_Receipt.Items.Add( UTA Planetarium)
Form1.lst_Receipt.Ite ms.Add(FormatDateTime(Today,DateFormat.LongDate))
Form1.lst_Receipt.Items.Add(FormatDateTime(Today,DateFormat.LongTime))
Form1.lst_Receipt.Items.Add( )
Form1.lst_Receipt.Items.Add( SubTotal:$ + Form1.noitemcost.ToString)
Form1.lst_Receipt.Items.Add( 税:$ + Form1.noitemtax.ToString( F2))
Form1.lst_Receipt.Items.Add( 总计:$ + Form1.noitemtotal.ToString( F2))
结束 如果
如果佛rm1.reset = 1 然后
Form1.subtotal = 0
Form1.tax = 0
Form1.total = 0
Form1.tickettax = 0
Form1.reset = 0
结束 如果
Form1.itemdesc = Button1.Text
Form1.sqlConnection1.Open()
' 我相信下一个行是它挂起的地方。
Form1.cmd = 新 SqlClient.SqlCommand( SELECT * FROM Inventory WHERE [ItemDescription] = + Form1.itemdesc,Form1.sqlConnection1)
Form1.reader = Form1.cmd.ExecuteReader
Form1.reader.Read()
Form1.itemcost = Form1.reader( 11 )
Form1.proddesc = Form1.reader( 3 )
' Form1.itemdesc = Form1.reader(2)
' itemdesc = reader(2).ToString
Form1.sqlConnection1.Close()
Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items .Count - 1 )
Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1 )
Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1 )
Form1.lst_Receipt.Items .Add(Form1.proddesc + vbTab + Form1.quantity + vbTab + Form1.itemcost.ToString( F2))
Form1.lst_Receipt.Items.Add( SubTotal:$ + Form1.subtotal.ToString( F2))
Form1.lst_Receipt.Items.Add( Tax:$ + Form1.tax.ToString( F2))
Form1.lst_Receipt.Items。添加( 总计:$ + Form1.total.ToString( F2))
Form1.TextBox1.Text = String .Empty
Form1.TextBox2.Text =
Form1.TextBox1。焦点()
关闭()
结束 Sub
这是一个标有立即窗口的框:
ClientConnectionId:dfec050c-bd3f-4cef-9e0e-ad871a9487e2
错误号码:207,状态:1,类:16< / ExceptionString>< DataItems>< ;数据>< Key> HelpLink.ProdName< / Key>< Value> Microsoft SQL Server< / Value>< / Data>< Data>< Key> HelpLink.ProdVer< / Key>< Value> 13.00 0.4001< /值及GT;< /数据与GT;<数据><钥匙及GT; HelpLink.EvtSrc< / Key与GT;<值>的MSSQLServer< /值及GT;< /数据与GT;<数据><钥匙及GT; HELPLINK。 EvtID< / Key与GT;<值> 207< /值及GT;< /数据与GT;<数据><钥匙及GT; HelpLink.BaseHelpUrl< / Key与GT;<值> HTTP://go.microsoft.com/fwlink< /Value></Data><Data><Key>HelpLink.LinkId</Key><Value>20476</Value></Data></DataItems></Exception></ TraceRecord>
希望这足够了ñ。这是我第一次在vb.net和sql写作。
如果您有任何疑问,请告诉我。
我尝试了什么:
我检查了两者的语法。使用条形码通过SQL查找时,它工作正常。与该代码的唯一区别是它需要从button1.text获取此特定形式。
+ Form1.noitemcost.ToString )
Form1.lst_Receipt.Items.Add( Tax:
+ Form1.noitemtax.ToString( F2))
Form1.lst_Receipt.Items.Add( 总计:
+ Form1.noitemtotal.ToString( F2))
结束 如果
如果 Form1。 reset = 1 然后
Form1.subtotal = 0
Form1.tax = 0
Form1.total = 0
Form1.tickettax = 0
Form1.reset = 0
结束 如果
Form1.itemdesc = Button1.Text
Form1.sqlConnection1.Open ()
' 我相信下一行是挂在哪里。
Form1.cmd = 新 SqlClient.SqlCommand( SELECT * FROM Inventory WHERE [ItemDescription] = + Form1.itemdesc,Form1.sqlConnection1)
Form1.reader = Form1.cmd。 ExecuteReader
Form1.reader.Read()
Form1.itemcost = Form1.reader( 11 )
Form1.proddesc = Form1.reader( 3 )
' Form1.itemdesc = Form1.reader(2)
' itemdesc = reader( 2).ToString
Form1.sqlConnection1.Close()
Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1 )
Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1 )
Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1 )
Form1.lst_Receipt.Items.Add (Form1.proddesc + vbTab + Form1.quantity + vbTab + Form1.itemcost.ToString( F2 ))
Form1.lst_Receipt.Items.Add( SubTotal:
Quote:System.Data.SqlClient.SqlException: 'Invalid column name 'Mugs'.'
I am not sure of the error, as I copied it from a piece of a code on another form that works perfectly.
I am pulling information from a database by using the text off of a button. The button is labeled "Mugs" and the Item Description is also labeled "Mugs". The Column Name is "ItemDescription".
All of the variables are publicly shared from Form1.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Form1.lst_Receipt.Items.Count = 0 Then Form1.noitemcost = 0 Form1.noitemtax = 0 Form1.noitemtotal = 0 Form1.lst_Receipt.Items.Add("UTA Planetarium") Form1.lst_Receipt.Items.Add(FormatDateTime(Today, DateFormat.LongDate)) Form1.lst_Receipt.Items.Add(FormatDateTime(Today, DateFormat.LongTime)) Form1.lst_Receipt.Items.Add(" ") Form1.lst_Receipt.Items.Add("SubTotal: $" + Form1.noitemcost.ToString) Form1.lst_Receipt.Items.Add("Tax: $" + Form1.noitemtax.ToString("F2")) Form1.lst_Receipt.Items.Add("Total: $" + Form1.noitemtotal.ToString("F2")) End If If Form1.reset = 1 Then Form1.subtotal = 0 Form1.tax = 0 Form1.total = 0 Form1.tickettax = 0 Form1.reset = 0 End If Form1.itemdesc = Button1.Text Form1.sqlConnection1.Open() 'I believe the next line is where it is hanging up at. Form1.cmd = New SqlClient.SqlCommand("SELECT * FROM Inventory WHERE [ItemDescription] =" + Form1.itemdesc, Form1.sqlConnection1) Form1.reader = Form1.cmd.ExecuteReader Form1.reader.Read() Form1.itemcost = Form1.reader(11) Form1.proddesc = Form1.reader(3) 'Form1.itemdesc = Form1.reader(2) 'itemdesc = reader(2).ToString Form1.sqlConnection1.Close() Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1) Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1) Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1) Form1.lst_Receipt.Items.Add(Form1.proddesc + vbTab + Form1.quantity + vbTab + Form1.itemcost.ToString("F2")) Form1.lst_Receipt.Items.Add("SubTotal: $" + Form1.subtotal.ToString("F2")) Form1.lst_Receipt.Items.Add("Tax: $" + Form1.tax.ToString("F2")) Form1.lst_Receipt.Items.Add("Total: $" + Form1.total.ToString("F2")) Form1.TextBox1.Text = String.Empty Form1.TextBox2.Text = "" Form1.TextBox1.Focus() Close() End Sub
This is in a box labeled "Immediate Window":
ClientConnectionId:dfec050c-bd3f-4cef-9e0e-ad871a9487e2 Error Number:207,State:1,Class:16</ExceptionString><DataItems><Data><Key>HelpLink.ProdName</Key><Value>Microsoft SQL Server</Value></Data><Data><Key>HelpLink.ProdVer</Key><Value>13.00.4001</Value></Data><Data><Key>HelpLink.EvtSrc</Key><Value>MSSQLServer</Value></Data><Data><Key>HelpLink.EvtID</Key><Value>207</Value></Data><Data><Key>HelpLink.BaseHelpUrl</Key><Value>http://go.microsoft.com/fwlink</Value></Data><Data><Key>HelpLink.LinkId</Key><Value>20476</Value></Data></DataItems></Exception></TraceRecord>
Hopefully this is enough to go on. This is my first time writing in vb.net and sql.
Please let me know if you have any questions.
What I have tried:
I've checked syntax on both. It works fine when using a barcode to look it up through SQL. The only difference from that code is that it needs to pull from the button1.text for this particular form.解决方案"+ Form1.noitemcost.ToString) Form1.lst_Receipt.Items.Add("Tax:
" + Form1.noitemtax.ToString("F2")) Form1.lst_Receipt.Items.Add("Total:
" + Form1.noitemtotal.ToString("F2")) End If If Form1.reset = 1 Then Form1.subtotal = 0 Form1.tax = 0 Form1.total = 0 Form1.tickettax = 0 Form1.reset = 0 End If Form1.itemdesc = Button1.Text Form1.sqlConnection1.Open() 'I believe the next line is where it is hanging up at. Form1.cmd = New SqlClient.SqlCommand("SELECT * FROM Inventory WHERE [ItemDescription] =" + Form1.itemdesc, Form1.sqlConnection1) Form1.reader = Form1.cmd.ExecuteReader Form1.reader.Read() Form1.itemcost = Form1.reader(11) Form1.proddesc = Form1.reader(3) 'Form1.itemdesc = Form1.reader(2) 'itemdesc = reader(2).ToString Form1.sqlConnection1.Close() Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1) Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1) Form1.lst_Receipt.Items.RemoveAt(Form1.lst_Receipt.Items.Count - 1) Form1.lst_Receipt.Items.Add(Form1.proddesc + vbTab + Form1.quantity + vbTab + Form1.itemcost.ToString("F2")) Form1.lst_Receipt.Items.Add("SubTotal:
这篇关于Sql异常:列名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!