I ma beginning Developer, I m still studying and currently im stuck on a error. I'm learning a new technic on how to make proper sqlconnections
.. but seems as i cannot find out whats wrong.
This is a simple program where, the user is scanning a bar code to fill a textbox. And depending on that value. Rest of the textboxes will be filled in.
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code .Additional information: Incorrect syntax near 'ProductBarcode'.
This is my code right now:
protected void PBarcodeTxt_TextChanged(object sender, EventArgs e)
{
string cmd = "SELECT ProductNaam, ProductPrijs, ProductOmschrijving, ProductBarcode" +
"FROM Producten" +
"WHERE ProductBarcode LIKE '@Barcode'";
using (SqlConnection connection = new SqlConnection(cstring))
{
SqlCommand command = new SqlCommand(cmd, connection);
command.Parameters.Add("@Barcode", SqlDbType.VarChar, 50).Value = PBarcodeTxt.Text;
try
{
connection.Open();
SqlDataReader rdr = command.ExecuteReader();
rdr.Read();
PNaamTxt.Text = (rdr["ProductNaam"].ToString());
POmschrijvingTxt.Text = (rdr["ProductOmschrijving"].ToString());
PPrijsTxt.Text = (rdr["ProductPrijs"].ToString());
// PBarcodeTxt.Text = (rdr["ProductBarcode"].ToString());
}
finally
{
connection.Close();
}
Thank you for your time and effort.
In your command you are missing a number of spaces, try the following (notice the spaces before FROM and WHERE:
string cmd = "SELECT ProductNaam, ProductPrijs, ProductOmschrijving, ProductBarcode" +
" FROM Producten" +
" WHERE ProductBarcode LIKE '@Barcode'";
When you concatenate strings in this way you need to explicitly enter new lines and spaces, what you had before when concatenated would actually be:
string cmd = "SELECT ProductNaam, ProductPrijs, ProductOmschrijving, ProductBarcodeFROM ProductenWHERE ProductBarcode LIKE '@Barcode'";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.