Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to store query result in string variable.I am using following code
VB
Dim str As String = ("SELECT Item_name FROM Item_Master where Item_no='" & cmbItemName.SelectedValue & "'")

System throws an error: Operator '&' is not defined for string "SELECT * FROM Item_Master where " and type 'DataRowView'.(Invalid cast exception)

Please suggest me right query format.

Thanks in Advance
Posted
Updated 3-Dec-12 5:39am
v2
Comments
ZurdoDev 3-Dec-12 11:40am    
You sure that is the error you are getting on that line? It sounds like you are trying to concatenate a string and a DataRowView together.
CPallini 3-Dec-12 12:16pm    
How is declared cmbItemName (ryanb31 observation is right)?

Either:
1) Try this
VB
Dim str As String = ("SELECT Item_name FROM Item_Master where Item_no='" & cmbItemName.SelectedValue.ToString() & "'")
Or
2) - And preferably - use parametrised queries instead. There isn't that much risk of SQL Injection from a combobox, but parametrized queries gets rid of it.
 
Share this answer
 
Comments
Yogi ,Pune 3-Dec-12 12:31pm    
Thanks
OriginalGriff 3-Dec-12 13:58pm    
You're welcome!
RaisKazi 3-Dec-12 14:52pm    
Clean!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900