CHANDRAHAS
CHANDRAHAS

Reputation: 647

date time in combobox

i am using visual stdio 2008 and sql server 2005

dim selectquery = "SELECT Purchase_master.Customer_name, Purchase_details.Item_code, Item_Master.Name,
    Purchase_details.Quantity, Purchase_details.Cost, Purchase_master.Date 
    FROM Item_Master INNER JOIN (Purchase_master INNER JOIN Purchase_details ON
    Purchase_master.Bill_id = Purchase_details.Bill_id) ON Item_Master.Item_code = Purchase_details.Item_code
    WHERE Purchase_master.Date= " + cboPDate.SelectedValue.ToString()

when this selectquery executed it gives me error

"ERROR near syntax 12"

my cboPDate is a combobox binded with my database which return's data and time in "2/18/2011 12:00:00 AM"

please help me out

Upvotes: 0

Views: 264

Answers (3)

JAiro
JAiro

Reputation: 5999

Try this:

dim selectquery = string.Format("SELECT Purchase_master.Customer_name,     Purchase_details.Item_code, Item_Master.Name,
Purchase_details.Quantity, Purchase_details.Cost, Purchase_master.Date 
FROM Item_Master INNER JOIN (Purchase_master INNER JOIN Purchase_details ON
Purchase_master.Bill_id = Purchase_details.Bill_id) ON Item_Master.Item_code = Purchase_details.Item_code
WHERE Purchase_master.Date= '{0}'", cboPDate.SelectedValue.ToString());

you have to add quots to the value. :)

Upvotes: 0

Kevin Ross
Kevin Ross

Reputation: 7215

I suspect it is treating it as a delimiter.It would be better to change it to

WHERE Purchase_master.Date=@Your_date

And then add the date as a parameter, this would prevent SQL injection attacks and also promote plan caching

Upvotes: 1

Kamal
Kamal

Reputation: 2522

You need to add quotes around your date.

Try

dim selectquery = "SELECT Purchase_master.Customer_name, Purchase_details.Item_code, Item_Master.Name,
    Purchase_details.Quantity, Purchase_details.Cost, Purchase_master.Date 
    FROM Item_Master INNER JOIN (Purchase_master INNER JOIN Purchase_details ON
    Purchase_master.Bill_id = Purchase_details.Bill_id) ON Item_Master.Item_code = Purchase_details.Item_code
    WHERE Purchase_master.Date= '" + cboPDate.SelectedValue.ToString() +"'"

Better yet, use SQL parameters.

Upvotes: 2

Related Questions