Text property of Dropdownlist

Last post 07-09-2009 4:37 AM by pateldevang84. 10 replies.

Sort Posts:

  • Text property of Dropdownlist

    07-03-2009, 11:03 AM
    • Member
      8 point Member
    • Pankaj18
    • Member since 05-27-2009, 9:31 AM
    • Posts 52

    Hi,

    I want to display a text getting from Database, into a Dropdownlist using Text property.

    My code::

    SqlDataReader drd;
    if (drd.Read())
    {
        drpcompname.Text  = drd.GetValue(0).ToString();

    }


    But it display an error:

    'drpcompname' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Please Help me..

    Thanks!

    Pankaj Singh


    Filed under:
  • Re: Text property of Dropdownlist

    07-03-2009, 2:56 PM

    Use:

    MyDropDownList.Items.Add(new ListItem("India", "IND"));

    Here "INDIA" would be the text and "IND" would be the value.

    Hope it works!


    Saurabh Nijhawan(B.Tech. CSE,GGSIPU,New Delhi)
    Application Architect, Eminent Solutions, New Delhi.
    Freelancer | Teacher
    Remember to click "Mark as Answer" on the post, if it helped you.
    ASP.NET Weblog
    http://www.saurabhnijhawan.com
    Learning Made Easy


  • Re: Text property of Dropdownlist

    07-03-2009, 3:09 PM

     Hi,

    SqlDataReader drd;
    if (drd.Read())
    {
        drpcompname.Text  = drd.GetValue(0).ToString();

    }

    as per my understanding if you want to select text in drop down which already bind.

    drpcompname.items.findbyvalue("drd.GetValue(0).ToString()).selected=true;

    data bind code like this

    DropDownList1.DataTextField=dr["<column>"].Tostring();

    DropDownList1.DataValueField=dr["<column>"]Tostring();

    drd.datasource=dr[0].Tostring();

     

    Thanks :)

     

     

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Srinivas Kotra.


  • Re: Text property of Dropdownlist

    07-04-2009, 4:04 AM
    • Member
      8 point Member
    • Pankaj18
    • Member since 05-27-2009, 9:31 AM
    • Posts 52

    Hi,
    I have a Dropdownlist name drpcompname,which add some items on page_load event. And I have a Button name btnsearch,on which click event ,i want to display a single text  matching from database.

    My Code as shown below::

    Page_Load event Code::

    protected void Page_Load(object sender, EventArgs e)

        {

            if (!Page.IsPostBack)

            {

                com = new SqlCommand("select compname from client_info where clienttype='" + drpclienttype.SelectedItem.Text + "' and uid='" + lblamid.Text + "'", con);

            con.Open();

            drd = com.ExecuteReader();

            while (drd.Read())

            {

                 drpcompname.Items.Add(drd.GetValue(0).ToString());

            }

            con.Close();

        }

    }

    And btnsearch Click event::

     

            protected void btnsearch_Click(object sender, EventArgs e)

            {

            com = new SqlCommand("select * from addrequirement where reqid='" + txtreqid.Text + "'", con);

                con.Open();

                drd = com.ExecuteReader();

                if (drd.Read())

                {

                     drpclienttype.Text = drd.GetValue(16).ToString();

                }

                    con.Close();

        }

    Thanks!

    Pankaj Singh

  • Re: Text property of Dropdownlist

    07-04-2009, 4:09 AM
    • Member
      8 point Member
    • Pankaj18
    • Member since 05-27-2009, 9:31 AM
    • Posts 52

    Hi,
    I have a Dropdownlist name drpcompname,which add some items on page_load event. And I have a Button name btnsearch,on which click event ,i want to display a single text  matching from database.

    My Code as shown below::

    Page_Load event Code::

    protected void Page_Load(object sender, EventArgs e)

        {

            if (!Page.IsPostBack)

            {

                com = new SqlCommand("select compname from client_info where clienttype='" + drpclienttype.SelectedItem.Text + "' and uid='" + lblamid.Text + "'", con);

            con.Open();

            drd = com.ExecuteReader();

            while (drd.Read())

            {

                 drpcompname.Items.Add(drd.GetValue(0).ToString());

            }

            con.Close();

        }

    }

    And btnsearch Click event::

     

            protected void btnsearch_Click(object sender, EventArgs e)

            {

            com = new SqlCommand("select * from addrequirement where reqid='" + txtreqid.Text + "'", con);

                con.Open();

                drd = com.ExecuteReader();

                if (drd.Read())

                {

                     drpclienttype.Text = drd.GetValue(16).ToString();

                }

                    con.Close();

        }

    But it display an error:

     

    'drpcompname' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Thanks!

    Pankaj Singh

  • Re: Text property of Dropdownlist

    07-04-2009, 5:01 AM
    • Member
      270 point Member
    • veenag81
    • Member since 07-04-2009, 7:04 AM
    • Gandhinagar,India
    • Posts 61

    'drpcompname' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Above error come when u have value in dropdownlist not match with the tabledata. If in your dropdownlist value like "Baroda" and value which u r fetching is "baroda" then also u get above error. so make sure that both r same so u not get error. Also watch for uppercase and lowercase of value also.

  • Re: Text property of Dropdownlist

    07-04-2009, 12:45 PM
    • Member
      178 point Member
    • rickj1
    • Member since 12-17-2007, 4:49 AM
    • Parksville B.C.
    • Posts 94

     Try it with linq I have two ddl that combine the results and select from data base selecting two rows

    I used this tutorial and tweeked it to fit my needs

    http://www.asp.net/learn/linq-videos/video-233.aspx

     

    protected void btnSelect_Click(object sender, EventArgs e)
        {
            RentagentDataContext db = new RentagentDataContext();
            var userProfile = from u in db.UserProfiles
                              where (u.CityId  == Int32 .Parse  (ddlCity .SelectedValue ) && (u.CatergoryId == Int32 .Parse (ddlCatergory .SelectedValue )))
                              select u;
            workerAdds.DataSource = userProfile;
            workerAdds.DataBind();
        }

     

    don't forget to mark as answer if this helps

    Beta Junky
    www.barterlinks.net
  • Re: Text property of Dropdownlist

    07-04-2009, 2:34 PM
    Answer

     hi,

    i dont find errors in your code, but plz change this code line

    com = new SqlCommand("select compname from client_info where clienttype=' " + drpclienttype.SelectedItem.Text.Trim() + " ' and uid=" + lblamid.Text , con);

    and i have observed some times the below syntex will not work.

    drpclienttype.Text = drd.GetValue(16).ToString(); change this to like below.

    drpclienttype.item.findbyvalue[drd.GetValue(16).ToString()].selected=True;

    Thanks :)

     

     

     

     

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Srinivas Kotra.


  • Re: Text property of Dropdownlist

    07-09-2009, 4:13 AM
    Answer

    Hi,

    I think the problem is based on the sql query instead of the code "drpclienttype.Text = drd.GetValue(16).ToString();".

    I noticed that you add the items to the DropDownList from table client_info, but when you set the DropDownList's Text, you used table addrequirement. You need to know, when we set the string to DropDownList's Text, this string must be contained in the DropDownList itself. Like the two example below, the first one of which is correct but the second one is not.

    DropDownList1.Items.Add("1");
    DropDownList1.Items.Add("2");
    DropDownList1.Items.Add("3");
    
    DropDownList1.Text = "2";


    DropDownList1.Items.Add("1");
    DropDownList1.Items.Add("2");
    DropDownList1.Items.Add("3");
    
    //this is not correct, because there is no "5" in the DropDownList
    DropDownList1.Text = "5";

    So, I suggest you having a check to make sure that you set the string in the items of the DropDownList to the DropDownList.

    Best Regards,
    Shengqing Yang

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread : )
  • Re: Text property of Dropdownlist

    07-09-2009, 4:33 AM
    • Member
      30 point Member
    • pateldevang84
    • Member since 07-06-2009, 8:51 AM
    • Ahmedabad - India
    • Posts 12

    USE BELOW CODE

    --------------------------

    SqlDataReader drd;
    if (drd.Read())
    {
        drpcompname.Items.Add(drd.GetValue(0).ToString());
    }


    May be this help........

    Devang Patel
    Software Engineer
    Ahmedabad - India
    Email Id : pateldevang84@gmail.com

    Mark as Answer if this reply helps you
  • Re: Text property of Dropdownlist

    07-09-2009, 4:37 AM
    • Member
      30 point Member
    • pateldevang84
    • Member since 07-06-2009, 8:51 AM
    • Ahmedabad - India
    • Posts 12

    USE BELOW CODE

    --------------------------

    SqlDataReader drd;
    if (drd.Read())
    {
         drpcompname.Items.Add(drd.GetValue(0).ToString());

         drpcompname.Items.FindByText("drd.GetValue(0).ToString()").Selected = True;
    }


    May be this help........

    Devang Patel
    Software Engineer
    Ahmedabad - India
    Email Id : pateldevang84@gmail.com

    Mark as Answer if this reply helps you
Page 1 of 1 (11 items)