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)
) 
0

There are 0 best solutions below