Visual Studio .NET - dropdownlist - Asked By Austine on 04-Mar-10 03:02 AM

 Hi ,I have up to Twenty Dropdownlist in my form.
I want to detect the ones that the user selected and search the database and display the result in a gridview based on the selecetd value.
 i found the answer using the nested if Statements. It works fine but the i have more that 750,000 lines of code and visual studio is telling me that SystemOut Ofmemory Exception.
I  dont know what to do.
I want to ask if there is any other way i can detect the selected item and search the Database  based on the selected item.

Please Help. I have spent days trying to figure this out.

Austine
Ryan C. replied to Austine on 04-Mar-10 03:27 AM
On the submit event of your button, you can check each dropdown if the SelectedIndex is greater than zero, and then add the value to an ArrayList. Then after loop through the arraylist to view the selected items.

e.g.

ArrayList myArray = new ArrayList();
if(dropdown1.SelectedIndex >0)
{
myArray.add(dropdown1.SelectedValue);
}
Austine replied to Ryan C. on 04-Mar-10 03:40 AM
This is the dropdownlist and it looks like this .

protected void Page_Load(object sender, EventArgs e)

{


if (!Page.IsPostBack)

{

grdRSS.Visible =

true;

drpAccommodationClass.DataBind();

drpBudgetLevel.DataBind();

drpCatering.DataBind();

drpCountry.DataBind();

drpEssentialFacilities.DataBind();

drpGroupSize.DataBind();

drpMalariaRisk.DataBind();

drpParkType.DataBind();

drpPrimarySafariActivity.DataBind();

drpPrimaryTourismRoute.DataBind();

drpSafariExperience.DataBind();

drpSecondaryTourismRoute.DataBind();

drpSpecialistSpecie.DataBind();

drpSpecialRequest.DataBind();

drpStarGrading.DataBind();

drpSunRating.DataBind();

drpTravelPreference.DataBind();



}

}



protected void Button1_Click(object sender, EventArgs e)

{


if (drpCountry.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpPrimaryTourismRoute.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpSecondaryTourismRoute.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpParkType.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpBudgetLevel.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpCatering.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpAccommodationClass.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpPrimarySafariActivity.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpSafariExperience.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpSpecialistSpecie.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpTravelPreference.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpMalariaRisk.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpEssentialFacilities.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpSpecialRequest.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpGroupSize.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpSunRating.SelectedValue.ToString().Equals("Not Specified"))

{


if (drpStarGrading.SelectedValue.ToString().Equals("Not Specified"))

{
So now i first ask ask all before starting to make the decisions.

Please i want to ask you what will happen to all the dropdownlist  Using the array.
Please help .
I do appreciate your reply but am stiil young in asp.net and C# arena.

Ryan C. replied to Austine on 04-Mar-10 03:43 AM
It really depends what you want to do with the results. Do you want to create a SELECT query for a database?
Austine replied to Ryan C. on 04-Mar-10 04:08 AM
Yes I have a select Querry that looks like this.

#region


BusTrainStarGradingBusinessCenterGymBusinessCenter()

private void BusTrainStarGradingBusinessCenterGymBusinessCenter()

{


try

{


string connectionString;

connectionString = (

"server=196.35.70.190;database=GameLodgeIndex;uid=jake;pwd=austine8877;");


string selectSQL = "SELECT LodgeName,SunRayRating FROM SearchModels where BusTrain = '" + drpBusTrain.SelectedValue + "' and StarGrading = '" + drpStarGrading.SelectedValue + "' and BusinessCenter = '" + drpBusinessCentre.SelectedValue + "' and Gym = '" + drpGym.SelectedValue + "' and Boma = '" + drpBoma + "' and BusinessCenter = '" +drpBusinessCentre.SelectedValue+ "'Order By SunRayRating Desc";

SqlConnection con = new SqlConnection(connectionString);

con.Open();


SqlCommand cmd = new SqlCommand(selectSQL, con);

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

// Fill the DataSet.


DataSet ds = new DataSet();

adapter.Fill(ds,

"SearchModels");

// Perform the binding.

grdRSS.DataSource = ds;

grdRSS.DataBind();



lblWelcome.Text =

"Welcome to Game Lodge Index Search engine. Here are you results :";

Panel2.Enabled =

false;

Panel2.Visible =

false;

Button1.Enabled =

false;

Button2.Enabled =

true;

lblWelcome.Text =

"Welcome to game lodge Index. Here are your search results";

con.Close();

}


catch

{

lblWelcome.Text =

" Please try Again with different Criteria";

}

}

#endregion




If the staements is true ,It callss a specific methods or it calls a specific method if it is no.


Please I do appreciate your reply and keep up the Good work.

Austine replied to Austine on 04-Mar-10 04:11 AM
Hi Please can you remove the Username and Password for me i made a mistake before posting online.

God bless you and our team.
Ryan C. replied to Austine on 04-Mar-10 04:12 AM
What you can do is something like this:

string filter="";
string selectSQL = "SELECT LodgeName,SunRayRating FROM SearchModels where " +filter " Order By SunRayRating Desc";

if (drpCountry.SelectedValue.ToString().Equals("Not Specified"))
filter += " country" +drpCountry.SelectedValue.ToString();

...etc

Austine replied to Ryan C. on 04-Mar-10 04:19 AM
Do you mean after this
ArrayList myArray = new ArrayList();
if(dropdown1.SelectedIndex >0)
{
myArray.add(dropdown1.SelectedValue);
}

I should use the the select Querry?.
Ryan C. replied to Austine on 04-Mar-10 04:22 AM
Well there is no need of an array in your case. I only mentioned it in my first pose because I did not know what you were going at.

All you have to do is build your "filter" for your select query using the if statements, as I shown earlier. You might get a problem with an extra "and" or so, but in this case jsut debug your program to see where its happening, and I bet you'll be able to figure how to solve it.
Austine replied to Ryan C. on 04-Mar-10 04:26 AM
You have answered me i will ues the filter to do  just that.
Thanks and God Bless You.
Austine.


Huggy Bear replied to Austine on 04-Mar-10 04:37 AM
First of all why do you have 7, 50, 000 lines in your page. It is not a normal thing its simple huge. I would definitely recommend you create common functions if the functionality is duplicated. This would really decrease the number of lines of code on your page.

1. Creating the common functions should be your first job because 7, 50, 000 is unacceptable at any point of time.
2. After doing that you could simply use a string builder and append the WHERE clause parameters optionally as shown below

string query = "SELECT LodgeName,SunRayRating FROM SearchModels";
StringBuilder queryBuilder = new StringBuilder();
if(!drpBusTrain.SelectedValue.ToString().Equals("Not Specified"))
{
    if(queryBuilder.ToString().Equals(String.Empty))
        queryBuilder.Append("BusTrain = '" + drpBusTrain.SelectedValue+ "'");
    else
        queryBuilder.Append("and BusTrain = '" + drpBusTrain.SelectedValue+ "'");
 
}
//Do this for all the dropdownlist
//finally merge query and querybuilder
string query = string.Format("{0} {1}", query, !queryBuilder.Equals(String.Empty) ? string.Format("WHERE {0}", queryBuilder.ToString()) : String.Empty);

Austine replied to Huggy Bear on 04-Mar-10 04:45 AM
Hi Surely You have answered my Question.
May the almigthy God Bless You and all your team.

Thanks a lot.
Austine
Huggy Bear replied to Austine on 04-Mar-10 05:08 AM
Thanks Austine.