linq错误:CS0102:该类型已包含定义

时间:2009-06-04 01:36:58

标签: .net asp.net linq mobile

我正在开发一个mobile.net项目。 我正在尝试列出一个显示在下拉列表中的餐馆列表...当用户选择一个项目时,他可以将其添加到收藏列表中。 但是我收到了错误

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0102: The type 'mobile_rest' already contains a definition for 'emptyChangingEventArgs'

Source Error:

Line 78: {
Line 79:    
Line 80:    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
Line 81:    
Line 82:    private int _r_id;

我不知道为什么会发生这种情况...... 是因为我在另一个文件中使用了同一个表吗?

这是我的代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Favorite.aspx.cs" Inherits="Favorite" %>
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <mobile:Form id="Form1" runat="server" BackColor="#fcf9f3" Method="get" Paginate="True">

        <mobile:Label ID="FaveError" Runat="server" ForeColor="red" 
            Font-Size="Small" Font-Bold="True" Visible="false" />

        <mobile:Label ID="Label2" Runat="server" ForeColor="#d3af63" Font-Italic="True" 
                Font-Name="Monotype Covasia" Font-Bold="True" text="Choose Favorite's" />

        <mobile:SelectionList ID="listSearch" Runat="server" DataTextfield="r_name"
                 DataValueField="r_name" BreakAfter="False">

        </mobile:SelectionList>

        <mobile:Command ID="btnadd" text="Add" Runat="server" OnClick="btn_add_Click" />

        <mobile:list runat="server" id="ListFavorite" DataTextfield="fave_name"
             DataValueField="user_id" Font-Size="Small" Font-Italic="True" 
             Wrapping="Wrap"  BreakAfter="True"/>
            <mobile:Command ID="btndelete" text="Delete" Runat="server" OnClick="btn_delete_Click" />

    </mobile:Form>
</body>
</html>

背后的代码

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Data.Linq;
using System.Xml.Linq;
using System.Linq;

public partial class Favorite : System.Web.UI.MobileControls.MobilePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["error_msg"] != null)
        {
            FaveError.Text = Session["error_msg"].ToString();
            Session["error_msg"] = null;
            FaveError.Visible = true;
        }

        //if (Session["user_id"] = null)
        //{
        //    Response.Redirect("Login.aspx");
        //}
        GetRstr();
        GetFave();
    }

    protected void btn_add_Click(object sender, EventArgs e)
    {
        if (Page.IsValid) // only valid page will proceed
        {
            AddFave();
        }
    }

    #region add fave
    protected void AddFave()
    {
        String faveitem = listSearch.SelectedIndex;

        using (FavoriteDataContext Favorite = new FavoriteDataContext())
        {
            mobile_favorite mobilefave = new mobile_favorite();
            mobilefave.fave_name = faveitem;
            mobilefave.user_id = Int32.Parse(Session["user_id"].ToString());
            mobilefave.username = Session["user_name"].ToString();

            Favorite.mobile_favorites.InsertOnSubmit(mobilefave);
            Favorite.SubmitChanges();

            Session["error_msg"] = "You have a new favorite";
            Response.Redirect("Favorite.aspx");
        }
    }
    #endregion


    protected void btn_delete_Click(object sender, EventArgs e)
    {
        if (Page.IsValid) // only valid page will proceed
        {
            DeleteFave();
        }
    }

    #region del fave
    protected void DeleteFave()
        {
            int iuser_id = Int32.Parse(Session["user_id"].ToString());
            using (FavoriteDataContext Favorite = new FavoriteDataContext())
            {
                try
                {
                    mobile_favorite fave = Favorite.mobile_favorites.Single(f => f.user_id == iuser_id);

                    Favorite.mobile_favorites.DeleteOnSubmit(fave);
                    Favorite.SubmitChanges();
                }
                catch (Exception ex)
                {
                }
            }
        }
    #endregion


    protected void GetFave()
    {
        using (FavoriteDataContext Favorite = new FavoriteDataContext())
        {
            var fave = from f in Favorite.mobile_favorites
                      // where f.user_id == Int32.Parse(Session["user_id"].ToString())
                       select f;

            ListFavorite.DataSource = fave;
            ListFavorite.DataBind();
        }
    }

    protected void GetRstr()
    {
        using (Restaurant2DataContext Restaurant2 = new Restaurant2DataContext())
        {
            var rstr = from r in Restaurant2.table_rests
                       select r;

            listSearch.DataSource = rstr;
            listSearch.DataBind();
        }
    }
}

mobile_rest是表名

3 个答案:

答案 0 :(得分:1)

您声明了2个或更多emptyChangingEventArgs。从错误消息中可以看出这一点。

答案 1 :(得分:1)

如果“mobile_rest”是一个数据库表名,并且您正在使用标准的VS2008工具来创建基于LINQ的类,那么我猜你还有一个名为“mobile_rest”的部分类。你不小心有这种类型在多个地方声明emptyChangingEventArgs吗?只需使用VS中的搜索功能,查看此静态字段的声明次数。

答案 2 :(得分:0)

我修正了错误。 它发生了bcoz我在超过1个linq文件中使用了相同的表。 发生了冲突。 感谢您抽出时间回复。