Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
                    }
                }
            }
        }
    }
share|improve this question
    
It looks like you're doing all the work in the code-behind to build the data source, and you're setting the ListViewResult.DataSource to the created datasource. Then you are DataBind()ing, which means you don't need to set a DataSourceID in the ASPX markup. –  Garrison Neely Jun 6 '13 at 16:24
    
Why so many If?, you could just use Males.Age >= From && Males.Age <= To –  Logar314159 Jun 6 '13 at 16:26
    
Garrison Neely - It gives error something like metatable etc etc when i run it. –  vinay teja reddy Jun 6 '13 at 16:29
    
Logar314159 - iam a beginner in c# :p –  vinay teja reddy Jun 6 '13 at 16:30
    
Garrison Neely - Error is "Could not determine a MetaTable. A MetaTable could not be determined for the data source '' and one could not be inferred from the request URL. Make sure that the table is mapped to the data source, or that the data source is configured with a valid context type and table name, or that the request is part of a registered DynamicDataRoute." –  vinay teja reddy Jun 6 '13 at 16:33

2 Answers 2

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)
share|improve this answer
    
It says System.Data.DataSet does not contain a defination for 'Table' . –  vinay teja reddy Jun 6 '13 at 16:54
    
ok, i am sorry, is Tables, i already edited the answer. –  Logar314159 Jun 6 '13 at 16:57
    
Logar314159 - and i will do remove the large code now :) :p –  vinay teja reddy Jun 7 '13 at 8:40
    
Logar314159 - thank you,that helped :) –  vinay teja reddy Jun 7 '13 at 8:43
    
@vinay teja reddy: did ds.Tables solve the problem? –  Logar314159 Jun 7 '13 at 13:48
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.