dysasha
asked on
listbox1.selecteditem.tostring question
when using this code:
label3.text = listbox1.selecteditem.tost ring
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_SelectedIndexChan ged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChan ged
Try
Label3.Text = ListBox1.SelectedItem.ToSt ring
Catch ex As Exception
MessageBox.Show(ex.ToStrin g)
End Try
End Sub
the error is as follows:
system.nullrefernceexcepti on:object refernce not set to an instance of an object.
at purchase_orders.jobsedit.l istbox1_se lectedinde xchanged(o bject 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
label3.text = listbox1.selecteditem.tost
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_SelectedIndexChan
Try
Label3.Text = ListBox1.SelectedItem.ToSt
Catch ex As Exception
MessageBox.Show(ex.ToStrin
End Try
End Sub
the error is as follows:
system.nullrefernceexcepti
at purchase_orders.jobsedit.l
what i want to accomplish, is when a user clicks on an item in the listbox, the label displays the text from the listbox
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.
ASKER
heres the code thats sets the selectcommand and fills the dataset for the listbox (the listbox is bound to dsjoblist21)
Private Sub ComboBox1_SelectedIndexCha nged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexCha nged
Dim selitem As String
selitem = ComboBox1.Text
SqlDataAdapter2.SelectComm and.Comman dText = "SELECT JobNumber FROM JobNumbers WHERE (Project = N'" + selitem + "') ORDER BY JobNumber"
DsJoblist21.Clear()
SqlConnection1.Close()
SqlConnection1.Open()
SqlDataAdapter2.Fill(DsJob list21)
SqlConnection1.Close()
End Sub
i did set the datasource and datamember, but i did it in the listbox properties instead of via code
Private Sub ComboBox1_SelectedIndexCha
Dim selitem As String
selitem = ComboBox1.Text
SqlDataAdapter2.SelectComm
DsJoblist21.Clear()
SqlConnection1.Close()
SqlConnection1.Open()
SqlDataAdapter2.Fill(DsJob
SqlConnection1.Close()
End Sub
i did set the datasource and datamember, but i did it in the listbox properties instead of via code
ASKER
also, i treid adding this to the on load:
ListBox1.DisplayMember = "Text"
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_SelectedIndexChan ged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChan ged
Try
if (Listbox1.selectedindex <> -1) then
Label3.Text = ListBox1.SelectedItem.ToSt ring
End If
Catch ex As Exception
MessageBox.Show(ex.ToStrin g)
End Try
End Sub
Private Sub ListBox1_SelectedIndexChan
Try
if (Listbox1.selectedindex <> -1) then
Label3.Text = ListBox1.SelectedItem.ToSt
End If
Catch ex As Exception
MessageBox.Show(ex.ToStrin
End Try
End Sub
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?
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!
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!
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
huh? i cant locate this property
Its a behaviour of the listbox. You will find it in the properties dialog (window) of the listbox.
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.
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.
ASKER
autopostback is not a memeber of system.windows.forms.listb ox?
ASKER
perhaps you are referencing something i am not?
Imports System.Windows.Forms
Imports System.Data are the imports ive used...
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.
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.
ASKER
i see :( i was hoping i had found a solution
any other thoughts?
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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dim theListBox As ListBox = DirectCast(sender, ListBox)
Label3.Text = theListBox.GetItemText(the ListBox.Se lectedItem )
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?
Label3.Text = theListBox.GetItemText(the
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?
ASKER
thanks again!
ASKER
system.data.datarowview
instead of needed text