Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > listbox selected index changed
 

listbox selected index changed

Hiall,
here is my code it works fine this way:-

If ListBox1.SelectedIndex > 0 Then

Dim dr As SqlDataReader

Dim cmd As New SqlCommand

Dim sql As String = "select name,price,[open] from lotterysale where Name = '" & ListBox1.SelectedValue & "'"

cmd.CommandText = sql

cmd.CommandType = CommandType.Text

cmd.Connection = con

dr = cmd.ExecuteReader

dr.Read()

If dr.HasRows Then

TxtName.Text = dr(0)

Txtprice.Text = dr(1)

Txtopen.Text = dr(2)

End If

dr.Close()

cmd.Dispose()

End If



But if i removeIf ListBox1.SelectedIndex > 0 It gives me error:-
Operator '&' is not defined for string "select name,price,[open] from lo" and type 'DataRowView'.

I am just stuck with it please help!

schauhan  Monday, December 15, 2008 8:41 PM
Well why would you want to remove that line? If nothing is selected, then the SelectedIndex will be -1 and the SelectedValue will be undefined. You should always check theSelectedIndex, but you should actually be checking that it's >= 0.
~~Bonnie Berent [C# MVP]
BonnieB  Tuesday, December 16, 2008 5:18 AM
Hi Schauhan,

try to separate the string assignment from the declaration of the sql identifier.
Anyway, I'd suggest to use the StringBuilder class to concatenate strings.



regards,
franking


franking  Monday, December 15, 2008 9:33 PM
Well why would you want to remove that line? If nothing is selected, then the SelectedIndex will be -1 and the SelectedValue will be undefined. You should always check theSelectedIndex, but you should actually be checking that it's >= 0.
~~Bonnie Berent [C# MVP]
BonnieB  Tuesday, December 16, 2008 5:18 AM
Hi Schauhan,

Firstly, if you'd like to remove the "if"condition line of code, you should remove the "End If" at the end as well.

Secondly, I don't think you should remove the "if" conditionfrom your code because you need to check whether there's an item selected in the ListBox before you use the ListBox's SelectedValue property.

Sincerely,
Linda Liu
Linda Liu  Wednesday, December 17, 2008 2:46 AM
A repeat of what I just said, more or less. =0(
~~Bonnie Berent [C# MVP]
BonnieB  Wednesday, December 17, 2008 4:32 AM

You can use google to search for other answers

Custom Search

More Threads

• How am I connecting to two tables according to personal parameters?
• Multiple Controls for editing a Single Column of Datagridview Control
• hide rows in datagridview
• A problem with ComboBox Column
• where to open my connection object?
• How Do I switch a datasource on a combobox "on the fly"?
• Combobox cell alignment in a DataGridView
• Passing value from another form to fill datagridview
• Can I bind the CheckedState of a CheckedListBox to my objects?
• Prompt when attempting to add duplicates