Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I am getting error in Invalid Expression Term "," in


SqlConnection sqlconn = new SqlConnection("Data Source=ARSALAN-PC;Initial Catalog=latestsensordata;Integrated Security=True");
SqlDataAdapter sa = new SqlDataAdapter();

string query = "insert into Table_1(SensorValue,Time) values(" +hexValue+, +DateTime.Now.ToString("HH:MM:ss")+")";

sa.InsertCommand = new SqlCommand(query, sqlconn);
Posted
Updated 16-Jan-13 23:40pm
v2

If you double click on the error in the IDE it will take you straight to the problem.
You have a comma after hexValue+ - You're actually missing some double quotes
 
Share this answer
 
First off, don't do it like that: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Secondly, fixing the first one will solve the second: you need some more double quotes.
C#
string query = "insert into Table_1(SensorValue,Time) values(" +hexValue+, +DateTime.Now.ToString("HH:MM:ss")+")";
Becomes
C#
string query = "insert into Table_1(SensorValue,Time) values(" +hexValue+ ", " +DateTime.Now.ToString("HH:MM:ss")+")";


But do use parametrised queries:
C#
string query = "insert into Table_1(SensorValue,Time) values(@SV, @TM)";
sa.InsertCommand = new SqlCommand(query, sqlconn);
sa.InsertCommand.Parameters.AddWithValue("@SV", hexValue);
sa.InsertCommand.Parameters.AddWithValue("@TM", DateTime.Now.ToString("HH:MM:ss"));


BTW: It is a bad idea to use the same name for a field as a datatype - it can cause problems with some queries. Change "Time" to "SpotTime" or something else which describes it better.

[edit] :doh: Copy and Paste is great - when you remember to update the variable names... "@SV" duplicate changed to "@TM" to match the SELECT statement - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
ontheline89 17-Jan-13 6:01am    
I am still getting error
Must declare the Scalar Variable at @SV
OriginalGriff 17-Jan-13 6:08am    
:doh:!
I forgot to update the variable name @SV to @TM to match the select statement when I copy and pasted the line start. Fixed now, sorry about that...
ontheline89 17-Jan-13 6:24am    
I have two columns in SQL Server
SensorValue ( int )
Time (nchar)
OriginalGriff 17-Jan-13 6:35am    
It is a bad idea to store date and / or time values as strings - it means you have to convert them to work with them. Store them in your DB as a Time datatype instead (then you can compare them a lot more easily if you need to later)
ontheline89 17-Jan-13 6:21am    
but it still giving the same error.
Must declare the Scalar Variable at @SV.
Hi.

You missed two things inside the SQL statement:
- Double quotation marks around the "," between the two "+".
- Single quotes (or other separator) surrounding the DateTime string representation.

So the code should be:
string query = "insert into Table_1(SensorValue,Time) values(" +hexValue+ ",
                    '" +DateTime.Now.ToString("HH:MM:ss") + "')";


Regards,
Daniele.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900