Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot add values to local database in windows phone app
    text
    copied!<p>I am trying to create a database app for Windows Phone 8. When I try to add a new table entry in one of the tables of the DB, I get an error like this.</p> <pre><code> {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)} </code></pre> <p>This is the code I used</p> <pre><code>if (CygSemName.Text.Length &gt; 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(); } </code></pre> <p>Could some one help me with this? Please keep in mind that, I am new in c# and Windows Phone development.</p> <p>ViewModel:</p> <pre><code>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 &amp; 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&lt;CygSem&gt; _allSems; public ObservableCollection&lt;CygSem&gt; AllSems { get { return _allSems; } set { _allSems = value; NotifyPropertyChanged("AllSems"); } } //All The Subjects private ObservableCollection&lt;CygSubs&gt; _allSubs; public ObservableCollection&lt;CygSubs&gt; AllSubs { get { return _allSubs; } set { _allSubs = value; NotifyPropertyChanged("AllSubs"); } } //All The Lectures private ObservableCollection&lt;CygLect&gt; _allLects; public ObservableCollection&lt;CygLect&gt; AllLects { get { return _allLects; } set { _allLects = value; NotifyPropertyChanged("AllLects"); } } //All The Exams private ObservableCollection&lt;CygExam&gt; _allExam; public ObservableCollection&lt;CygExam&gt; AllExam { get { return _allExam; } set { _allExam = value; NotifyPropertyChanged("AllExam"); } } //All The Instructors private ObservableCollection&lt;CygInst&gt; _allInsts; public ObservableCollection&lt;CygInst&gt; AllInsts { get { return _allInsts; } set { _allInsts = value; NotifyPropertyChanged("AllInsts"); } } //All The Holidays private ObservableCollection&lt;CygHoli&gt; _allHolis; public ObservableCollection&lt;CygHoli&gt; AllHolis { get { return _allHolis; } set { _allHolis = value; NotifyPropertyChanged("AllHolis"); } } //All the lectures of today private ObservableCollection&lt;CygLect&gt; _todayLect; public ObservableCollection&lt;CygLect&gt; TodayLect { get { return _todayLect; } set { _todayLect = value; NotifyPropertyChanged("TodayLect"); } } //All the Exams of today private ObservableCollection&lt;CygExam&gt; _todayExam; public ObservableCollection&lt;CygExam&gt; 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&lt;CygSem&gt;(Sems); } //Query DB and load all Holidays public void LoadAllHolis() { var Holis = from CygHoli holid in CygDb.Holidays select holid; AllHolis = new ObservableCollection&lt;CygHoli&gt;(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(); } } } </code></pre> <p>Model:</p> <pre><code>//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&lt;CygSubs&gt; _subs; [Association(Storage = "_subs", OtherKey = "_cygSemID", ThisKey = "SemID")] public EntitySet&lt;CygSubs&gt; Subs { get { return this._subs; } set { this._subs.Assign(value); } } // Assign handlers for the add and remove operations, respectively. public CygSem() { _subs = new EntitySet&lt;CygSubs&gt;( new Action&lt;CygSubs&gt;(this.attach_sem), new Action&lt;CygSubs&gt;(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 } </code></pre> <p>DataContext:</p> <pre><code>[Database] public class CygDataContext : DataContext { //Pass The Connection String to the base class. public CygDataContext(string ConnectionString) : base(ConnectionString) { } //Specify table Semesters public Table&lt;CygSem&gt; Semesters; //Specify table Subjects public Table&lt;CygSubs&gt; Subjects; //Specify table Lecture public Table&lt;CygLect&gt; Lectures; //Specify table Exams public Table&lt;CygExam&gt; Exams; //Specify table Instructors public Table&lt;CygInst&gt; Insts; //Specify table Holidays public Table&lt;CygHoli&gt; Holidays; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload