Operator '&' is not defined for string

I have a really stupid question, but why is this SELECT not working with my cmbClient.SelectedValue?   I've done this 100 times, and now getting an error saying "Operator '&' is not defined for string "SELECT ProjectID, UserID, Name F" and type 'DataRowView'."
 
        Dim sConn As String = strConnectionString
        Dim myConn As New SqlConnection(sConn)
        Dim sSQL As String = "SELECT ProjectID, UserID, Name FROM  Ryhal_Tickets_Projects WHERE UserID = '" & cmbClient.SelectedValue & "'"
        Dim myCommand As New SqlCommand(sSQL, myConn)
        myCommand.CommandText = sSQL
        Dim dr As SqlDataReader
        Dim dt As New DataTable

        dt.Columns.Add("Name")
        dt.Columns.Add("ProjectID")

        myConn.Open()
        dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
        Do While dr.Read
            dt.Rows.Add(dr("Name"), dr("ProjectID"))
        Loop
        dr.Close()
        myCommand.Dispose()
        myConn.Close()

        cmbProject.DataSource = dt
        cmbProject.DisplayMember = "Name"
        cmbProject.ValueMember = "ProjectID"


LVL 2
chrisryhalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
can you try: cmbClient.SelectedValue.tostring
chrisryhalAuthor Commented:
Yeah I did try that and I get a "Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type int." on my WHILE statement.
Jorge PaulinoIT Pro/DeveloperCommented:
Try this way instead when you fill the combobox
cmbProject.DisplayMember = "DisplayMember "
cmbProject.ValueMember = "ValueMember "
cmbProject.DataSource = dt

Open in new window

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

chrisryhalAuthor Commented:
All you did was move the properties around, and it still did not work no
Éric MoreauSenior .Net ConsultantCommented:
how are you binding or feeding your combo?
Jorge PaulinoIT Pro/DeveloperCommented:
Show us the code that you use to bind the combobox.
chrisryhalAuthor Commented:
Here is the cmbProjects combo

        Dim sConn As String = strConnectionString
        Dim myConn As New SqlConnection(sConn)
        Dim sSQL As String = "SELECT UserProfile.PropertyDefinitionID, UserProfile.PropertyValue, Users.UserID AS UserID, Users.DisplayName AS DisplayName, Users.FirstName, Users.LastName FROM Users INNER JOIN UserProfile ON Users.UserID = UserProfile.UserID WHERE (UserProfile.PropertyDefinitionID = 41)"
        Dim myCommand As New SqlCommand(sSQL, myConn)
        myCommand.CommandText = sSQL
        Dim dr As SqlDataReader
        Dim dt As New DataTable

        dt.Columns.Add("DisplayName")
        dt.Columns.Add("UserID")

        myConn.Open()
        dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
        Do While dr.Read
            dt.Rows.Add(dr("DisplayName"), dr("UserID"))
        Loop
        dr.Close()
        myCommand.Dispose()
        myConn.Close()

        cmbClient.DataSource = dt
        cmbClient.DisplayMember = "DisplayName"
        cmbClient.ValueMember = "UserID"
Jorge PaulinoIT Pro/DeveloperCommented:
Try like this instead:
    Public Sub fillComboBox(ByVal cbo As ComboBox, ByVal query As String)
        Try

            Using da As New SqlDataAdapter(query, strConnectionString)
                da.SelectCommand.CommandType = CommandType.Text
                Using ds As New DataSet
                    da.Fill(ds)

                    With cbo
                        .DropDownStyle = ComboBoxStyle.DropDownList
                        .DisplayMember = ds.Tables(0).Columns(1).ToString
                        .ValueMember = ds.Tables(0).Columns(0).ToString
                        .DataSource = ds.Tables(0).DefaultView
                    End With

                    cbo.SelectedIndex = -1
                    cbo.Enabled = True

                End Using
            End Using

        Catch ex As Exception
            Throw New ArgumentException(ex.Message)
        End Try

       
    End Sub

    ' Then use it like this
    Dim sSQL As String = "SELECT Users.DisplayName AS DisplayName, Users.UserID AS UserID, UserProfile.PropertyDefinitionID, UserProfile.PropertyValue, Users.FirstName, Users.LastName FROM Users INNER JOIN UserProfile ON Users.UserID = UserProfile.UserID WHERE (UserProfile.PropertyDefinitionID = 41)"
    Call fillComboBox(Me.cmbClient, sSQL)

Open in new window

grayeCommented:
Hang on... that's not the way the DataReader class works... it does not understand column names at all.   So change this:
            dt.Rows.Add(dr(3), dr(2))
 
Actually, most folks don't use a dataread to fill a datatable via code.  That's because there already is a high-level routine that does that for you (namely the DataAdapter.Fill method).
Consider this alternative (typed "by hand", so watch for typos, etc)

Dim sConn As String = strConnectionString
        Dim myConn As New SqlConnection(sConn)
        Dim sSQL As String = "SELECT Users.DisplayName AS DisplayName, Users.UserID AS UserID FROM Users INNER JOIN UserProfile ON Users.UserID = UserProfile.UserID WHERE (UserProfile.PropertyDefinitionID = 41)"
        Dim myCommand As New SqlCommand(sSQL, myConn)
        myCommand.CommandText = sSQL
        Dim da As New SqlDataAdapter(myCommand)
        Dim dt As New DataTable

        da.Fill(dt)

        cmbClient.DataSource = dt
        cmbClient.DisplayMember = "DisplayName"
        cmbClient.ValueMember = "UserID"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZhaolaiCommented:
Try changing your sql query string to this:

Dim sSQL As String = "SELECT ProjectID, UserID, Name FROM  Ryhal_Tickets_Projects WHERE UserID = '" & CType(cmbClient.SelectedValue, DataRowView)("UserID") & "'"

Open in new window

chrisryhalAuthor Commented:
Using Graye and Zhaolai's changes, this worked out beautifully and thank you so much!!
chrisryhalAuthor Commented:
Thanks again
Jorge PaulinoIT Pro/DeveloperCommented:
Have you checked my code? I don't understand ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.