473,385 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Strange Error Message

When I run the following UpdateCommand event I get the error: "Line 1:
Incorrect syntax near '?'." and the highlighted line is
"updateCommand.ExecuteNonQuery()". I've been troubleshooting this for 2
hours and have no clue what the problem is. Could someone please help me
understand what is going wrong? Thanks.

Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.UpdateCommand
'Retrieve the values from the control
Dim PKColumn As String
PKColumn = (CType(e.Item.FindControl("HL99"), HyperLink)).Text
Dim SpecieID As Integer
SpecieID = Integer.Parse(CType(e.Item.Cells(3).Controls(1),
DropDownList).SelectedItem.Value)
Dim updateCommand As New SqlCommand("UPDATE tblLogs SET SpecieID = ?
WHERE (PKColumn = ?)", SqlConnection1)
updateCommand.Parameters.Add("@SpecieID", SqlDbType.VarChar).Value
= SpecieID
updateCommand.Parameters.Add("@PKColumn", SqlDbType.VarChar).Value =
PKColumn
updateCommand.Connection.Open()
updateCommand.ExecuteNonQuery()
'Routine tasks...
DataGrid1.EditItemIndex = -1
Bind()
End Sub
Nov 22 '05 #1
3 1398
=?Utf-8?B?TXJNaWtl?= <Mr****@discussions.microsoft.com> wrote in
news:D0**********************************@microsof t.com:
When I run the following UpdateCommand event I get the error:
"Line 1: Incorrect syntax near '?'." and the highlighted line is
"updateCommand.ExecuteNonQuery()". I've been troubleshooting
this for 2 hours and have no clue what the problem is. Could
someone please help me understand what is going wrong? Thanks.

Private Sub DataGrid1_UpdateCommand(ByVal source As Object,
ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.UpdateCommand
'Retrieve the values from the control
Dim PKColumn As String
PKColumn = (CType(e.Item.FindControl("HL99"),
HyperLink)).Text Dim SpecieID As Integer
SpecieID =
Integer.Parse(CType(e.Item.Cells(3).Controls(1),
DropDownList).SelectedItem.Value)
Dim updateCommand As New SqlCommand("UPDATE tblLogs SET
SpecieID = ?
WHERE (PKColumn = ?)", SqlConnection1)
updateCommand.Parameters.Add("@SpecieID",
SqlDbType.VarChar).Value
= SpecieID
updateCommand.Parameters.Add("@PKColumn",
SqlDbType.VarChar).Value =
PKColumn
updateCommand.Connection.Open()
updateCommand.ExecuteNonQuery()
'Routine tasks...
DataGrid1.EditItemIndex = -1
Bind()
End Sub


Change the SQL to use named parameters instead of positional
parameters:

UPDATE tblLogs SET SpecieID = @SpecieID WHERE (PKColumn = @PKColumn)
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 22 '05 #2
Thank you Chris. However, after making this update the error is now:
"Invalid column name 'PKColumn'." My full code is below. Thanks much for
your help.

'UPDATE command:
Dim updateCommand As New SqlCommand("UPDATE tblLogs SET SpecieID =
@SpecieID WHERE (PKColumn = @PKColumn)", SqlConnection1)
updateCommand.Parameters.Add("@SpecieID",
SqlDbType.VarChar).Value = SpecieID
updateCommand.Parameters.Add("@PKColumn", SqlDbType.VarChar).Value =
PKColumn
updateCommand.Connection.Open()
updateCommand.ExecuteNonQuery()
DataGrid1.EditItemIndex = -1
Bind()
"Chris R. Timmons" wrote:
=?Utf-8?B?TXJNaWtl?= <Mr****@discussions.microsoft.com> wrote in
news:D0**********************************@microsof t.com:
When I run the following UpdateCommand event I get the error:
"Line 1: Incorrect syntax near '?'." and the highlighted line is
"updateCommand.ExecuteNonQuery()". I've been troubleshooting
this for 2 hours and have no clue what the problem is. Could
someone please help me understand what is going wrong? Thanks.

Private Sub DataGrid1_UpdateCommand(ByVal source As Object,
ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.UpdateCommand
'Retrieve the values from the control
Dim PKColumn As String
PKColumn = (CType(e.Item.FindControl("HL99"),
HyperLink)).Text Dim SpecieID As Integer
SpecieID =
Integer.Parse(CType(e.Item.Cells(3).Controls(1),
DropDownList).SelectedItem.Value)
Dim updateCommand As New SqlCommand("UPDATE tblLogs SET
SpecieID = ?
WHERE (PKColumn = ?)", SqlConnection1)
updateCommand.Parameters.Add("@SpecieID",
SqlDbType.VarChar).Value
= SpecieID
updateCommand.Parameters.Add("@PKColumn",
SqlDbType.VarChar).Value =
PKColumn
updateCommand.Connection.Open()
updateCommand.ExecuteNonQuery()
'Routine tasks...
DataGrid1.EditItemIndex = -1
Bind()
End Sub


Change the SQL to use named parameters instead of positional
parameters:

UPDATE tblLogs SET SpecieID = @SpecieID WHERE (PKColumn = @PKColumn)
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 22 '05 #3
=?Utf-8?B?TXJNaWtl?= <Mr****@discussions.microsoft.com> wrote in
news:FF**********************************@microsof t.com:
Thank you Chris. However, after making this update the error is
now: "Invalid column name 'PKColumn'." My full code is below.
Thanks much for your help.

'UPDATE command:
Dim updateCommand As New SqlCommand("UPDATE tblLogs SET
SpecieID =
@SpecieID WHERE (PKColumn = @PKColumn)", SqlConnection1)
updateCommand.Parameters.Add("@SpecieID",
SqlDbType.VarChar).Value = SpecieID
updateCommand.Parameters.Add("@PKColumn",
SqlDbType.VarChar).Value =
PKColumn
updateCommand.Connection.Open()
updateCommand.ExecuteNonQuery()
DataGrid1.EditItemIndex = -1
Bind()


Is the PKColumn column in tblLogs a VARCHAR or some numeric type?
I'm guessing it's an INT, and that's why you're getting that error
message.

The statement

updateCommand.Parameters.Add("@PKColumn",
SqlDbType.VarChar).Value = PKColumn

is treating the value in PKColumn as a VARCHAR, so the generated SQL
statement looks something like this:

UPDATE tblLogs SET SpecieID = "999" WHERE (PKColumn = "42")

SQL Server doesn't do type coercion in this case, so it thinks "42"
is a literal column name which, of course, doesn't exist in tblLogs.

The solution is to change the SqlDbType in the Parameters.Add
statement to reflect the column type it is responsible for. If
PKColumn is an INT, then SqlDbType.Int should be used. Same advice
for the SpecieID column.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sam | last post by:
Guys facing a strange problem any clue would really rescue me.. i am using a ASP application with following things Server : Intel Xeon (TM) CPU 2GHz, 2GB RAM, 136GB HDD OS : Windows 2000...
2
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
2
by: Kris Vanherck | last post by:
yesterday i started getting this strange error when i try to run my asp.net project: Compiler Error Message: CS0006: Metadata file 'c:\winnt\microsoft.net\framework\v1.1.4322\temporary asp.net...
5
by: Nathan Sokalski | last post by:
When I view my index.aspx page any time after the first time, I recieve the following error: System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize)...
5
by: Shapper | last post by:
Hello, I just upload my web site to my hosting server and when I access it I always get an error: "Redicterion limit for this URL exceeded. Unable to load the requested page" Does anyone...
11
by: Mike C# | last post by:
Hi all, I keep getting a strange error and can't pin it down. The message is: This application has requested the Runtime to terminate it in an unusual way. Please contact the application's...
3
by: Shelly | last post by:
I am encountering two strange problems. First one: I get a "server misconfiguration error", but only sometimes. It occurs on the first screen that accesses the database on a submit. This error...
8
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am migrating my C++ COM server to managed code (C# COM server). I am using the same client to use the same COM class in COM server. The C++ version COM server works properly,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.