0

Windows Phone 8用のデータベースアプリを作成しようとしています。DBのテーブルの1つに新しいテーブルエントリを追加しようとすると、次のようなエラーが発生します。

    {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#とWindowsPhoneの開発に不慣れであることに注意してください。

ViewModel:

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;
}
4

1 に答える 1

0

CygSemName.Textのタイプは文字列ではなくbyte[]のようです。データモデルを確認して、それを確認してください。

于 2013-03-04T08:07:47.957 に答える