Domanda

I have tried some example for data list control but i am getting some error which i can not fix. Please help me by correcting error in my program.

This is my aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="datalist.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" 
            OnItemCommand="DataList1_ItemCommand" Height="774px">
        <ItemTemplate> <asp:Panel ID="Panel1" runat="server" BackColor="#FF9933" BorderWidth="3px" Height="380px" Width="270px">
        <table>
        <tr >
        <td width="75%" style="color: #0000FF; font-weight: bold">
        <asp:Label ID="lbl" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label></td></tr>
        <tr >
        <td width="50%" style="color: #009900; font-weight: bold">
        <span style="color: Black; font-weight: bold;">ProductDetails:</span><br />
        <asp:Label ID="lbl2" runat="server" Text='<%#Eval("ProductDescription") %>'></asp:Label>
        </td>
        </tr>
        <tr >
        <td width="75%" style="color: #FF0000; font-weight: bold"><span style="color: Black; font-weight: bold;">Price:</span>
        <br /><asp:Label ID="lbl3" runat="server" Text='<%#Eval("ProductCost") %>'></asp:Label>
        </td>
        </tr>
        <tr>
        <td align="right">
        <asp:LinkButton ID="LinkButton1" runat="server"
        Font-Underline="False" style="font-weight: 700; color: Black" CommandName="ViewDetails" CommandArgument='<%#Eval("ProductId") %>' BackColor="#FF9933">ViewDeatils</asp:LinkButton>
        </td></tr>
                </table>
                </asp:Panel>
        </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>

And this my another aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="datalist.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 369px;
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <table class="style1">
        <tr>
        <td style="color: #0000FF; font-weight: 700" >
        <span style="color: Black; font-weight: bold;">Modal:</span><br /><asp:Literal ID="Literal1" runat="server"></asp:Literal>
        </td>
        </tr>
        <tr>
        <td style="font-weight: 700; color: #009933" >
        <span style="color: Black; font-weight: bold;">ProductDetails:</span><br /><asp:Literal ID="Literal2" runat="server"></asp:Literal>
        </td>
        </tr>
        <tr>
        <td style="font-weight: 700; color: #FF0000" >
        <span style="color: Black; font-weight: bold;">Price:</span><br /><asp:Literal ID="Literal3" runat="server"></asp:Literal>
        </td>
        </tr>
        </table>

            </div>
            </form>
        </body>
        </html>

This my aspx.cs file for webform1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace datalist
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetData();
        }

        public void GetData()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sql connection"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from datalist", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataList1.DataSource = ds.Tables[0].DefaultView;
            DataList1.DataBind();

        }

        protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e)
        {
            if (e.CommandName == "ViewDetails")
            {
                Response.Redirect("WebForm2.aspx?Id=" + e.CommandArgument.ToString());

            }
        }
    }
}

And this my aspx.cs file for webform2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace datalist
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetData();
        }

        public void GetData()
        {
            string Id = Request["Id"].ToString();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sql connection"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from datalist where ProductId = " + Id, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Literal1.Text = ds.Tables[0].Rows[0][1].ToString();
            Literal2.Text = ds.Tables[0].Rows[0][2].ToString();
            Literal3.Text = ds.Tables[0].Rows[0][3].ToString();

        }
    }
}

And i am getting error like this

Server Error in '/' Application.
Both DataSource and DataSourceID are defined on 'DataList1'.  Remove one definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'DataList1'.  Remove one definition.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: Both DataSource and DataSourceID are defined on 'DataList1'.  Remove one definition.]
   System.Web.UI.WebControls.BaseDataList.ConnectToDataSourceView() +8685854
   System.Web.UI.WebControls.BaseDataList.OnLoad(EventArgs e) +19
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Control.LoadRecursive() +146
   System.Web.UI.Control.LoadRecursive() +146
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Thank you

È stato utile?

Soluzione

It requires you to do exactly that. You seem to have set both the DataSourceID and DataSource properties on DataList1. Remove one of them. The DataSourceID tends to be set in declarative markup and the DataSource tends to be set in codebehind. Because you're using a SqlDataSource, remove any lines that explicitly set the DataSource property in codebehind.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top