[RESOLVED] How to? fill the list box from a particular table based on selection?
I am wanting to select a table for the FROM part:
categoryComm = new SqlCommand("SELECT ListRelationID, ListRelation FROM ListRelationTbl", conn);
based on what value the user selects in a listbox.
How can I replace ListRelationTbl" with the value from the listbox?
" + listboxtableslct.Value + "", conn);
conn.Open();
// Create command to read the categories
reader = categoryComm.ExecuteReader();
// Populate the list of categories
categoryLst.DataSource = reader;
categoryLst.DataValueField = "ListRelationID";
categoryLst.DataTextField = "ListRelation";
Thank you so much in advance.
Re: How to? fill the list box from a particular table based on selection?
This might be it, not sure though. Testing it now.
SqlCommand comm = new SqlCommand("SELECT * FROM '%" + DropDownList1.SelectedIndex + "%'", conn);
What does '% do?
EDIT:
Nope didn't work. I get the error:
System.Data.SqlClient.SqlException: Incorrect syntax near '%1%'.
at the line 28:
Line 27: SqlCommand comm = new SqlCommand("SELECT * FROM '%" + DropDownList1.SelectedIndex + "%'", conn);
Line 28: SqlDataReader reader = comm.ExecuteReader();
Re: How to? fill the list box from a particular table based on selection?
I have a table that has an id for each category of tables. The id in this table has an associated categoryID. The categoryID is a foreign key for all the other tables which are categories(tables) of items. What I wanted to try to do was allow a user to select a category from the listbox to choose what category of items the user would like to view. Call up everything from a table based on what the listbox's selection value is. I am having a problem trying to figure out the sql for this.
I tried to use parameters:
SqlCommand comm = new SqlCommand("SELECT * FROM categoryTbl where categoryID = @tableID", conn);
comm.Parameters.AddWithValue("@tableID", DropDownList1.SelectedIndex);
But I don't believe that gives me access to the other tables data. I believe there should be another nested selection. I used something like this before but I forgot how to use it:
SqlCommand comm = new SqlCommand("SELECT * FROM UsrTbl, RelativeTable, IP_Table WHERE RelativeTable.UserID IN (SELECT UserID FROM UsrTbl WHERE UserName = @usrnmeLbl)", conn);
comm.Parameters.AddWithValue("@usrnmeLbl", usrNmeLbl.Text);
I don't know how I can make this work with what I have here. I have forgotten what I was trying to do in that second query.
Thanks so much in advance for your time and instruction.
Re: How to? fill the list box from a particular table based on selection?
SqlCommand comm = new SqlCommand("SELECT * FROM " + DropDownList1.SelectedValue, conn);
Seems like something like that should work, but I don't know if I need something like selectedindex.selection or some other property.
If the dropdown list has the names of the tables as datafields or datavalues, shouldn't this work out?
Re: How to? fill the list box from a particular table based on selection?
Got this to work:
SqlCommand comm = new SqlCommand("SELECT * FROM inventoryTbl WHERE categoryID = " + DropDownList1.SelectedValue, conn);