List box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raam
    New Member
    • Jun 2008
    • 11

    List box

    hi

    I have a list box with some country names from database.
    when i click search button the "country" list selecteditem should be selected.
    but it is not.
    here is my code where iam using a stored procedure which is outputting 3 tables simultaneously.
    the list box of country is 3rd table that is table[2]

    Code:
    if (objDataSet.Tables[1].Rows.Count > 0)
                    {
                        for (int i = 0; i < objDataSet.Tables[1].Rows.Count; i++)
                        {
                            int k;
                            k = Convert.ToInt32(objDataSet.Tables[1].Rows[i][0]);
                            lbspecialty.Items[k].Selected = true;
                            lbspecialty.Items.FindByText("select").Selected = false;
    
                        }
                    }
                    else
                    {
                        lbspecialty.Items.FindByText("select").Selected = true;
                    }
    
    if (objDataSet.Tables[2].Rows.Count > 0)
                    {
                        for (int i = 0; i < objDataSet.Tables[2].Rows.Count; i++)
                        {
                            string k;
                            k = objDataSet.Tables[2].Rows[i][0].ToString();
                            //k = Convert.ToInt32(objDataSet.Tables[2].Rows[i][0]);
                            lstgbgterr.Items.FindByText("k").Selected=true;
                            lstgbgterr.Items.FindByText("select").Selected = false;
    
                        }
                    }
    if iam using the above condition its neither showing any results nor debugger entering the if condition

    Code:
    if (objDataSet.Tables[1].Rows.Count > 0)
                    {
                        for (int i = 0; i < objDataSet.Tables[1].Rows.Count; i++)
                        {
                            int k;
                            k = Convert.ToInt32(objDataSet.Tables[1].Rows[i][0]);
                            lbspecialty.Items[k].Selected = true;
                            lbspecialty.Items.FindByText("select").Selected = false;
    
                        }
                    }
                    else
                    {
                        lbspecialty.Items.FindByText("select").Selected = true;
                    }
     if (objDataSet.Tables[2].Rows.Count > 0)
                    {
                        for (int i = 0; i < objDataSet.Tables[2].Rows.Count; i++)
                        {
                            int k;
                            k = Convert.ToInt32(objDataSet.Tables[2].Rows[i][0]);
                            lbspecialty.Items[k].Selected = true;
                            lbspecialty.Items.FindByText("select").Selected = false;
    
                        }
                    }
    when i use this condition as table is passing string it is showing error.

    plz help me out where iam going wrong

    thanks,
    raam
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    Raam,
    Let's say your integer data in your first column is 10, 30, 20.
    The way your first loop is written, you are setting
    lbspecialty.Ite ms[10] to selected;
    lbspecialty.Ite ms[30] to selected;
    lbspecialty.Ite ms[20] to selected;

    This is probably not what you want. You want to select the item whose "Value" is 10, 30, or 20 (or perhaps whose DisplayedText is "10", it is not clear from your code).

    So you need to either find item with a string value or display of "10":

    lbspecialty.Ite ms.FindByText(k .ToString()).Se lected = true;
    or
    lbspecialty.Ite ms.FindByValue( k).Selected = true;

    In your second loop, your are searching for the item whose text is "k". Again, you probably want the item whose text is stored in the string k (no quotes):

    lstgbgterr.Item s.FindByText(k) .Selected = true;

    Comment

    Working...