2013-03-03 58 views
0

我想为Windows Phone 8创建数据库应用程序。当我尝试在数据库的其中一个表中添加新表项时,出现如下错误。无法将值添加到本地数据库在Windows Phone应用程序

{System.InvalidCastException: Could not convert from type 'System.Byte[]' to type 'System.String'. 
    at System.Data.Linq.DBConvert.ChangeType(Object value, Type type) 
    at System.Data.Linq.ChangeDirector.StandardChangeDirector.DoResultSetAutoSync(TrackedObject item) 
    at System.Data.Linq.ChangeDirector.StandardChangeDirector.DoResultSetInsert(TrackedObject item) 
    at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item) 
    at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) 
    at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) 
    at System.Data.Linq.DataContext.SubmitChanges() 
    at CygnusModel.ViewModel.CygnusViewModel.AddNewSem(CygSem NewCygSem) 
    at Cygnus.AddSemView.Save_sem(Object sender, EventArgs e) 
    at Microsoft.Phone.Shell.ApplicationBarItemContainer.FireEventHandler(EventHandler handler, Object sender, EventArgs args) 
    at Microsoft.Phone.Shell.ApplicationBarIconButtonContainer.ClickEvent() 
    at Microsoft.Phone.Shell.ApplicationBar.OnCommand(UInt32 idCommand, Boolean isButton) 
    at Microsoft.Phone.Shell.Interop.NativeCallbackInteropWrapper.OnCommand(UInt32 idCommand, Boolean isButton)} 

这是我用

