Avatar of Victor  Charles
Victor Charles
Flag for United States of America asked on

Help with Query Statement

Hello,

Can you please help me with the right syntax for "in (S)". Basically s represents the ID values already selected. I need to check it against each row and if found set the row as selected = true, problem is with my "in (S)" command.

        C1Country.DataSource = dtCountry
        C1Country.DataTextField = "Country"
        C1Country.DataValueField = "Country_ID"
        C1Country.DataBind()
        C1Country.Text = dtCountry.Rows(CurrentIndex).Item("Country")
        Dim S As String
        S = "1,2,6,7,11"
        For i = 0 To C1Country.Items.Count - 1
       if C1Country.Items(i).Text in (S) Then
                C1Country.Items(i).Selected = True
            End If
        Next
Thanks,
Victor
.NET ProgrammingASP.NETSQL

Avatar of undefined
Last Comment
Rajar Ahmed

8/22/2022 - Mon
Gerwin Jansen

Can you try like this:

if C1Country.Items(i).Text in (1,2,6,7,11) Then
Victor Charles

ASKER
I need to include a variable because the value of s will change.
harfang

This looks like a variant of VB, not ASP. VB and VB.NET do not have an In() operator. SQL does, but you don't show us the SQL part of your code.

Basically, you will have to use a data structure like an array, a collection, or a dictionary to store your list of values and use a loop inside the loop to simulate the In() operator.

    Dim S As Variant
    For Each S In Array(1, 2, 6, 7, 11)
        If C1Country.Items(i) = S Then
            ' select and...
            Exit For
        End If
    Next S

Open in new window

The array can be a passed argument, a global variable, &c.

(°v°)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Victor Charles

ASKER
Hi,

This is an ASP.NET project with VB.NET code.  My problem with line

For Each S In Array(1, 2, 6, 7, 11) is the value of s will vary, it will not alwasy be 1,2,6,7. How do I accomplish the same without harcd coding the value of s?

Thanks,
harfang

For example:

    Dim Included() As Integer

    ReDim Included(2)
    Included(0) = 11
    Included(1) = 6
    Included(2) = 7
    
    ...
    
    For Each I In Included
        ...

Open in new window

Or as a parameter of a Sub
Sub SelectRows(pvarInclude)
    ...
    For Each I In pvarInclude
       ...
End Sub

' call as:
    SelectRows Array(1, 2, 3)

Open in new window

You can manipulate arrays in many ways. If you show us where the list of numbers comes from, we can show you how to store them in an array.

(°v°)
Rajar Ahmed

i guess this is the  same question we are progressing in another question also . isnt ?

       
 If utube IsNot Nothing Then
            For i As Integer = 0 To DropDownList1.Items.Count - 1
                If utube.Split(",").Contains(DropDownList1.Items(i).Text) Then
                    DropDownList1.Items(i).Selected = True
                End If
            Next
End If

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Victor Charles

ASKER
Yes it is, workimg on it now, will get back to you soon. Thanks.
Victor Charles

ASKER
Just tried your code from this post. I noticed not all the values in the utube string are selected in the control, for example if the string contains BEL,CAN. Only CAN is selected in the control.
Rajar Ahmed

which control are you using

dropdownlist or listbox ?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Victor Charles

ASKER
I am using a third party Combobox control from Componentone.
ASKER CERTIFIED SOLUTION
Rajar Ahmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rajar Ahmed

however, i dont know about your componentone combobox features anyway
if u see dropdownlist.clearselection() on your code plz comment and check .
Victor Charles

ASKER
Hi,

it is working ok now.

Thanks

Victro
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rajar Ahmed

Glad it worked :) Close the question by accepting solutions which worked .