Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
KarinElvira
Starting Member
47 Posts |
Posted - 2010-08-23 : 04:24:07
|
I have a problem with a sql statement (MS SQL Server 2005). I've used statements like this before without any problems, but this time something is wrong.1) I want to fetch all names in the database connected to a certain street ('street' column is varchar). The street is named by the user in a textbox (string). I'm under the impression no conversion is needed between string and varchar, but maybe I'm wrong about that.string Street = TextBoxStreet.Text; SqlConnection conn = new SqlConnection(config); conn.Open(); string sql = "SELECT ID, firstName, lastName FROM Customer WHERE street=" + Street; SqlCommand comm = new SqlCommand(sql, conn); SqlDataReader dr = comm.ExecuteReader(); //Incorrect column name '[whatever's in TextBoxStreet.Text]'while (dr.Read()) { ListBoxSearchCust.Items.Add(dr[1] + " " + dr[2]); }I've tried different combinations of " and '.(Yes, parameters... I know... I just don't get how they work yet...will get there eventually ) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 04:35:57
|
make string as string sql = "SELECT ID, firstName, lastName FROM Customer WHERE street='" + Street + "'"; and try------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-23 : 05:01:25
|
| Better approach is to make use of paramtersMadhivananFailing to plan is Planning to fail |
 |
|
|
KarinElvira
Starting Member
47 Posts |
Posted - 2010-08-23 : 05:39:06
|
quote: Originally posted by visakh16 make string as string sql = "SELECT ID, firstName, lastName FROM Customer WHERE street='" + Street + "'"; and try------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Ok, that was the one I hadn't tried yet and of course it works like a charm! *embarrased*Thank you! |
 |
|
|
KarinElvira
Starting Member
47 Posts |
Posted - 2010-08-23 : 05:39:50
|
| embarrassed*I can spell, at least sometimes... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 08:45:08
|
quote: Originally posted by KarinElvira
quote: Originally posted by visakh16 make string as string sql = "SELECT ID, firstName, lastName FROM Customer WHERE street='" + Street + "'"; and try------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Ok, that was the one I hadn't tried yet and of course it works like a charm! *embarrased*Thank you!
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|