if (CygSemName.Text.Length > 0) 
     { 
      CygSem NewCygSem = new CygSem 
      { 
       SemName = (string)CygSemName.Text, 
       SemStart = (DateTime)CygSemStart.Value, 
       SemEnd = (DateTime)CygSemEnd.Value 
      }; 

      App.ViewModel.AddNewSem(NewCygSem); 

      if (NavigationService.CanGoBack) 
      { 
       NavigationService.GoBack(); 
      } 

能有人帮我这个代码?请记住,我是c#和Windows Phone开发中的新成员。

视图模型:

using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Linq; 

//Includes DataModel Classes 
using CygnusModel.Model; 

namespace CygnusModel.ViewModel 
{ 
    public class CygnusViewModel : INotifyPropertyChanged 
    { 
     private CygDataContext CygDb; 

     //Class Constructor Creating the connection between Model & View 
     public CygnusViewModel(string CygDataConnectionString) 
     { 
      CygDb = new CygDataContext(CygDataConnectionString); 
     } 

     //Save changing Method 
     private void CygSaveChanges() 
     { 
      CygDb.SubmitChanges(); 
     } 

     //Contains Change tracking INotify Events 
     #region INotifies 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     #endregion 

     //All The Semesters 
     private ObservableCollection<CygSem> _allSems; 
     public ObservableCollection<CygSem> AllSems 
     { 
      get 
      { 
       return _allSems; 
      } 
      set 
      { 
       _allSems = value; 
       NotifyPropertyChanged("AllSems"); 
      } 
     } 

     //All The Subjects 
     private ObservableCollection<CygSubs> _allSubs; 
     public ObservableCollection<CygSubs> AllSubs 
     { 
      get 
      { 
       return _allSubs; 
      } 
      set 
      { 
       _allSubs = value; 
       NotifyPropertyChanged("AllSubs"); 
      } 
     } 

     //All The Lectures 
     private ObservableCollection<CygLect> _allLects; 
     public ObservableCollection<CygLect> AllLects 
     { 
      get 
      { 
       return _allLects; 
      } 
      set 
      { 
       _allLects = value; 
       NotifyPropertyChanged("AllLects"); 
      } 
     } 

     //All The Exams 
     private ObservableCollection<CygExam> _allExam; 
     public ObservableCollection<CygExam> AllExam 
     { 
      get 
      { 
       return _allExam; 
      } 
      set 
      { 
       _allExam = value; 
       NotifyPropertyChanged("AllExam"); 
      } 
     } 

     //All The Instructors 
     private ObservableCollection<CygInst> _allInsts; 
     public ObservableCollection<CygInst> AllInsts 
     { 
      get 
      { 
       return _allInsts; 
      } 
      set 
      { 
       _allInsts = value; 
       NotifyPropertyChanged("AllInsts"); 
      } 
     } 

     //All The Holidays 
     private ObservableCollection<CygHoli> _allHolis; 
     public ObservableCollection<CygHoli> AllHolis 
     { 
      get 
      { 
       return _allHolis; 
      } 
      set 
      { 
       _allHolis = value; 
       NotifyPropertyChanged("AllHolis"); 
      } 
     } 

     //All the lectures of today 
     private ObservableCollection<CygLect> _todayLect; 
     public ObservableCollection<CygLect> TodayLect 
     { 
      get 
      { 
       return _todayLect; 
      } 
      set 
      { 
       _todayLect = value; 
       NotifyPropertyChanged("TodayLect"); 
      } 
     } 

     //All the Exams of today 
     private ObservableCollection<CygExam> _todayExam; 
     public ObservableCollection<CygExam> TodayExam 
     { 
      get 
      { 
       return _todayExam; 
      } 
      set 
      { 
       _todayExam = value; 
       NotifyPropertyChanged("TodayExam"); 
      } 
     } 

     // Query database and load the all the data 
     public void LoadAllAvailables() 
     { 

     } 

     //Query DB and load all the semester 
     public void LoadAllSems() 
     { 
      var Sems = from CygSem semes in CygDb.Semesters select semes; 
      AllSems = new ObservableCollection<CygSem>(Sems); 
     } 

     //Query DB and load all Holidays 
     public void LoadAllHolis() 
     { 
      var Holis = from CygHoli holid in CygDb.Holidays select holid; 
      AllHolis = new ObservableCollection<CygHoli>(Holis); 
     } 

     //Query DB and load Subjects based on Semester 
     public void LoadSemBasedSubs() 
     { 

     } 

     //Query DB and load all the Lectures and exams per day 
     public void LoadForTheDay() 
     { 

     } 

     //Query the DB and load Lectures and Exams per Subject 
     public void LoadForTheSub() 
     { 

     } 

     //Add New Sem 
     public void AddNewSem(CygSem NewCygSem) 
     { 
      CygDb.Semesters.InsertOnSubmit(NewCygSem); 
      CygDb.SubmitChanges(); 
      AllSems.Add(NewCygSem); 
     } 

     //Add New Holiday 
     public void AddNewHoli(CygHoli NewCygHoli) 
     { 
      CygDb.Holidays.InsertOnSubmit(NewCygHoli); 
      CygDb.SubmitChanges(); 
     } 

    } 
} 

型号:

//Semester Class 
    [Table] 
    public class CygSem : INotifyPropertyChanging, INotifyPropertyChanged 
    { 
     //_version variable. Binary Datatype 
     [Column(IsVersion = true)] 
     private string _version; 

     //Contains Change tracking INotify Events 
     #region INotifies 
     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged(string PropertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); 
      } 
     } 

     public event PropertyChangingEventHandler PropertyChanging; 

     private void NotifyPropertyChanging(string PropertyName) 
     { 
      if (PropertyChanging != null) 
      { 
       PropertyChanging(this, new PropertyChangingEventArgs(PropertyName)); 
      } 
     } 
     #endregion 

     //Semester ID Variable 
     private int _semID; 
     [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", 
      CanBeNull = false, AutoSync = AutoSync.OnInsert)] 
     public int SemID 
     { 
      get 
      { 
       return _semID; 
      } 
      set 
      { 
       NotifyPropertyChanging("SemID"); 
       _semID = value; 
       NotifyPropertyChanged("SemID"); 
      } 
     } 

     ////Semester Name 
     //private string _semName; 
     //[Column] 
     //public string SemName 
     //{ 
     // get 
     // { 
     //  return _semName.ToString(); 
     // } 
     // set 
     // { 
     //  NotifyPropertyChanging("SemName"); 
     //  _semName = value.ToString(); 
     //  NotifyPropertyChanged("SemName"); 
     // } 
     //} 

     //Semester Start Date 
     private DateTime _semStart; 
     [Column] 
     public DateTime SemStart 
     { 
      get 
      { 
       return _semStart; 
      } 
      set 
      { 
       NotifyPropertyChanging("SemStart"); 
       _semStart = value; 
       NotifyPropertyChanged("SemStart"); 
      } 
     } 

     //Semester End Date 
     private DateTime _semEnd; 
     [Column] 
     public DateTime SemEnd 
     { 
      get 
      { 
       return _semEnd; 
      } 
      set 
      { 
       NotifyPropertyChanging("SemEnd"); 
       _semEnd = value; 
       NotifyPropertyChanged("SemEnd"); 
      } 
     } 

     #region Subject Set 
     // Define the entity set for the collection side of the relationship. 
     private EntitySet<CygSubs> _subs; 
     [Association(Storage = "_subs", OtherKey = "_cygSemID", ThisKey = "SemID")] 
     public EntitySet<CygSubs> Subs 
     { 
      get 
      { 
       return this._subs; 
      } 
      set 
      { 
       this._subs.Assign(value); 
      } 
     } 

     // Assign handlers for the add and remove operations, respectively. 
     public CygSem() 
     { 
      _subs = new EntitySet<CygSubs>(
       new Action<CygSubs>(this.attach_sem), 
       new Action<CygSubs>(this.dettach_sem) 
       ); 
     } 

     // Called during an add operation 
     private void attach_sem(CygSubs _subj) 
     { 
      NotifyPropertyChanging("CygSubs"); 
      _subj.Semester = this; 
     } 

     // Called during a remove operation 
     private void dettach_sem(CygSubs _subj) 
     { 
      NotifyPropertyChanging("CygSubs"); 
      _subj.Semester = null; 
     } 
     #endregion 
    } 

的DataContext:

[Database] 
public class CygDataContext : DataContext 
{ 
    //Pass The Connection String to the base class. 
    public CygDataContext(string ConnectionString) 
     : base(ConnectionString) 
    { } 

    //Specify table Semesters 
    public Table<CygSem> Semesters; 

    //Specify table Subjects 
    public Table<CygSubs> Subjects; 

    //Specify table Lecture 
    public Table<CygLect> Lectures; 

    //Specify table Exams 
    public Table<CygExam> Exams; 

    //Specify table Instructors 
    public Table<CygInst> Insts; 

    //Specify table Holidays 
    public Table<CygHoli> Holidays; 
} 

回答

0

好像CygSemName.Text的类型是字节[]不字符串。检查你的数据模型以确保这一点。

+0

它是字符串。此外,错误说它不能从字节[]转换为字符串。这意味着错误不在数据模型中吗? – Vishnu 2013-03-04 18:35:28

+0

仍然只是为了确保我试着编译没有datamodel中的属性。我得到了同样的错误。 datetime属性会成为问题吗? – Vishnu 2013-03-05 05:19:21

+0

也许尝试删除它然后测试 – 2013-03-05 07:49:25

相关问题