Thursday, March 29, 2012

Setting no value for a new ListItem of a dropdownbox

Hi,
I am trying to use 2 dropdownlist for a query. The query works over an indexsearch.

As long as I select a value of both dropdownlists, I get a result (when a record was found in the DB).

But when I leave one Dropdown list blank, I do not get any results.

How do I set the value for the blank ListItem, which I have added to the dropdownlists to a value (or better no value) to achieve this result?

My dropdownlists are build like on page_Load:

...
DDLCountry.DataValueField="Country_ID";
DDLCountry.DataBind();
DDLCountry.Items.Insert(0, new ListItem("",""));
reader.Close();
...

My Query after the submit button was pressed:
...
string query = " SELECT * FROM Main WHERE";
query += " AND Country_ID = " + DDLCountry.SelectedItem.Value + "";
query += " AND Engineer_ID = " + DDLEngineer.SelectedItem.Value + "";
...

Thank you for your help in advance.

GorginoTo set a value do this:

ddl.Items.Add( new ListItem(dr["Name"].ToString(), dr["InfoID"].ToString())));

this is filling from a Datareader but you get the idea.

Sam
Hi Gorginio,

If I understood your question correctly, are you looking for something like this:


System.Text.StringBuilder query = new System.Text.StringBuilder();

string country = this.DDLCountry.SelectedItem.Value;
string engineer = this.DDLEngineer.SelectedItem.Value;
string where = (country != "" || engineer != "" ? " WHERE " : "");

country = (country != "" ? "Country_ID = '" + country + "'" : "");
engineer = (engineer != "" ? "Engineer_ID = '" + engineer + "'" : "");

query.Append("SELECT * FROM Main");
query.Append(where);
query.Append(country);
query.Append((country !="" && engineer != "" ? " AND " : ""));
query.Append(engineer);

//implement using query.ToString();


This will result in a Select statement based off the items selected in the dropdownlists.

Hope this helps!

Jason
Thank you for your help.

That is basically what I needed, but I have to make it for 4 dropdownlists, 6 textboxes and 7 checkboxes.

But with your support, I should be able...

Greetings

Gorgino

0 comments:

Post a Comment