Link to home
Start Free TrialLog in
Avatar of dysasha
dysasha

asked on

listbox1.selecteditem.tostring question

when using this code:

label3.text = listbox1.selecteditem.tostring

which i have found in a  few examples on the web, doesnt seem to work, the listbox1 is bound to a dataset, everything seem to work great, but i get an error when the form loads and the label doesnt get updated,  heres my full code:

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            Label3.Text = ListBox1.SelectedItem.ToString
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

the error is as follows:

system.nullrefernceexception:object refernce not set to an instance of an object.
at purchase_orders.jobsedit.listbox1_selectedindexchanged(object sender, eventargs e)

what i want to accomplish, is when a user clicks on an item in the listbox, the label displays the text from the listbox
Avatar of dysasha
dysasha

ASKER

with the try method, after the errors (one for each item in the listbox) the label3 displays:

system.data.datarowview

instead of needed text
Can you show the code with wich you binded the listbox to the dataset.  Did you set the Displaymember and Value member properties of the listbox
Check if you fill the listbox in page_load event. It might reinitialize the listbox on and it does not have reference set.
Avatar of dysasha

ASKER

heres the code thats sets the selectcommand and fills the dataset for the listbox (the listbox is bound to dsjoblist21)

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim selitem As String
        selitem = ComboBox1.Text
        SqlDataAdapter2.SelectCommand.CommandText = "SELECT JobNumber FROM JobNumbers WHERE (Project = N'" + selitem + "') ORDER BY JobNumber"
        DsJoblist21.Clear()
        SqlConnection1.Close()
        SqlConnection1.Open()
        SqlDataAdapter2.Fill(DsJoblist21)
        SqlConnection1.Close()
    End Sub


i did set the datasource and datamember, but i did it in the listbox properties instead of via code
Avatar of dysasha

ASKER

also, i treid adding this to the on load:

        ListBox1.DisplayMember = "Text"

Check whether any of the items in the listbox is selected before you assign the value of the selected item to the listbox
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
       Try
        if (Listbox1.selectedindex <> -1) then
          Label3.Text = ListBox1.SelectedItem.ToString
        End If
       Catch ex As Exception
           MessageBox.Show(ex.ToString)
       End Try
End Sub


Avatar of dysasha

ASKER

we, i'm not sure if its a step forward.. but it looks better,  the pop up error disappeared, even if i remove the try/catch statement,  but the label still says:

system.data.datarowview

reguardless of which item i select..

any more thoughts?
I dont know whether this is the solution to your problem or not but this is what I tried on a test page.
The OnSelectedIndexChanged event of the listbox did not fire for me until I changed the autopostback property to "true".
Dont know whether it will work in your existing code but you can give it a shot.
Good Luck!
Avatar of dysasha

ASKER

The OnSelectedIndexChanged event of the listbox did not fire for me until I changed the autopostback property to "true".

huh?  i cant locate this property
Its a behaviour of the listbox. You will find it in the properties dialog (window) of the listbox.
Avatar of dysasha

ASKER

sir, i've looked and looked, and i cant find it, are you by chance using vb.net?  perhaps you could narrow it down a bit, what group are you seeing it in? and what else is near it,  ive expanded all the groups, and i dont see it anywhere
lol :) Alright you can try this on the page_load
sub page_load(sender As Object, e As EventArgs)
  listbox1.autopostback=true
end sub

I am using webmatrix but I believe that VS.Net also has it in its properties window cant be sure though.
Avatar of dysasha

ASKER

autopostback is not a memeber of system.windows.forms.listbox?
Avatar of dysasha

ASKER

perhaps you are referencing something i am not?

Imports System.Windows.Forms
Imports System.Data  are the imports ive used...
You are right! We are not referencing to the same thing. I get a
Namespace or type 'Forms' for the Imports 'System.Windows.Forms' cannot be found.
Let me get back to u if I find something.
I am referencing to System.Web.UI.WebControls.ListBox which I believe is always the default one with my webmatrix pages.
Avatar of dysasha

ASKER

i see :(   i was hoping i had found a solution

any other thoughts?
If you are writting a Web page you should not be using
Imports System.Windows.forms

those are the imports for a Visual basic to run on a windows enviroment.

try to include the imports that farzinm is using instead.
Avatar of dysasha

ASKER

hmm, scroll up a bit, i'm not writing a webpage
just a thought ...maybe you need to add event handlers?
ok ignore my previous comment I dont think you need to add handlers
ASKER CERTIFIED SOLUTION
Avatar of farsight
farsight

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dysasha

ASKER

       Dim theListBox As ListBox = DirectCast(sender, ListBox)
        Label3.Text = theListBox.GetItemText(theListBox.SelectedItem)



was exactly what i was looking for, i wasnt aware of directcast... reccomend any good books that dont prattle on about hello world and filling a dataset?  something more advanced, that doesnt assume you know vb6?
Avatar of dysasha

ASKER

thanks again!