please anybody to help Dropdownlist selected index problem

Discussion in 'C#' started by Ravikumar, Mar 7, 2007.

  1. #1
    Hi

    I am working in asp.net with C# web application, I have used visual studio 2003, i have used dropdownlist
    in my application to populate the data from mysql database, i have got problem while i selected the item from
    dropdownlist, its not selecting correct item i'm selecting from dropdownlist, i have 64 records in my table, but i
    tried to work with upto 15 records it will not giving problem, when the scroll will enable to dropdownlist its
    giving problem, its not selecting the correct record what i'm selecting in dropdownlist, i have used following
    codes to fill dropdown and selected index,

    public DataSet LoadShotTaskCode()
    {
    cmd = new MySqlCommand("HD_PROC_LOADSHOTTASK",global_con);
    cmd.CommandType = CommandType.StoredProcedure;
    adapter = new MySqlDataAdapter(cmd);
    dataset = new DataSet();
    adapter.Fill(dataset,"hd_tbl_taskcodes");
    return dataset;
    }

    private void FillDropDown()
    {
    Access = new clshome();
    ds = Access.LoadShotTaskCode();
    if(ds.Tables[0].Rows.Count != 0 )
    {
    DropDownList1.DataTextField = "task_code";
    DropDownList1.DataValueField = "task_description";
    DropDownList1.DataSource = ds;
    DropDownList1.DataBind();
    if(DropDownList1.Items.Count != 0)
    {
    TxtEditTaskCode.Text = ds.Tables[0].Rows[0]["task_code"].ToString();
    TxtEditTaskDesc.Text = ds.Tables[0].Rows[0]["task_description"].ToString();
    }
    }
    else
    {
    DropDownList1.Items.Clear();
    }
    }

    public MySqlDataReader Select_ShotTaskCoes(string tcode)
    {
    MySqlCommand taskcmd = new MySqlCommand("select * from hd_tbl_taskcodes where task_code='"+ tcode+"'",global_con);
    global_con.Open();
    MySqlDataReader dr = taskcmd.ExecuteReader(CommandBehavior.CloseConnection);
    return dr;
    }

    private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    Access = new clshome();
    MySqlDataReader taskdr = Access.Select_ShotTaskCoes(DropDownList1.SelectedItem.Text);
    if(taskdr.Read())
    {
    TxtEditTaskCode.Text = taskdr.GetValue(0).ToString();
    TxtEditTaskDesc.Text = taskdr.GetValue(1).ToString();
    }
    Access.global_con.Close();
    }


    please anybody help me to slove this problem, advanced Thanks for any reply.

    Thanks.
     
    Ravikumar, Mar 7, 2007 IP
  2. Forrest

    Forrest Peon

    Messages:
    500
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure, I don't use data-binding. Normally I would get a DataReader, then populate list items in a loop.

    You might want to look at the query data that's being returned, though? Is there a duplicate somewhere that might be throwing your code off?
     
    Forrest, Mar 7, 2007 IP
  3. ztoma

    ztoma Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sorry, but the code is bearly readable.

    i would just say one thing: ListItem's Text and Value fields are supposed to be used as follows: Text contains the display property, and the value an unique identifier.

    Thus, when you check which item was selected, you use Value.
     
    ztoma, Mar 7, 2007 IP
  4. Ravikumar

    Ravikumar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,


    Thanks lot, i did big mistake in table, i didnt set primary key for my tables, i have set the primary key for my table now, dropdownlist problem automically solved, thanks for your immediate reply, because your reply gave more boost and ideas to me.



    Thanks.
     
    Ravikumar, Mar 8, 2007 IP