Search code examples
c#compact-frameworkwindows-cepushdown-automaton

"A quotation mark delimiter is missing from the end of the query." error in WinCE PDA application


In my WinCE PDA application, I am doing comparing the barcode value I scanned with the one in my database to generate a table.

I build the query like this:

for (int i = 0; i < listBox2.Items.Count; i++)
{
    if (i == 0)
    {
        sb.Append("Select * from ToolsBar where BarcodeValue in (");
    }

    sb.Append("'" + listBox2.Items[i] + "',");

}

sb.Length = sb.Length - 1;
sb.Append(")");

And use it here:

cmd.CommandText = sb.ToString();
cmd.CommandType = CommandType.Text;

cmd.Connection = con;
con.Open();

reader = cmd.ExecuteReader(); // this is where the error came out  "A quotation mark delimiter is missing from the end of the query." 
reader.Close();
SqlCeDataAdapter ad = new SqlCeDataAdapter(sb.ToString(), con);
DataSet ds = new DataSet();
ad.Fill(ds);
dataGrid2.DataSource = ds.Tables[0];

con.Close();
sb.Length = 0;

Solution

  • Alternate loop to build your query which removes the need to alter the string length:

    for (int i = 0; i < listBox2.Items.Count; i++)
    {
        if (i == 0)
        {
            sb.Append("Select * from ToolsBar where BarcodeValue in (");
            sb.Append("'" + listBox2.Items[i] + "'");
        }
        else
        {
            sb.Append(",'" + listBox2.Items[i] + "'");
        }
    }
    
    sb.Append(")");