0

I Have a ListView and a Database with two tables, i have a Linq search query, when a person selects groom radio button and hits search it has to show data from Male table into the ListView and when person selects bride radio button and hits search it should show data from Female table in the ListView how can i manage it

protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        using (WebTechManiaDataContext Data = new WebTechManiaDataContext())
        {
            string Education = DropDownListEducation.SelectedItem.Text.ToString();
            string MaritalStatus = DropDownListMaritalStatus.SelectedItem.Text.ToString();

            if (RadioButtonGroom.Checked == true | RadioButtonBride.Checked == false)
            {
                if ((DropDownListFrom.SelectedItem.Text == "21") && (DropDownListTo.SelectedItem.Text == "22"))
                {
                    var FoundGroom = Data.Males.Where(Males => (Males.Age == 21 || Males.Age == 22) && (Males.Education == Education || Education.Length == 0) && (Males.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
                    if (!FoundGroom.Any())
                    {
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewResult.DataSourceID = "LinqDataSourceGroom";
                        ListViewResult.DataSource = FoundGroom;
                        ListViewResult.DataBind();
                    }
                }
                else if ((DropDownListFrom.SelectedItem.Text == "21") && (DropDownListTo.SelectedItem.Text == "23"))
                {
                    var FoundGroom = Data.Males.Where(Males => (Males.Age == 21 || Males.Age == 22 || Males.Age == 23) && (Males.Education == Education || Education.Length == 0) && (Males.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
                    if (!FoundGroom.Any())
                    {
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewResult.DataSourceID = "LinqDataSourceGroom";
                        ListViewResult.DataSource = FoundGroom;
                        ListViewResult.DataBind();
                    }
                }

                else if ((DropDownListFrom.SelectedItem.Text == "21") && (DropDownListTo.SelectedItem.Text == "24"))
                {
                    var FoundGroom = Data.Males.Where(Males => (Males.Age == 21 || Males.Age == 22 || Males.Age == 23 || Males.Age == 24) && (Males.Education == Education || Education.Length == 0) && (Males.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
                    if (!FoundGroom.Any())
                    {
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewResult.DataSourceID = "LinqDataSourceGroom";
                        ListViewResult.DataSource = FoundGroom;
                        ListViewResult.DataBind();
                    }
                }
            }
            else if (RadioButtonBride.Checked == true | RadioButtonBride.Checked == false)
            {
                if ((DropDownListFrom.SelectedItem.Text == "21") && (DropDownListTo.SelectedItem.Text == "22"))
                {
                    var FoundBride = Data.Females.Where(Females => (Females.Age == 21 || Females.Age == 22) && (Females.Education == Education || Education.Length == 0) && (Females.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
                    if (!FoundBride.Any())
                    {
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewResult.DataSourceID = "LinqDataSourceBride";
                        ListViewResult.DataSource = FoundBride;
                        ListViewResult.DataBind();
                    }
                }
                else if ((DropDownListFrom.SelectedItem.Text == "21") && (DropDownListTo.SelectedItem.Text == "23"))
                {
                    var FoundBride = Data.Females.Where(Females => (Females.Age == 21 || Females.Age == 22 || Females.Age == 23) && (Females.Education == Education || Education.Length == 0) && (Females.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
                    if (!FoundBride.Any())
                    {
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewResult.DataSourceID = "LinqDataSourceBride";
                        ListViewResult.DataSource = FoundBride;
                        ListViewResult.DataBind();
                    }
                }

                else if ((DropDownListFrom.SelectedItem.Text == "21") && (DropDownListTo.SelectedItem.Text == "24"))
                {
                    var FoundBride = Data.Females.Where(Females => (Females.Age == 21 || Females.Age == 22 || Females.Age == 23 || Females.Age == 24) && (Females.Education == Education || Education.Length == 0) && (Females.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
                    if (!FoundBride.Any())
                    {
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewResult.DataSourceID = "LinqDataSourceBride";
                        ListViewResult.DataSource = FoundBride;
                        ListViewResult.DataBind();
                    }
                }
            }
        }
3
  • else if (RadioButtonBride.Checked == true | RadioButtonBride.Checked == false) this is always true. Jun 5, 2013 at 17:12
  • that code is fine, but i need to show data from two different tables in ListView ,see my question.. Jun 5, 2013 at 17:14
  • As far as how you can manage it goes, I suggest breaking down your if statements into one method instead of chaining them together. Also take a look at int.Parse(). If you have integer values as string in your dropdown list, you don't need to check for their string value in one line and hardcode their int values in the next. Jun 5, 2013 at 17:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.