I'm writing a database CRUD application, and I'm stuck when trying to pass in a variable to the Database. When I pass in a literal value to the database, the application works fine, such as the code below:
myCommand.CommandText = "Select * From Customers Where CustomerID = 'ALFKI'";
However, when I try to make a variable from the text within a textbox, the application will not do anythying. It does not throw an error, it just sits idle. Here is the code i used for my variable:
string searcher = Convert.ToString(txtSearch);
...
myCommand.CommandText = "Select * From Customers Where CustomerID = " + "'" + searcher + "'";
I've tried different CustomerIDs in the textbox. I also tried re-arranging the way the single and double quotation marks are used, but to no avail. Could anyone help me with this?
Thanks
**
Thanks Jon Skeet! Your method worked perfectly. Darren, thank you for your input as well. I will be brushing up on parameterized sql statements very soon, thanks for giving me another frontier to explore.
Convert.ToStringinstead of what I'd expect to betxtSearch.Text.