|
-
Jun 21st, 2007, 09:22 AM
#1
Thread Starter
Frenzied Member
[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:
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.
-
Jun 21st, 2007, 09:34 AM
#2
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
 
-
Jun 21st, 2007, 09:42 AM
#3
Re: [02/03] ListBox errors
If I'm not misinterpreting something, isn't it just:
vb.net Code:
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.
-
Jun 21st, 2007, 09:49 AM
#4
Thread Starter
Frenzied Member
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.
-
Jun 21st, 2007, 09:53 AM
#5
Hyperactive Member
Re: [02/03] ListBox errors
vb.net Code:
Me.txtInfo.Text = Me.lstUsers.SelectedItem.ToString
-
Jun 21st, 2007, 09:58 AM
#6
Thread Starter
Frenzied Member
Re: [02/03] ListBox errors
-
Jun 21st, 2007, 10:05 AM
#7
Hyperactive Member
Re: [02/03] ListBox errors
can you copy the whole code ?
try:
vb.net Code:
Me.txtInfo.Text = Me.lstUsers.Items.Item(lstUsers.SelectedIndex).ToString
-
Jun 21st, 2007, 10:21 AM
#8
Thread Starter
Frenzied Member
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:
Private Sub getEmployeeInfo(ByVal firstName As String, ByVal lastName As String)
Dim sqlConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbDirectory & ";User Id=admin;Password=;"
Dim sqlConn As New OleDb.OleDbConnection(sqlConnString)
Dim sqlStatement As String = "SELECT First_Name, Last_Name FROM Directory WHERE First_Name = '" & firstName & "' AND Last_Name = '" & lastName & "'"
Dim sqlDA As New OleDb.OleDbDataAdapter
Dim sqlCommand As New OleDb.OleDbCommand(sqlStatement, sqlConn)
Dim DS As New DataSet
Dim DT As New DataTable
Try
sqlConn.Open()
sqlDA.SelectCommand = sqlCommand
DS.Tables.Add(DT)
sqlDA.Fill(DT)
If DS.Tables(0).Rows.Count = 0 Then
txtInfo.Text = "Employee not found"
Exit Sub
End If
DS.Tables(0).Columns.Remove("UserID")
txtInfo.Text = txtInfo.Text & "Employee Information for " & firstName & " " & lastName & vbNewLine & vbNewLine
Dim counter As Integer
For Each row As DataRow In DS.Tables(0).Rows
For counter = 0 To DS.Tables(0).Columns.Count - 1
txtInfo.Text = txtInfo.Text & DS.Tables(0).Columns(counter).ColumnName & ": " & row.Item(counter).ToString & vbNewLine
Next
Next
Catch ex As Exception
Response.Write("<script>alert(""Error connecting to the database. Please contact your administrator." & vbNewLine & ex.Message & """);</script>")
Finally
sqlConn.Close()
sqlCommand.Dispose()
End Try
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
txtInfo.Text = ""
Dim name(1) As String
Dim firstName As String
Dim lastName As String
'It fails here at this code.
name = Split(lstUsers.SelectedItem.Text, " ")
'txtInfo.Text = lstUsers.SelectedItem.Text
firstName = name(0)
lastName = name(1)
getEmployeeInfo(firstName, lastName)
End Sub
-
Jun 21st, 2007, 10:31 AM
#9
Hyperactive Member
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 .
-
Jun 21st, 2007, 12:28 PM
#10
Re: [02/03] ListBox errors
 Originally Posted by nmadd
If I'm not misinterpreting something, isn't it just:
vb.net Code:
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
 
-
Jun 21st, 2007, 01:05 PM
#11
Thread Starter
Frenzied Member
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.
-
Jun 21st, 2007, 01:09 PM
#12
Hyperactive Member
Re: [02/03] ListBox errors
I would try to get a record by hardcoding it.
vb.net Code:
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.
-
Jun 21st, 2007, 02:13 PM
#13
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
 
-
Jun 21st, 2007, 02:17 PM
#14
Thread Starter
Frenzied Member
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.
-
Jun 21st, 2007, 02:34 PM
#15
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:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.txtInfo.Text = CStr(Me.ListBox1.SelectedItem)
End Sub
if you call the method in your custom sub or function then this should take a care or it.
vb Code:
If Me.ListBox1.Items.Count > 0 AndAlso Me.ListBox1.SelectedItems.Count > 0 Then
Me.txtInfo.Text = CStr(Me.ListBox1.SelectedItem)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|