PDA

View Full Version : Syntax error (comma) in query expression in c#


m_tavakoli
07-31-2010, 08:11 AM
hi,all
my code is in c# as below , the error message is "Syntax error (comma) in query expression 'period=0,cluster=0' " at runtime and apears on line *** .

void find_record(int num, int clu)
{
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\supply_chain.accdb";
con = new OleDbConnection(conStr);
con.Open();
strsql = "SELECT ID,price,capacity FROM suppliers WHERE period=" + num.ToString() + ",cluster=" + clu.ToString();
OleDbCommand cmd = new OleDbCommand(strsql, con);
*** OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Suppliers.Add(reader["price"].ToString());
help.Add(reader["ID"].ToString());
help2.Add(reader["capacity"].ToString());
}
reader.Close();
con.Close();
}
thanks a lot.

iamscottj
08-03-2010, 06:37 AM
hi,all
my code is in c# as below , the error message is "Syntax error (comma) in query expression 'period=0,cluster=0' " at runtime and apears on line *** .

void find_record(int num, int clu)
{
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\supply_chain.accdb";
con = new OleDbConnection(conStr);
con.Open();
strsql = "SELECT ID,price,capacity FROM suppliers WHERE period=" + num.ToString() + ",cluster=" + clu.ToString();
OleDbCommand cmd = new OleDbCommand(strsql, con);
*** OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Suppliers.Add(reader["price"].ToString());
help.Add(reader["ID"].ToString());
help2.Add(reader["capacity"].ToString());
}
reader.Close();
con.Close();
}
thanks a lot.

I think the issue you are facing is because you are not using the valid syntax for the parameters. This is not allwoed in ADO.NET. If you want to add the parameters to the code, I suggest you must use the code like:
THE SELECT STATEMENT:
"SELECT ID,price,capacity FROM suppliers WHERE period=@period,cluster=@cluster"

AND THEN ADD THE FOLLOWING
SqlParameter periodParam = new SqlParameter();
param.ParameterName = "@period";
param.Value = num; //num is the variable of yours.
ADD THE SECOND PARAMETER:
SqlParameter clusterParam = new SqlParameter();
param.ParameterName = "@cluster";
param.Value = clu; //num is the variable of yours.
After that add the parameters as following
cmd.Parameters.Add(periodParam);
cmd.Parameters.Add(clusterParam);

Try this... and do let me know.