0

My assignment is to do crud operations using database first on an Windows Form app. Currently my save button (Salveaza) is not working properly ( I get the above error when I debug it) and I don't understand why.

namespace Database
    {
        public partial class EntityFrameworkCRUD : Form
        {
            Cursuri modelCursuri = new Cursuri();
            Studenti modelStudenti = new Studenti();

            public EntityFrameworkCRUD()
            {
                InitializeComponent();

            }

            private void btnCurata_Click(object sender, EventArgs e)
            {
                Clear();
            }

            void Clear() 
            {
                tbNumeCurs.Text = tbDescriere.Text = tbNume.Text = "";
                btnSalveaza.Text = "Salveaza.";
                btnSterge.Enabled = false;
                modelCursuri.CursID = 0;
            }

            private void EntityFrameworkCRUD_Load(object sender, EventArgs e)
            {
                Clear();
                this.ActiveControl = tbNumeCurs;

            }

            private void btnSalveaza_Click(object sender, EventArgs e)
            {
                modelCursuri.CursNume = tbNumeCurs.Text.Trim();
                modelCursuri.CursDescriere = tbDescriere.Text.Trim();
                modelStudenti.StudentNume = tbNume.Text.Trim();
                using (AplicariCursuriContext db = new AplicariCursuriContext())
                {
                    var cursnume = Console.ReadLine();
                    var cursdescriere = Console.ReadLine();
                    var studentNume = Console.ReadLine();
                    var cursurile = new Cursuri { CursNume = cursnume, CursDescriere = cursdescriere };
                    // var cursurile2 = new Cursuri { CursDescriere = cursdescriere };
                    var studentii1 = new Studenti { StudentNume = cursdescriere };

                    db.Cursuris.Add(modelCursuri);
                   // db.Studentis.Add(modelStudenti);
                    db.SaveChanges();

                }
                Clear();
                MessageBox.Show("Submited Successfully.");
     }
        }
    }

Also the designs of the tables I'm using with Sql Server for the tables Cursuri:

CREATE TABLE Cursuri(
    [CursID] INT NOT NULL IDENTITY,
    [CursNume] VARCHAR(50) NULL,
    [CursDescriere] VARCHAR(50) NULL,
    CONSTRAINT [PK_CursID] PRIMARY KEY ([CursID])
) 

And Studenti:

CREATE TABLE Studenti(
    [StudentID] INT NOT NULL IDENTITY,
    [StudentNume] VARCHAR(50) NULL,
    CONSTRAINT [PK_StudentID] PRIMARY KEY ([StudentID]),
    CONSTRAINT [FK_Studenti_Cursuri] FOREIGN KEY ([CursID]) REFERENCES Cursuri(CursID)
) 
Dvalin
  • 31
  • 1
  • 7
  • show your Cursuri class. You probably need to make sure CursID is set to `null` instead of `0` – Garr Godfrey May 30 '20 at 21:52
  • public partial class Cursuri { public int CursID { get; set; } public string CursNume { get; set; } public string CursDescriere { get; set; } public virtual Studenti Studenti { get; set; } } } – Dvalin May 30 '20 at 21:55
  • 2
    Try to put this code on your property CursID: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CursID { get; set; } – Eliseu Marcos May 30 '20 at 22:06
  • I put it on the property but when I tried to type into my form and save the data, the form was not responding – Dvalin May 30 '20 at 22:17

0 Answers0