2013-03-03 56 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

相關問題