vinay teja reddy
vinay teja reddy

Reputation: 259

How to load two SQL Tables into dataset and how to bind it to ListView?

How to bind the tables to the ListView and which control should be used LinqDataSource or ObjectDataSource,what should be the DataSourceID for the ListView

My ListView Is

<asp:ListView ID="ListViewResult" runat="server" DataSourceID="" GroupItemCount="3">
                    <EmptyDataTemplate>
                        <table id="Table1" runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                            <tr>
                                <td>No data was returned.</td>
                            </tr>
                        </table>
                    </EmptyDataTemplate>
                    <EmptyItemTemplate>
<td id="Td1" runat="server" />
                    </EmptyItemTemplate>
                    <GroupTemplate>
                        <tr id="itemPlaceholderContainer" runat="server">
                            <td id="itemPlaceholder" runat="server"></td>
                        </tr>
                    </GroupTemplate>
                    <InsertItemTemplate>
                        <td id="Td2" runat="server" style="">ID:
                            <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="ID" Mode="Insert" ValidationGroup="Insert" />
                            <br />Image:
                            <asp:DynamicControl ID="DynamicControl2" runat="server" DataField="Image" Mode="Insert" ValidationGroup="Insert" />
                            <br />Name:
                            <asp:DynamicControl ID="DynamicControl3" runat="server" DataField="Name" Mode="Insert" ValidationGroup="Insert" />
                            <br />Age:
                            <asp:DynamicControl ID="DynamicControl4" runat="server" DataField="Age" Mode="Insert" ValidationGroup="Insert" />
                            <br />Height:
                            <asp:DynamicControl ID="DynamicControl5" runat="server" DataField="Height" Mode="Insert" ValidationGroup="Insert" />
                            <br />Education:
                            <asp:DynamicControl ID="DynamicControl6" runat="server" DataField="Education" Mode="Insert" ValidationGroup="Insert" />
                            <br />CurrentStatus:
                            <asp:DynamicControl ID="DynamicControl7" runat="server" DataField="CurrentStatus" Mode="Insert" ValidationGroup="Insert" />
                            <br />
                            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" ValidationGroup="Insert" />
                            <br />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                            <br /></td>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <td id="Td3" runat="server" style="background-color: #FFFBD6;color: #333333;">ID:
                            <asp:DynamicControl ID="DynamicControl8" runat="server" DataField="ID" Mode="ReadOnly" />
                            <br />Image:
                            <asp:DynamicControl ID="DynamicControl9" runat="server" DataField="Image" Mode="ReadOnly" />
                            <br />Name:
                            <asp:DynamicControl ID="DynamicControl10" runat="server" DataField="Name" Mode="ReadOnly" />
                            <br />Age:
                            <asp:DynamicControl ID="DynamicControl11" runat="server" DataField="Age" Mode="ReadOnly" />
                            <br />Height:
                            <asp:DynamicControl ID="DynamicControl12" runat="server" DataField="Height" Mode="ReadOnly" />
                            <br />Education:
                            <asp:DynamicControl ID="DynamicControl13" runat="server" DataField="Education" Mode="ReadOnly" />
                            <br />CurrentStatus:
                            <asp:DynamicControl ID="DynamicControl14" runat="server" DataField="CurrentStatus" Mode="ReadOnly" />
                            <br /></td>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <table id="Table2" runat="server">
                            <tr id="Tr1" runat="server">
                                <td id="Td4" runat="server">
                                    <table id="groupPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                        <tr id="groupPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr id="Tr2" runat="server">
                                <td id="Td5" runat="server" style="text-align: center;background-color: #FFCC66;font-family: Verdana, Arial, Helvetica, sans-serif;color: #333333;">
                                    <asp:DataPager ID="DataPager1" runat="server" PageSize="9">
                                        <Fields>
                                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
                                        </Fields>
                                    </asp:DataPager>
                                </td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                    <SelectedItemTemplate>
                        <td id="Td6" runat="server" style="background-color: #FFCC66;font-weight: bold;color: #000080;">ID:
                            <asp:DynamicControl ID="DynamicControl15" runat="server" DataField="ID" Mode="ReadOnly" />
                            <br />Image:
                            <asp:DynamicControl ID="DynamicControl16" runat="server" DataField="Image" Mode="ReadOnly" />
                            <br />Name:
                            <asp:DynamicControl ID="DynamicControl17" runat="server" DataField="Name" Mode="ReadOnly" />
                            <br />Age:
                            <asp:DynamicControl ID="DynamicControl18" runat="server" DataField="Age" Mode="ReadOnly" />
                            <br />Height:
                            <asp:DynamicControl ID="DynamicControl19" runat="server" DataField="Height" Mode="ReadOnly" />
                            <br />Education:
                            <asp:DynamicControl ID="DynamicControl20" runat="server" DataField="Education" Mode="ReadOnly" />
                            <br />CurrentStatus:
                            <asp:DynamicControl ID="DynamicControl21" runat="server" DataField="CurrentStatus" Mode="ReadOnly" />
                            <br /></td>
                    </SelectedItemTemplate>
                </asp:ListView>

