Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类型为'的首次机会例外;System.Data.SqlServerCe.SqlCeException';发生在Microsoft.Phone.Data.Internal.ni.dll中_C#_Sql Server_Windows Phone 7 - Fatal编程技术网

C# 类型为'的首次机会例外;System.Data.SqlServerCe.SqlCeException';发生在Microsoft.Phone.Data.Internal.ni.dll中

C# 类型为'的首次机会例外;System.Data.SqlServerCe.SqlCeException';发生在Microsoft.Phone.Data.Internal.ni.dll中,c#,sql-server,windows-phone-7,C#,Sql Server,Windows Phone 7,我在数据库表中添加了一个新列。当我添加这个新专栏时,第一个偶然的异常开始出现。当我调试它时,它说明了代码行 MLongListSelector.ItemsSource=c.ToDoList.ToList() 是导致问题的下一条要执行的语句。我做错了什么 using (DatabaseContext c = new DatabaseContext(DatabaseContext.ConnectionString)) { c.CreateIfNotExists

我在数据库表中添加了一个新列。当我添加这个新专栏时,第一个偶然的异常开始出现。当我调试它时,它说明了代码行
MLongListSelector.ItemsSource=c.ToDoList.ToList() 是导致问题的下一条要执行的语句。我做错了什么

using (DatabaseContext c = new DatabaseContext(DatabaseContext.ConnectionString))
        {
            c.CreateIfNotExists();
            c.LogDebug = true;
            //output todolist data from database
            MLongListSelector.ItemsSource = c.ToDoList.ToList();
        }
该行的堆栈跟踪:

    ex.StackTrace   "   at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)\r\n   at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()\r\n   at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)\r\n   at System.Data.SqlServerCe.SqlCeCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult, Boolean isCompiledQuery)\r\n   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries, Boolean isCompiledQuery)\r\n   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)\r\n   at System.Data.Linq.Table`1.GetEnumerator()\r\n   at System.Data.Linq.Table`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at PhoneApp.MainPage.OnNavigatedTo(NavigationEventArgs e)"  
获取并设置:

