Tuesday, March 13, 2012

Setting the WHERE CLAUSE in my querystring

Good day,

Im using a querstring in the code behind page and it works just fine if I dont insert a where clause. The problem is that I need the where clause to grab from a textbox on the page.

Im using c# my code looks like this.

string queryString ="SELECT date, SUM(calories) AS cal,profile FROM dbo.meal_history GROUP BY date,profile ;"; <----I need the where clause to say where profile=texttbox_username.text ----

Can someone please help!!

Thanks!!

Karl

Try the below code

string queryString ="SELECT date, SUM(calories) AS cal,profile FROM dbo.meal_history WHERE profile ='" + texttbox_username.text + "'" + "GROUP BY date,profile ;";

HC


HC, you ARE THE MAN!! Thanks bud, I've been trying to get this all day long!

Props and many beers your way!! Have a good one!

Karl


I would suggest you use sql parameters for your sql statement:

string queryString ="SELECT date, SUM(calories) AS cal,profile " & _"FROM dbo.meal_history WHERE profile = @.userName " & _"GROUP BY date,profile ;";'And thendim sqlCmd asNew SqlCommand(queryString, objconnection)sqlCmd.Parameters.Add(New SqlParameter("@.userName", _textbox_username.text))'And now use a datareader to get the data

You can also do that if you use a data adapter to get your queryresults.


Another question HC. How can I format my "date" field to display mm/dd/yyyy ?

Thanks again, you're a life saver.

Karl


you can use convert(varchar, <datefield>, 101) to display mm/dd/yyyy format date in your query. I presume the database used is sql server

0 comments:

Post a Comment