Click here to Skip to main content
12,356,154 members (65,395 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
I want to store query result in string variable.I am using following code
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 3-Dec-12 5:35am
Edited 3-Dec-12 5:39am
ryanb31 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)?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

1) Try this
Dim str As String = ("SELECT Item_name FROM Item_Master where Item_no='" & cmbItemName.SelectedValue.ToString() & "'")
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.
Yogi ,Pune 3-Dec-12 12:31pm
OriginalGriff 3-Dec-12 13:58pm
You're welcome!
RaisKazi 3-Dec-12 14:52pm

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100