My Code is

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();
            int From = int.Parse(DropDownListFrom.SelectedItem.Text);
            int To = int.Parse(DropDownListTo.SelectedItem.Text);

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            if (RadioButtonGroom.Checked == true | RadioButtonBride.Checked == false)
            {
                if ((From == 21) && (To == 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
                    {
                        SqlCommand cmd = Data.GetCommand(FoundGroom) as SqlCommand;
                        SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
                        DataAdapter.Fill(ds);

                        ListViewResult.DataSource = ds;
                        ListViewResult.DataBind();
                    }
                }
                else if ((From == 21) && (To == 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
                    {
                        SqlCommand cmd = Data.GetCommand(FoundGroom) as SqlCommand;
                        SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
                        DataAdapter.Fill(ds);

                        ListViewResult.DataSource = ds;
                        ListViewResult.DataBind();
                    }
                }

                else if ((From == 21) && (To == 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
                    {
                        SqlCommand cmd = Data.GetCommand(FoundGroom) as SqlCommand;
                        SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
                        DataAdapter.Fill(ds);

                        ListViewResult.DataSource = ds;
                        ListViewResult.DataBind();
                    }
                }
            }
            else if (RadioButtonBride.Checked == true | RadioButtonBride.Checked == false)
            {
                if ((From == 21) && (To == 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
                    {
                        SqlCommand cmd = Data.GetCommand(FoundBride) as SqlCommand;
                        SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
                        DataAdapter.Fill(ds);

                        ListViewResult.DataSource = ds;
                        ListViewResult.DataBind();
                    }
                }
                else if ((From == 21) && (To == 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
                    {
                        SqlCommand cmd = Data.GetCommand(FoundBride) as SqlCommand;
                        SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
                        DataAdapter.Fill(ds);

                        ListViewResult.DataSource = ds;
                        ListViewResult.DataBind();
                    }
                }

                else if ((From == 21) && (To == 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
                    {
                        SqlCommand cmd = Data.GetCommand(FoundBride) as SqlCommand;
                        SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
                        DataAdapter.Fill(ds);

                        ListViewResult.DataSource = ds;
                        ListViewResult.DataBind();
                    }
                }
            }
        }
    }

Upvotes: 0

Views: 751

Answers (2)

vinay teja reddy
vinay teja reddy

Reputation: 259

Ok i did this i want to show data of two tables on the ListView, so that i don't need to use two list views on a page,

what i did is,i inserted two ListViews and two LinqDataSource's and attached them and after that did this.

 protected void Page_Load(object sender, EventArgs e)
    {

        if(!Page.IsPostBack)
        {

            ListViewGroom.Visible = false;
            ListViewBride.Visible = false;
        }
        else if(Page.IsPostBack)
        {
            if (RadioButtonGroom.Checked == true)
            {
                ListViewBride.Visible = false;
            }
            else if(RadioButtonBride.Checked == true)
            {
                ListViewGroom.Visible = false;
            }
        }
    }



    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();
            int From = int.Parse(DropDownListFrom.SelectedItem.Text);
            int To = int.Parse(DropDownListTo.SelectedItem.Text);

            if (RadioButtonGroom.Checked == true | RadioButtonBride.Checked == false)
            {
                if ((From == 21) && (To == 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())
                    {
                        ListViewGroom.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewGroom.Visible = true;
                        ListViewGroom.DataSourceID = "";
                        ListViewGroom.DataSource = FoundGroom;
                        ListViewGroom.DataBind();
                    }
                }
                else if ((From == 21) && (To == 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())
                    {
                        ListViewGroom.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewGroom.Visible = true;
                        ListViewGroom.DataSourceID = "";
                        ListViewGroom.DataSource = FoundGroom;
                        ListViewGroom.DataBind();
                    }
                }

                else if ((From == 21) && (To == 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())
                    {
                        ListViewGroom.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewGroom.Visible = true;
                        ListViewGroom.DataSourceID = "";
                        ListViewGroom.DataSource = FoundGroom;
                        ListViewGroom.DataBind();
                    }
                }
            }
            else if (RadioButtonBride.Checked == true | RadioButtonBride.Checked == false)
            {
                if ((From == 21) && (To == 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())
                    {
                        ListViewBride.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewBride.Visible = true;
                        ListViewBride.DataSourceID = "";
                        ListViewBride.DataSource = FoundBride;
                        ListViewBride.DataBind();
                    }
                }
                else if ((From == 21) && (To == 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())
                    {
                        ListViewBride.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewBride.Visible = true;
                        ListViewBride.DataSourceID = "";
                        ListViewBride.DataSource = FoundBride;
                        ListViewBride.DataBind();
                    }
                }

                else if ((From == 21) && (To == 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())
                    {
                        ListViewBride.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewBride.Visible = true;
                        ListViewBride.DataSourceID = "";
                        ListViewBride.DataSource = FoundBride;
                        ListViewBride.DataBind();
                    }
                }
            }
        }
    }

hided the ListView's according to requirement.

Upvotes: 0

Logar314159
Logar314159

Reputation: 503

i think it could be:

ListViewResult.DataSource = ds;

use

ListViewResult.DataSource = ds.Tables[0];

or

ListViewResult.DataSource = ds.Tables("TableName");

and please replace all of these:

if(From = 21 && To = 24)
{
    (Males.Age == 21 || Males.Age == 22 || Males.Age == 23 || Males.Age == 24)
}

just with

(Males.Age >= From && Males.Age <= To)

Upvotes: 1

Related Questions