hello all,
i want to ask how to i set variable in asp:sqldatasource in sqlcommand in asp.net c#....
This is my code

asp.net

<dx:ASPxListBox ID="lsAssignToko" runat="server" DataSourceID="SqlDataSource6" 
                                            TextField="NAMA" ValueField="ID" AutoPostBack="true" 
                                            EnableCallbackMode="True" SelectionMode="CheckColumn" 
                                            onselectedIndexChanged="lsAssignToko_SelectedIndexChanged">
                                            <Columns>
                                                <dx:ListBoxColumn FieldName="ID"/>
                                                <dx:ListBoxColumn FieldName="NAMA" />
                                            </Columns>
                                        </dx:ASPxListBox>
                                      
                                        <asp:SqlDataSource ID="SqlDataSource6" runat="server" 
                                            ConnectionString="<%$ ConnectionStrings:Ora2010 %>" 
                                            ProviderName="<%$ ConnectionStrings:Ora2010.ProviderName %>" 
                                            
                                            
                                            
                                            SelectCommand="SELECT ID, NAMA FROM REF_TOKO WHERE ID IN ( :inTOKO ) ORDER BY NAMA">
                                          
                                            <SelectParameters>
                                                <asp:SessionParameter Name="inTOKO" SessionField="inTOKO" Size="200" Type="String" />
                                            </SelectParameters>
                                          
                                        </asp:SqlDataSource>

C#

string dummy = "";
        string data = "";
        string inTK = "";
        string inTOKO = "";
        //string inTOKO1 = "";
        int pot;

        for (int i = lsToko.SelectedItems.Count - 1; i >= 0; i--)
        {
            string tok = lsToko.SelectedItem.Text.ToString();
            data = lsToko.SelectedItem.Value.ToString();
            dummy = dummy + "" + data + ",";
            int index = lsToko.SelectedItem.Index;
            lsToko.Items.RemoveAt(index);

        }

        if (lsToko.SelectedItems.Count <= 1)
        {
            inTK = dummy.Length.ToString();
            pot = int.Parse(dummy.Length.ToString()) - 1;
            inTOKO = dummy.Substring(0, pot);
        }

       
        Session["inTOKO"] = inTOKO;

the problem is where in my variable inTOKO consist of couple of value...
Example
while variable inTOKO = one value success but while value inTOKO = two value error
inTOKO = 1101111 => success
inTOKO = 1101111,1211321 => not success

i stuck in here. Please tell me how i fix it. Thanks.....

I might try something like the following in your code-behind:

string[] inTOKOset = inTOKO.Split(',');
string selCommand = "SELECT ID, NAMA FROM REF_TOKO WHERE ID='";
if (inTOKOset.Length > 1)
{
    for (int a = 0; a < inTOKOset.Length; a++)
    {
        if (a < inTOKOset.Length - 1)
        {
            selCommand += inTOKOset[a] + "' AND WHERE ID='";
        }
        else
        {
            selCommand += inTOKOset[a] + "' ORDER BY NAMA";
        }
    }
}
else
{
    selCommand += inTOKOset[0] + "' ORDER BY NAMA";
}
SqlDataSource6.SelectCommand = selCommand;

I'm sure there's an easier way where a collection of strings can be passed in one WHERE query segment but this should at least get you working.

Mark as solved if this clears up your issue ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.