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.
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.
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.
Re: [02/03] ListBox errors
vb.net Code:
Me.txtInfo.Text = Me.lstUsers.SelectedItem.ToString
Re: [02/03] ListBox errors
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
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
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 .
Re: [02/03] ListBox errors
Quote:
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()
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.
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.
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.
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.
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