public partial class ToDoList : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _Id;

    private string _Title;

    private string _Description;

    private string _Col;

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnIdChanging(int value);
partial void OnIdChanged();
partial void OnTitleChanging(string value);
partial void OnTitleChanged();
partial void OnDescriptionChanging(string value);
partial void OnDescriptionChanged();
partial void OnColChanging(string value);
partial void OnColChanged();
#endregion

    public ToDoList()
    {
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int Id
    {
        get
        {
            return this._Id;
        }
        set
        {
            if ((this._Id != value))
            {
                this.OnIdChanging(value);
                this.SendPropertyChanging();
                this._Id = value;
                this.SendPropertyChanged("Id");
                this.OnIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Title", DbType="NVarChar(100) NOT NULL", CanBeNull=false)]
    public string Title
    {
        get
        {
            return this._Title;
        }
        set
        {
            if ((this._Title != value))
            {
                this.OnTitleChanging(value);
                this.SendPropertyChanging();
                this._Title = value;
                this.SendPropertyChanged("Title");
                this.OnTitleChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Description", DbType="NVarChar(100)")]
    public string Description
    {
        get
        {
            return this._Description;
        }
        set
        {
            if ((this._Description != value))
            {
                this.OnDescriptionChanging(value);
                this.SendPropertyChanging();
                this._Description = value;
                this.SendPropertyChanged("Description");
                this.OnDescriptionChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col", DbType="NVarChar(25)")]
    public string Col
    {
        get
        {
            return this._Col;
        }
        set
        {
            if ((this._Col != value))
            {
                this.OnColChanging(value);
                this.SendPropertyChanging();
                this._Col = value;
                this.SendPropertyChanged("Col");
                this.OnColChanged();
            }
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我刚试过你的解决方案,我不明白为什么它不起作用。您是否将ToDoList标记为“表属性”?i、 e

此外,您可能值得删除数据库并重新创建它。遵循下面的代码示例并尝试从那里开始。如果这不起作用,那么可以附加一个简单的项目来复制这个问题

Xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <ListBox x:Name="lstToDos">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title}"></TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
数据上下文文件

public class DatabaseContext : DataContext
{
    public DatabaseContext(string connectionString)
    : base(connectionString)
    {

    }


    public Table<ToDoList> ToDoLists
    {
        get
        {
            return this.GetTable<ToDoList>();
        }
    }

    public void CreateIfNotExists()
    {
        if (!this.DatabaseExists())
        {
            this.CreateDatabase();
        }
    }

    public const string ConnectionString = @"isostore:/MyDatabases.sdf";
}

发布异常堆栈跟踪和消息。对于内部异常,如果它不是空的,也是这样。对不起,我是新的。我不知道在调试模式下发布异常堆栈跟踪和消息意味着什么,然后在异常消息弹出窗口中,单击“查看详细信息”>PhoneApp.DLL!PhoneApp.MainPage.On导航到(System.Windows.Navigation.NavigationEventArgs e)第43行C#这是我得到的。你是说这个吗?在我的postHi@FunksMaName上添加了stacktrace,谢谢你花时间做cod。行c.CreateIfNotExists();在后面的代码中有一个小错误,我用你的代码解决了它,但是用if(!c.DatabaseExists()){c.CreateDatabase();}替换它。非常感谢!经过一整天的测试,我现在可以放松了!再次感谢
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <ListBox x:Name="lstToDos">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title}"></TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

   protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        using (DatabaseContext c = new DatabaseContext(DatabaseContext.ConnectionString))
        {
            c.CreateIfNotExists(); 

            //Add new records for debug sake: 
            var dateNow = DateTime.UtcNow.TimeOfDay.ToString();
            var newToDo = new ToDoList { Col = "Col" + dateNow, Description = "Some description" + dateNow, Title = "Some title" + dateNow };

            c.ToDoLists.InsertOnSubmit(newToDo);
            c.SubmitChanges();

            lstToDos.ItemsSource = c.ToDoLists.ToList();
        }
    }
}
public class DatabaseContext : DataContext
{
    public DatabaseContext(string connectionString)
    : base(connectionString)
    {

    }


    public Table<ToDoList> ToDoLists
    {
        get
        {
            return this.GetTable<ToDoList>();
        }
    }

    public void CreateIfNotExists()
    {
        if (!this.DatabaseExists())
        {
            this.CreateDatabase();
        }
    }

    public const string ConnectionString = @"isostore:/MyDatabases.sdf";
}
 [Table]
public partial class ToDoList : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _Id;

    private string _Title;

    private string _Description;

    private string _Col;

    #region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnIdChanging(int value);
    partial void OnIdChanged();
    partial void OnTitleChanging(string value);
    partial void OnTitleChanged();
    partial void OnDescriptionChanging(string value);
    partial void OnDescriptionChanged();
    partial void OnColChanging(string value);
    partial void OnColChanged();
    #endregion

    public ToDoList()
    {
        OnCreated();
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id
    {
        get
        {
            return this._Id;
        }
        set
        {
            if ((this._Id != value))
            {
                this.OnIdChanging(value);
                this.SendPropertyChanging();
                this._Id = value;
                this.SendPropertyChanged("Id");
                this.OnIdChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Title", DbType = "NVarChar(100) NOT NULL", CanBeNull = false)]
    public string Title
    {
        get
        {
            return this._Title;
        }
        set
        {
            if ((this._Title != value))
            {
                this.OnTitleChanging(value);
                this.SendPropertyChanging();
                this._Title = value;
                this.SendPropertyChanged("Title");
                this.OnTitleChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Description", DbType = "NVarChar(100)")]
    public string Description
    {
        get
        {
            return this._Description;
        }
        set
        {
            if ((this._Description != value))
            {
                this.OnDescriptionChanging(value);
                this.SendPropertyChanging();
                this._Description = value;
                this.SendPropertyChanged("Description");
                this.OnDescriptionChanged();
            }
        }
    }

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Col", DbType = "NVarChar(25)")]
    public string Col
    {
        get
        {
            return this._Col;
        }
        set
        {
            if ((this._Col != value))
            {
                this.OnColChanging(value);
                this.SendPropertyChanging();
                this._Col = value;
                this.SendPropertyChanged("Col");
                this.OnColChanged();
            }
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}