Adarsh
Adarsh

Reputation: 59

Both DataSource and DataSourceID are defined on 'DataList1'. Remove one definition

Heres the dropdownlist and datalist code

<div>
    Sort by Category: 
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>All</asp:ListItem>
        <asp:ListItem>Decoration</asp:ListItem>
        <asp:ListItem>Catering</asp:ListItem>
        <asp:ListItem>Entertainment</asp:ListItem>
        <asp:ListItem>Sound</asp:ListItem>
        <asp:ListItem>Others</asp:ListItem>
    </asp:DropDownList>

    <asp:DataList ID="DataList1" runat="server"  
        GridLines="Both" RepeatColumns="4" RepeatDirection="Horizontal"
        Width="1000px" DataSourceID="SqlDataSource1" >
        <ItemTemplate>
            <table class="nav-justified">
                <tr>
                    <td class="text-center">
                        <strong>
                            <asp:Label ID="Label1" runat="server"
                            Text='<%# Eval("serName") %>'></asp:Label>
                        </strong>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Image ID="Image1" runat="server" Height="179px"
                            ImageUrl='<%# Eval("serImg") %>' Width="191px"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <strong>
                            <asp:Label ID="Label2" runat="server" Text="Rs"></asp:Label>
                            <asp:Label ID="Label3" runat="server" 
                                Text='<%# Eval("sprice") %>'>
                            </asp:Label>
                        </strong>
                    </td>
                </tr>
                <tr>
                    <td class="text-center">
                        <asp:Button ID="Button1" runat="server" Text="Details" />
                    </td>
                </tr>
            </table>
            <br />
        </ItemTemplate>
    </asp:DataList>
</div>

Here is the .cs code. Its purpose is to filter the data in the list according to the selected category in the dropdown list

protected void Page_Load(object sender, EventArgs e)
{
    String conString = ConfigurationManager.ConnectionStrings["regcon"].ConnectionString;
    string query = "select * from addService where serCategory=@cat";

    SqlCommand cmd = new SqlCommand(query);
    cmd.Parameters.AddWithValue("@cat", DropDownList1.SelectedItem.Value);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                DataList1.DataSource = ds;
                DataList1.DataBind();
            }
        }
    }   
}

I get the error "Both DataSource and DataSourceID are defined on 'DataList1'. Remove one definition." when i run it.Removing the Datalist1 or DataSourceID would give an error.How do i fix this?

Upvotes: 0

Views: 428

Answers (1)

wazz
wazz

Reputation: 5068

The problem is, on the datalist you have a datasource

DataSourceID="SqlDataSource1"

Then you also apply a data source in the code behind

DataList1.DataSource = ds;

You can't do both. You could remove the existing one in code then apply a new one if you want.

Upvotes: 1

Related Questions