Results 1 to 20 of 20

Thread: [RESOLVED] fill textbox from dropdown thanks ACONYBEAR

  1. #1

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Resolved [RESOLVED] fill textbox from dropdown thanks ACONYBEAR

    i am trying to populate a drop down list from 3 columns in one the row

    has anyone every did this before, it seems to be a complete nightmare
    Last edited by d2005; Oct 4th, 2005 at 05:31 AM.
    it works 60% of the time, all the time.

  2. #2
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: populate drop down from multiple columns

    Have you got a few more details on this? Are the vals coming from a db? And are you trying to do it manually or are you using bind()?

    Have you got someting to post here, as cut down as possible to keep it simple please.

    When I populate dropdownlists from a DB I use a datareader and loop through the recordset, if this is the kind of thing you're trying to do I'll post a simple example.

    Cheers Al

    If I'm missing the point do let me know
    Last edited by aconybeare; Sep 30th, 2005 at 08:38 AM.

  3. #3
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: populate drop down from multiple columns

    Here's some (pseudo) code to fill a 1-D array from 3 columns. Then you can pop the
    array into a dropdown list:

    SQLCommand1 = "SELECT a,b,c FROM tablex"

    Dim myArray() as String
    Dim myReader as SQLDataReader

    myReader = SQLCommand1.ExecuteReader

    Dim i as integer = 0

    While(myReader.Read)
    myArray(i) = myReader.Item(0)
    myArray(i+1) = myReader.Item(1)
    myArray(i+2) = myReader.Item(2)
    i = i + 3
    End While

  4. #4
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    Re: populate drop down from multiple columns

    If you are pulling the data from a SQL server...this is how I did it:

    http://www.vbforums.com/showthread.php?t=359584

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: populate drop down from multiple columns

    Quote Originally Posted by d2005
    i am trying to populate a drop down list from 3 columns in one the row

    has anyone every did this before, it seems to be a complete nightmare
    Do you want all three concatenated? If so, perform the concatenation in your SQL Statement.

    EG:
    Code:
    SELECT Field1 + ' ' + Field2 + ' ' + Field3 AS TheCompleteThing FROM TableName

  6. #6

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    thanks guys, im gonna chack out all these threads now im sure they will help,
    basically the drop down will just contain like three words
    msg default
    custom1
    custom2

    which will have the drop down linked to the columns in databse,
    i need it to say this because the mesages are to long
    when i get it fixed ill mark resolved and post the code
    it works 60% of the time, all the time.

  7. #7

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    i want it to say
    if in drop down chosen fill textbox1 with the correspondong column
    if that clears it up
    it works 60% of the time, all the time.

  8. #8

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    VB Code:
    1. Dim selectcommand As New SqlCommand
    2.         SelectCommand = New SqlCommand("SELECT c_msg_default, c_msg_custom1, c_msg_custom2 from tb_comp_detail WHERE (c_companycode = '" & lblUser.Text & "')", oSQLConn)
    3.         Dim myArray() As String
    4.         Dim myReader As SqlDataReader
    5.         myReader = selectcommand.ExecuteReader
    6.  
    7.         Dim i As Integer = 0
    8.  
    9.         While (myReader.Read)
    10.             myArray(i) = myReader.Item(0)
    11.             myArray(i + 1) = myReader.Item(1)
    12.             myArray(i + 2) = myReader.Item(2)
    13.             i = i + 3
    14.         End While

    so thats how far i am now,
    howe can i move on to loading this to my ddl
    i am getting an error now object reference
    it works 60% of the time, all the time.

  9. #9
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: populate drop down from multiple columns

    Hi,

    This is the way I do it, I use a stored proc.

    VB Code:
    1. Private Sub LoadCountriesDDL(Optional ByRef sDefault As String = "0")
    2.  
    3.         ' Open the connection
    4.         If myCon.State = ConnectionState.Closed Then myCon.Open()
    5.  
    6.         ' 2. Create the command object
    7.         Dim myCmd As New SqlCommand
    8.         myCmd.CommandType = CommandType.StoredProcedure
    9.         ' Set command to create your SQL statement
    10.         myCmd.CommandText = "up_GetCountries"
    11.         ' Set the database connection
    12.         myCmd.Connection = myCon
    13.  
    14.         Dim dr As SqlDataReader
    15.         Dim i As Integer
    16.  
    17.         i = 0
    18.  
    19.         Try
    20.             dr = myCmd.ExecuteReader()
    21.  
    22.             ddlCountry.Items.Add("[Select]")
    23.             ddlCountry.Items(i).Value = "0"
    24.  
    25.             Do While dr.Read()
    26.                 i += 1
    27.                 ddlCountry.Items.Add(Convert.ToString(dr("Country")))
    28.                 ddlCountry.Items(i).Value = Convert.ToString(dr("Country_Code"))
    29.                 If sDefault.Trim = Convert.ToString(dr("Country_Code")) Then
    30.                     ddlCountry.SelectedIndex = i
    31.                 End If
    32.             Loop
    33.  
    34.         Catch ex As Exception
    35.             ' Record any exceptions and exit
    36.             message.Visible = True
    37.             message.Text = "<p><font color=""red"">The following exception occurred: LoadCountriesDDL<br />" + ex.Message + "</font></p>"
    38.         End Try
    39.  
    40.         dr.Close()
    41.         If myCon.State = ConnectionState.Open Then myCon.Close()
    42.     End Sub

  10. #10

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    using my above code in array how do i pop that into ddl
    it works 60% of the time, all the time.

  11. #11
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: populate drop down from multiple columns

    d2005,

    I've never used this technique, but I would say it would be something like this

    Code:
    myddl.DataSource = myArray()
    myddl.DataBind()
    If you don't want to use my approach I would use Mendhak's technique which would remove a step thus making your code more efficient (I think).

    My way allows you to set the default selected item where you don't know the index but know the value or text they previously chose ideal if allowing a user to edit their data.

    and use the above to bind to the datareader

    VB Code:
    1. Dim selectcommand As New SqlCommand
    2. SelectCommand = New SqlCommand("SELECT c_msg_default + ' ' + c_msg_custom1 + ' ' + c_msg_custom2 as threeFields FROM tb_comp_detail WHERE (c_companycode = '" & lblUser.Text & "')", oSQLConn)
    3. Dim myReader As SqlDataReader
    4. myReader = selectcommand.ExecuteReader
    5.  
    6. myddl.DataSource = myReader
    7. myddl.DataBind()
    Last edited by aconybeare; Oct 3rd, 2005 at 01:04 PM.

  12. #12

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    this is how far ive got now

    my code looks an absolute mess and have about 100 commented lines lol

    when the page loads it does indeed fill my textbox to the default message, but if i change the drop down list to another selection nothin happens
    i need it to change the textbox info from selection of drop down

    any ideas

    thanks

    VB Code:
    1. lstMessage.Items.Add(New ListItem("Default", "c_msg_default"))
    2.         lstMessage.Items.Add(New ListItem("Message1", "c_msg_custom1"))
    3.         lstMessage.Items.Add(New ListItem("Message2", "c_msg_custom2"))
    4.  
    5.         Dim imessage = lstMessage.SelectedItem.Value
    6.  
    7.         If lstMessage.SelectedItem.Value = "c_msg_default" Then
    8.             Dim cmdselect As SqlCommand
    9.             cmdselect = New SqlCommand("SELECT c_msg_default from tb_comp_detail  where(c_companycode = '" & lblUser.Text & "')", oSQLConn)
    10.             txtMessageReply.Text = cmdselect.ExecuteScalar().ToString()
    11.         ElseIf lstMessage.SelectedItem.Value = "c_msg_custom1" Then
    12.             Dim cmdselect As SqlCommand
    13.             cmdselect = New SqlCommand("SELECT c_msg_custom1 from tb_comp_detail  where(c_companycode = '" & lblUser.Text & "')", oSQLConn)
    14.             txtMessageReply.Text = cmdselect.ExecuteScalar().ToString()
    15.         ElseIf lstMessage.SelectedItem.Value = "c_msg_custom1" Then
    16.             Dim cmdselect As SqlCommand
    17.             cmdselect = New SqlCommand("SELECT c_msg_custom2 from tb_comp_detail  where(c_companycode = '" & lblUser.Text & "')", oSQLConn)
    18.             txtMessageReply.Text = cmdselect.ExecuteScalar().ToString()
    19.         End If
    it works 60% of the time, all the time.

  13. #13
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: populate drop down from multiple columns

    d2005,

    Sorry, I posted the above without two necessary settings, here is a fully working snippet
    VB Code:
    1. oSQLConn.Open()
    2. Dim cmd As New SqlCommand
    3. cmd = New SqlCommand("SELECT c_msg_default, c_msg_custom1, c_msg_custom2 As ThreeFields FROM tb_comp_detail WHERE (c_companycode = '" & lblUser.Text & "')", oSQLConn)
    4. Dim dr As SqlDataReader
    5. dr = cmd.ExecuteReader
    6.  
    7. With myDDL ' change this to whatever your DDL is called
    8.    .DataSource = dr
    9.    [b].DataTextField = "ThreeFields"
    10.    .DataValueField = "ThreeFields"[/b]
    11.    .DataBind()
    12. End With
    13.  
    14. dr.Close()

  14. #14
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: populate drop down from multiple columns

    d2005,

    For a start (based on what you've got ie the value in your list is the name of your column) you can tidy your code up like this -
    VB Code:
    1. lstMessage.Items.Add(New ListItem("Default", "c_msg_default"))
    2.             lstMessage.Items.Add(New ListItem("Message1", "c_msg_custom1"))
    3.             lstMessage.Items.Add(New ListItem("Message2", "c_msg_custom2"))
    4.  
    5.             Dim imessage = lstMessage.SelectedItem.Value
    6.             Dim cmdselect As SqlCommand
    7.             Dim sSQL As New StringBuilder ' imports system.text
    8.  
    9.             sSQL.Append("SELECT " & lstMessage.SelectedItem.Value & _
    10.                               " FROM tb_comp_detail" & _
    11.                               " WHERE(c_companycode = '" & lblUser.Text & "')")
    12.            
    13.             cmdselect = New SqlCommand(sSQL.ToString, oSQLConn)
    14.             txtMessageReply.Text = cmdselect.ExecuteScalar().ToString()

  15. #15

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    i did your above code acony and it seems to give me just the last message in the drop down, ie the value of default message 2
    i really appreciate all of your replys and your help has given me good insight as to how the whole drop down works

    what my web application needs to do is when the user selectes a value from the drop down then it fills the textbox txtmessage out

    then when the user presses send i shall use this textbox to write to the database
    my problem is filling the textbox frfom the users selection,
    as you can see i have tried now a different approach

    when the page first loads using my above code it does fill the txtbox with the default message but when i select a different message nothing happens,
    it must be a setting or something

    ill get there, ill let u know
    it works 60% of the time, all the time.

  16. #16

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    acony that code abouve works great ,
    the setting i have to change is in the post back
    i have that code in a function bind message choice,
    i think i need to put it into its own function with bindmessage choice working on its own just filling in the three choices
    it works 60% of the time, all the time.

  17. #17
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: populate drop down from multiple columns

    The reason your dropdown in only getting one value is I suspect because of your where clause?

    Plus I'm not sure whether this is what you want or not

    Html
    Code:
    <TR vAlign="top">
      <TD>Countries:</TD>
      <TD><asp:dropdownlist id="myDDL" runat="server" AutoPostBack="True" OnSelectedIndexChanged="myDDL_SelectedIndexChanged" /></TD>
    </TR>
    <TR vAlign="top">
      <TD>Full Name:</TD>
      <TD><asp:textbox id="txtSelectedName" runat="server" /></TD>
    </TR>
    CodeBehind
    VB Code:
    1. Public Sub myDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myDDL.SelectedIndexChanged
    2.   txtSelectedName.Text = myDDL.SelectedItem.Value
    3. End Sub

  18. #18

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    i think the advice you have given me earlier is working but my settings are not right,
    ill show you what i have and tell you what it does

    all in code behind file


    VB Code:
    1. If Not IsPostBack Then
    2.             bindmessagechoice()
    3.         End If
    .................

    VB Code:
    1. Public Sub bindmessagechoice()
    2.  
    3.      
    4.  
    5.         lstMessage.Items.Add(New ListItem("Default", "c_msg_default"))
    6.         lstMessage.Items.Add(New ListItem("Message1", "c_msg_custom1"))
    7.         lstMessage.Items.Add(New ListItem("Message2", "c_msg_custom2"))
    8.  
    9.    oSQLConn.ConnectionString = "Data Source=(local);" & _
    10.                 "Initial Catalog=KIT;" & _
    11.                 "Integrated Security=SSPI"
    12.         oSQLConn.Open()
    13.  
    14.         Dim imessage = lstMessage.SelectedItem.Value
    15.         Dim cmdselect As SqlCommand
    16.         Dim sSQL As New StringBuilder ' imports system.text
    17.  
    18.         sSQL.Append("SELECT " & lstMessage.SelectedItem.Value & _
    19.                           " FROM tb_comp_detail" & _
    20.                           " WHERE(c_companycode = '" & lblUser.Text & "')")
    21.  
    22.         cmdselect = New SqlCommand(sSQL.ToString, oSQLConn)
    23.         txtMessageReply.Text = cmdselect.ExecuteScalar().ToString()
    24.  
    25.         oSQLConn.Close()
    26.      
    27.      
    28.  
    29.     End Sub

    i know that this does in fact work because i have a datagrid with a select function that does something or other,
    my drop down fills with the above three fields,
    when the page is loaded the default message is displayed in the textbox as expected
    if i change the index of my drop down nothing happens

    but
    i know it works because if i choose a different option from my drop down and then press select on my datagrid the textbox is filled as expected

    i thin k im so close to getting this working and i know its to do with the postback thingy - lol

    i have tried the selected index changed function and put the code in there
    but it doesnt seem to be dynamic
    if i choose a new item in the drop down the textbox should fill immediately
    it works 60% of the time, all the time.

  19. #19

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    i think the advice you have given me earlier is working but my settings are not right,
    ill show you what i have and tell you what it does

    all in code behind file


    VB Code:
    1. If Not IsPostBack Then
    2.             bindmessagechoice()
    3.         End If
    .................

    VB Code:
    1. Public Sub bindmessagechoice()
    2.  
    3.      
    4.  
    5.         lstMessage.Items.Add(New ListItem("Default", "c_msg_default"))
    6.         lstMessage.Items.Add(New ListItem("Message1", "c_msg_custom1"))
    7.         lstMessage.Items.Add(New ListItem("Message2", "c_msg_custom2"))
    8.  
    9.    oSQLConn.ConnectionString = "Data Source=(local);" & _
    10.                 "Initial Catalog=KIT;" & _
    11.                 "Integrated Security=SSPI"
    12.         oSQLConn.Open()
    13.  
    14.         Dim imessage = lstMessage.SelectedItem.Value
    15.         Dim cmdselect As SqlCommand
    16.         Dim sSQL As New StringBuilder ' imports system.text
    17.  
    18.         sSQL.Append("SELECT " & lstMessage.SelectedItem.Value & _
    19.                           " FROM tb_comp_detail" & _
    20.                           " WHERE(c_companycode = '" & lblUser.Text & "')")
    21.  
    22.         cmdselect = New SqlCommand(sSQL.ToString, oSQLConn)
    23.         txtMessageReply.Text = cmdselect.ExecuteScalar().ToString()
    24.  
    25.         oSQLConn.Close()
    26.      
    27.      
    28.  
    29.     End Sub

    i know that this does in fact work because i have a datagrid with a select function that does something or other,
    my drop down fills with the above three fields,
    when the page is loaded the default message is displayed in the textbox as expected
    if i change the index of my drop down nothing happens

    but
    i know it works because if i choose a different option from my drop down and then press select on my datagrid the textbox is filled as expected

    i thin k im so close to getting this working and i know its to do with the postback thingy - lol

    i have tried the selected index changed function and put the code in there
    but it doesnt seem to be dynamic
    if i choose a new item in the drop down the textbox should fill immediately
    it works 60% of the time, all the time.

  20. #20

    Thread Starter
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: populate drop down from multiple columns

    thank you acony bear all for your replyas mendhak and aconybear

    thanks acony bear for your quick replys and you patience,
    what ive actually done now is put a button beneath my saying use message
    this actually has benifits for me as the textbox can be left then blank for people to write thier own messages

    the code that has resolved this problem is aconybears code above there fore i shall not repost it,

    once again thanks
    i really appreciate it
    it works 60% of the time, all the time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width