can't get value of selecteditem in DropDownList!!!!

Last post 07-10-2009 10:43 PM by MAW74656. 12 replies.

Sort Posts:

  • can't get value of selecteditem in DropDownList!!!!

    03-30-2005, 12:17 PM
    • Member
      30 point Member
    • kristi3
    • Member since 02-09-2005, 11:48 AM
    • Posts 6

    Hi:)  I know this might sound simple, but I would like to use the selected item in my drop down list as the parameter in a query and then populate my text boxes with it!  I can't see what I am doing wrong.  Here is my code.. (the error that is thrown is telling me I can't convert string"" to double...)

    Private Sub ddlInvNums2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlInvNums2.SelectedIndexChanged

    Dim selectedID As Integer

    selectedID = ddlInvNums2.SelectedItem.Text

    cmdType.Parameters("@invnum").Value = selectedID

    MROcnn.Open()

    Dim drea As SqlClient.SqlDataReader

    drea = cmdType.ExecuteReader(CommandBehavior.SingleRow)

    If drea.Read() Then

    txttype.Text = drea(0)

    End If

    drea.Close()

    MROcnn.Close

    End Sub

     

    help! 

    kristinConfused [*-)]

  • Re: can't get value of selecteditem in DropDownList!!!!

    03-30-2005, 1:10 PM
    • All-Star
      28,014 point All-Star
    • bmains
    • Member since 10-22-2004, 12:20 PM
    • Posts 5,651
    • TrustedFriends-MVPs

    You should check to see if the text is numeric, before passing it to the command and continuing.  Do you have autopostback="true" on the dropdownlist?  How are you populating the dropdownlist?

    Brian

    "Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
  • Re: can't get value of selecteditem in DropDownList!!!!

    03-30-2005, 1:18 PM
    • All-Star
      46,513 point All-Star
    • ndinakar
    • Member since 05-05-2003, 4:57 PM
    • Redmond, WA
    • Posts 6,858
    • Moderator
      TrustedFriends-MVPs
    (1) you need to declare the parameter first before you set its value.
    here's a sample :
     
    myCommand.Parameters.Add(New SqlParameter("@ttype",SqlDbType.VarChar,25))
    myCommand.Parameters("@ttype").Value = "charge"
     
    the first line above declares the parameter. the second line sets the value to it.

    (2) I dont see your sql statemenr or the commandtype of stored procedure..
    (3) IF you are requesting for only one value back from the database you can use Execute Scalar() Method which is much faster.
     
    I'd recommend reading up some articles on Data Access.
    ***********************
    Dinakar Nethi
    Life is short. Enjoy it.
    ***********************
  • Re: can't get value of selecteditem in DropDownList!!!!

    03-30-2005, 2:09 PM
    • Member
      30 point Member
    • kristi3
    • Member since 02-09-2005, 11:48 AM
    • Posts 6

    I have autopostback set to true.  The error message is telling me that it isn't reading any value from the dropdownlist.  what do you think?

     

    kristin

  • Re: can't get value of selecteditem in DropDownList!!!!

    03-30-2005, 2:18 PM
    • Member
      30 point Member
    • kristi3
    • Member since 02-09-2005, 11:48 AM
    • Posts 6

    the parameter is declared in the Collections as nvarchar .  I am just wondering why it isn't reading the selecteditem.text.....

     

     

  • Re: can't get value of selecteditem in DropDownList!!!!

    03-30-2005, 4:02 PM
    • All-Star
      28,014 point All-Star
    • bmains
    • Member since 10-22-2004, 12:20 PM
    • Posts 5,651
    • TrustedFriends-MVPs

    Don't know, last time I had that problem was because I forgot to put if (not Page.IsPostBack) around my code loading the data into the page.

    Brian

    "Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
  • Re: can't get value of selecteditem in DropDownList!!!!

    03-30-2005, 4:23 PM
    • Member
      30 point Member
    • kristi3
    • Member since 02-09-2005, 11:48 AM
    • Posts 6

    got that...still telling me input string not in the correct format.

    InvalidCastException: Cast from string "" to type 'Double' is not valid.

  • Re: can't get value of selecteditem in DropDownList!!!!

    03-31-2005, 7:42 AM
    • All-Star
      16,469 point All-Star
    • stiletto
    • Member since 07-10-2003, 8:42 AM
    • Louisville, KY
    • Posts 3,216

    Change:

    selectedID = ddlInvNums2.SelectedItem.Text

    To:

    selectedID = Convert.ToInt32(ddlInvNums2.SelectedItem.Text)

  • Re: can't get value of selecteditem in DropDownList!!!!

    03-31-2005, 9:37 AM
    • Member
      30 point Member
    • kristi3
    • Member since 02-09-2005, 11:48 AM
    • Posts 6

    nopeCrying [:'(]  still giving me that same error!  For some reason it is reading the value of my dropdown as null!  I'm ready to give up.  but thanks for the input...

    kristin

  • Re: can't get value of selecteditem in DropDownList!!!!

    03-31-2005, 11:05 AM
    • All-Star
      46,513 point All-Star
    • ndinakar
    • Member since 05-05-2003, 4:57 PM
    • Redmond, WA
    • Posts 6,858
    • Moderator
      TrustedFriends-MVPs
    you are probably losing the value during postback..do you have the If not ispostback loop in your page_load ?
    ***********************
    Dinakar Nethi
    Life is short. Enjoy it.
    ***********************
  • Re: can't get value of selecteditem in DropDownList!!!!

    03-31-2005, 11:25 AM
    • Member
      30 point Member
    • kristi3
    • Member since 02-09-2005, 11:48 AM
    • Posts 6

    I have it.  Do I have to have "if ispostback" in there?

    I'm not sure if it matters, but the dropdownlist (ddlInvNumbers) is created dynamically.  I fill it on page load like this:

    If Not IsPostBack And txtinvnum.Text <> "" Then

    Dim dreader As SqlClient.SqlDataReader

    MROcnn.Open()

    cmdAllNums.Parameters("@mrocomid").Value = txtinvnum.Text

    dreader = cmdAllNums.ExecuteReader()

    Dim firstrow As Boolean = True

    While dreader.Read()

    ddlInvNumbers.Items.Add(New ListItem(dreader(0).ToString()))

    If firstrow Then

    txtmrocomid.Text = dreader(1).ToString

    txtinvnum.Text = dreader(1).ToString

    txtnumber.Text = dreader(0).ToString()

    txtcompany.Text = dreader(2).ToString()

    txtbillingcontact.Text = dreader(3).ToString()

    txtbillingphone.Text = dreader(4).ToString()

    txtbillingext.Text = dreader(5).ToString()

    txtbillingfax.Text = dreader(6).ToString()

    txtbillingemail.Text = dreader(7).ToString()

    txtbillingaddress.Text = dreader(8).ToString()

    txtbillingaddress2.Text = dreader(9).ToString()

    txtbillingcity.Text = dreader(10).ToString()

    txtbillingstate.Text = dreader(11).ToString()

    txtbillingzip.Text = dreader(12).ToString()

    txtinvdate.Text = dreader(14).ToString()

    txtinvdate.Text = FormatDateTime(txtinvdate.Text, DateFormat.ShortDate).ToString()

    txttype.Text = dreader(15).ToString()

    firstrow = False

    End If

    Session("MROID") = txtmrocomid.Text

    End While

    dreader.Close()

    end if

     

    what do you think?

     

     

     

     

     

  • Re: can't get value of selecteditem in DropDownList!!!!

    04-01-2005, 9:49 PM
    • All-Star
      16,469 point All-Star
    • stiletto
    • Member since 07-10-2003, 8:42 AM
    • Louisville, KY
    • Posts 3,216

    You have to rebuild dynamic DDLs on every postback for the values to be there.

  • Re: can't get value of selecteditem in DropDownList!!!!

    07-10-2009, 10:43 PM
    • Member
      21 point Member
    • MAW74656
    • Member since 07-08-2009, 12:56 PM
    • Posts 26

    Is this an ASP.NET DropDownList Control? If so, then ASP.NET creates a new id for the control when it is rendered for the browser. See This Post for help finding the client id.

Page 1 of 1 (13 items)