Results 1 to 15 of 15

Thread: [02/03] ListBox errors

  1. #1

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    [02/03] ListBox errors

    I'm working on an ASP.net page in VS.net 2003 and I am stuck on something...

    I populate a listbox from data in an access table and I would like to get the text of the selected item in the listbox. Once I get the text, I would then display it on a textbox. Seems simple to me... here is the code:

    vb Code:
    1. txtInfo.Text = lstUsers.Items(lstUsers.SelectedItem.Value).Text

    I keep getting an object reference not set to instance of an object. I know i'm not instantiating something.. am I missing something? Thanks.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [02/03] ListBox errors

    I think that

    lstUsers.Items(lstUsers.SelectedIndex).text

    would work better than the selectedItem.Value, but I'm not sure that that could cause a problem. Haven't dealt with ASP, but what I would normally do in a case like this would be to highlight different pieces of the code and use Shift+F9. The one that is Nothing is the one that is a problem.
    My usual boring signature: Nothing

  3. #3
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [02/03] ListBox errors

    If I'm not misinterpreting something, isn't it just:

    vb.net Code:
    1. Me.txtInfo.Text = Me.lstUsers.SelectedItem.Text

    This seems to work for me.

    EDIT: Sorry, I read DropDownList for some unknown reason. I don't know if this works on a ListBox but imagine it does.
    Last edited by nmadd; Jun 21st, 2007 at 10:07 AM.

  4. #4

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] ListBox errors

    I tried using lstUsers.SelectedIndex and I get the same error... and lstUsers.SelectedItem.Value = Nothing (or lstUsers.SelectedIndex = -1). I'm confused as to why it would be doing this...

    Same thing with lstUsers.SelectedItem.Text.

  5. #5
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [02/03] ListBox errors

    vb.net Code:
    1. Me.txtInfo.Text = Me.lstUsers.SelectedItem.ToString

  6. #6

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] ListBox errors

    Nope... same thing.

  7. #7
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [02/03] ListBox errors

    can you copy the whole code ?

    try:
    vb.net Code:
    1. Me.txtInfo.Text = Me.lstUsers.Items.Item(lstUsers.SelectedIndex).ToString

  8. #8

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] ListBox errors

    In other forms, I have used lstUsers.SelectedItems.Text and it works fine. I don't know why it is saying the object is not instantiated.

    Here is the code:
    vb Code:
    1. Private Sub getEmployeeInfo(ByVal firstName As String, ByVal lastName As String)
    2.         Dim sqlConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbDirectory & ";User Id=admin;Password=;"
    3.         Dim sqlConn As New OleDb.OleDbConnection(sqlConnString)
    4.         Dim sqlStatement As String = "SELECT First_Name, Last_Name FROM Directory WHERE First_Name = '" & firstName & "' AND Last_Name = '" & lastName & "'"
    5.         Dim sqlDA As New OleDb.OleDbDataAdapter
    6.         Dim sqlCommand As New OleDb.OleDbCommand(sqlStatement, sqlConn)
    7.         Dim DS As New DataSet
    8.         Dim DT As New DataTable
    9.  
    10.         Try
    11.             sqlConn.Open()
    12.  
    13.             sqlDA.SelectCommand = sqlCommand
    14.  
    15.             DS.Tables.Add(DT)
    16.             sqlDA.Fill(DT)
    17.  
    18.             If DS.Tables(0).Rows.Count = 0 Then
    19.                 txtInfo.Text = "Employee not found"
    20.                 Exit Sub
    21.             End If
    22.  
    23.             DS.Tables(0).Columns.Remove("UserID")
    24.  
    25.             txtInfo.Text = txtInfo.Text & "Employee Information for " & firstName & " " & lastName & vbNewLine & vbNewLine
    26.  
    27.             Dim counter As Integer
    28.  
    29.             For Each row As DataRow In DS.Tables(0).Rows
    30.                 For counter = 0 To DS.Tables(0).Columns.Count - 1
    31.                     txtInfo.Text = txtInfo.Text & DS.Tables(0).Columns(counter).ColumnName & ": " & row.Item(counter).ToString & vbNewLine
    32.                 Next
    33.             Next
    34.  
    35.         Catch ex As Exception
    36.             Response.Write("<script>alert(""Error connecting to the database. Please contact your administrator." & vbNewLine & ex.Message & """);</script>")
    37.  
    38.         Finally
    39.             sqlConn.Close()
    40.             sqlCommand.Dispose()
    41.         End Try
    42. End Sub
    43.  
    44.  
    45.     Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    46.         txtInfo.Text = ""
    47.  
    48.         Dim name(1) As String
    49.         Dim firstName As String
    50.         Dim lastName As String
    51.  
    52.         'It fails here at this code.
    53.         name = Split(lstUsers.SelectedItem.Text, " ")
    54.  
    55.         'txtInfo.Text = lstUsers.SelectedItem.Text
    56.  
    57.         firstName = name(0)
    58.         lastName = name(1)
    59.  
    60.         getEmployeeInfo(firstName, lastName)
    61.     End Sub

  9. #9
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [02/03] ListBox errors

    A couple things..
    when is the list box populated with items ? (runtime, design time)
    you should check to see if the selectedindex > -1 to make sure that something is clicked .

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [02/03] ListBox errors

    Quote Originally Posted by nmadd
    If I'm not misinterpreting something, isn't it just:

    vb.net Code:
    1. Me.txtInfo.Text = Me.lstUsers.SelectedItem.Text

    This seems to work for me.

    EDIT: Sorry, I read DropDownList for some unknown reason. I don't know if this works on a ListBox but imagine it does.

    Whoops, I regressed back to VB6 for a moment there.



    By the way, have you checked to see what is Nothing? I would expect that it isn't the listbox itself, but it could be Items()
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] ListBox errors

    The data is populated during runtime as I access a MS Access Table to retrieve the records.

    Also, read post #4 as to which is equal to nothing and -1. It sounds like it is not registering the clicks or something is not instantiated properly. I just can't figure out what.

  12. #12
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [02/03] ListBox errors

    I would try to get a record by hardcoding it.

    vb.net Code:
    1. name = Split(lstUsers.Items.Item(0), " ")

    to make sure that the list box has information in it.
    i got the error with an empty list box.

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [02/03] ListBox errors

    Ok, I missed that post #4.

    So if SelectedIndex = -1, then nothing is selected. However, you clearly think that something IS selected....but it's not, so why not?

    This could well have something to do with ASP, which I am not familiar with. My suspicion is that you are not accessing the listbox that you think you are accesssing, but it's hard for me to think of a way that could be happening.

    In general, I like what NPassero suggested, but before you do that, try just tossing in a Messagebox showing the Items.Count. I suspect that you will find the count is 0, but you might find it is some other number, too. The key is to try to figure out what you are accessing. If lstUsers is not Nothing, then there has to be a listbox that has not been clicked.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] ListBox errors

    I've seen some funky things happen with ASP.net where the page will refresh and clear the selected items. When this happens it looks like something was passed but it wasn't. I've only seen that happen during postback, though.

  15. #15
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [02/03] ListBox errors

    Where do you call the method that gets the item? Is it in an event? If it is than have the method in the "SelectedIndexChanged" listbox event handler.
    vb Code:
    1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.         Me.txtInfo.Text = CStr(Me.ListBox1.SelectedItem)
    3.     End Sub
    if you call the method in your custom sub or function then this should take a care or it.
    vb Code:
    1. If Me.ListBox1.Items.Count > 0 AndAlso Me.ListBox1.SelectedItems.Count > 0 Then
    2.             Me.txtInfo.Text = CStr(Me.ListBox1.SelectedItem)
    3.         End If

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