While working with various listboxes in the past, I mostly had hard-coded text & values.
Lately, I have been working with "unbound" listboxes and had data populated from a sql query & databind.
I have also am getting these listboxes to do various actions on a button click event using the:
'" + ListBox1.SelectedItem.Text + "'"
I used this method as a means to auto-populate user's multiple choices, rather than being open to interpretation by letting the user input option via a textbox.
Using this method allows for only available options to display.
Having said that, the '" + ListBox1.SelectedItem.Text + "'" method, seems to only have worked on
queries that used the method for DELETE or UPDATE methods. The issue is it is not working corrcetly with SELECT.
I used this same style of code to create the logical answer:
Me.ListBox2.Items.Clear()
Dim mySqlString As New StringBuilder()
mySqlString.Append("SELECT fieldname1")
mySqlString.Append("FROM tablename WHERE fieldname2='" + ListBox1.SelectedItem.Text + "'")
mySqlString.Append("ORDER BY fieldname1 ASC")
Me.ListBox2.DataSource = CType(DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), Nothing), IDataReader)
Me.ListBox2.DataBind()
I have been told that this code looks correct, yet it does not work correctly.
As a control to testing, I setup the same event, but replaced the '" + ListBox1.SelectedItem.Text + "' with a hard-coded value, and it works!
By doing this, I verified that the query is working correctly. Nothing changes between the two experiments other than the:
...fieldname2='" + ListBox1.SelectedItem.Text + "'")
&
...fieldname2=value")
Therfore, it seems to be an issue with the syntax.
I also tried:
'" + ListBox1.SelectedItem.Value + "'"
'" + ListBox1.SelectedValue + "'"
And Yes, I already am using correct DataValueField name
(which would traditionally cause listbox to be blank, but this is assigned and correct.....?)
The overall goal is to have:
1.) ListBox1 populate list. (which is working correctly)
2.) Button1, when clicked, will run the blue code above, and dynamically include ListBox1's selection into query & populateListBox2 in step #3.
3.) ListBox2 will populate list accordingly (which is not working)
4.) Repeat etc...
Currently, step #2 causes the browser to think for a second and return to same state. ListBox2 is empty.
What syntax will I need to achieve this, so that the Listbox2 calculates the SELECT sql query using ListBox1 selected option?
And/or why wouldn't the traditional '" + ListBox1.SelectedItem.Text + "'" work?
Feedback Appreciated,
- machina12