Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have 2 problems that I can't seem to understand,maybe you can help me.The first one is not described in the question but it concerns directly the database and my inner join.Before I tried to insert values into the table RegisterTeacher,everything was working fine,specifically this method that retrieves the rows from "education" column.
public IEnumerable<Cours> GetByEducation()
     {

         using (var context = new DatabaseStudentsEntities1())
         {
             var query = (from data in context.Courses select new { Education = data.education }).ToList().Select(c => new Cours { education = c.Education }).ToList();

             return query.ToList();

         }

     }



After I tried to insert the values from the registration into the database,i get this error(although I have checked all the values in the tables along with foreign keys and primary keys):"
Additional information: Schema specified is not valid. Errors: 
The relationship 'DatabaseStudentsModel.FK_Login_ToTable' was not loaded because the type 'DatabaseStudentsModel.RegisterTeacher' is not available."

Why???It did that before a couple of times but it allowed me to actually go on the register page and input data.Now it won't let me do that,and I don't understand why.Second,I have my inner join which I don't know how to do it exactly.Can anyone pleasegive me an example of inner join in C# for inserting data in 2 tables?These are my tables:
CREATE TABLE [dbo].[Courses] (
    [courseID]   INT            NOT NULL,
    [courseName] NVARCHAR (MAX) NOT NULL,
    [education]  NVARCHAR (50)  NULL,
    PRIMARY KEY CLUSTERED ([courseID] ASC)
);

CREATE TABLE [dbo].[RegisterTeacher] (
    [SNTeacher]  INT            NOT NULL,
    [UserName]   NVARCHAR (10)  NOT NULL,
    [pwd]        INT            NOT NULL,
    [fullName]   NVARCHAR (MAX) NOT NULL,
    [CourseName] NVARCHAR (MAX) NOT NULL,
    PRIMARY KEY CLUSTERED ([SNTeacher] ASC)
);

I would like to inner join the column education...

What I have tried:

I have tried this sintax for inner join:
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\source\repos\VIAApp2Demo\VIAApp2Demo\DB\DatabaseStudents.mdf;Integrated Security=True;Connect Timeout=30");
try
{
    if (conn.State == System.Data.ConnectionState.Closed)
        conn.Open();
    String query = "INSERT INTO RegisterTeacher( SNTeacher,UserName,pwd,fullName,CourseName) INNER JOIN education on Courses.education=RegisterTeacher.education VALUES(@SNTeacher,@UserName,@pwd,@fullName,@CourseName,@education)";
    SqlCommand cmd = new SqlCommand(query, conn);
    cmd.CommandType = CommandType.Text;

Am I on the right track?
Posted
Comments
Richard Deeming 19-Mar-18 10:16am    
Your SQL INSERT syntax is completely wrong: INSERT (Transact-SQL) | Microsoft Docs[^]

The "type is not available" error is a little more cryptic. It usually indicates there's a problem with your entity mapping.
Richard Deeming 19-Mar-18 10:19am    
Also, why is your RegisterTeacher table using the CourseName rather than the courseID?

Does a teacher really only have one course?

And you shouldn't be limiting passwords to numbers, nor storing them in plain text:
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
[no name] 19-Mar-18 13:22pm    
Are you using "database first" or "code first" or "don't know"?

Why do you not have "identifier" columns; i.e. auto-generated primary keys; the EF [KEY] attribute.

If you are using EF, why are you "hard-coding" your SQL? Why not LINQ to EF?

Answer those, and it will start you on the "right track".
Daniel Andrei Popescu 20-Mar-18 4:16am    
Hello,first of all I'm using db first because i thoght it would be a much easier and logical approach.Second of all,a teacher has multiple courses that will select from a listbox,that's why I passed the courseName.I understand that it's not the best approach,but it can be changed.My password is limited to nr because my system is made in such way that the teacher can log in with his NFC card(and the NFC card has a serial nr that contains aprox 10-12 numbers).
Daniel Andrei Popescu 20-Mar-18 4:23am    
I don't know Gerry Schmitz what you mean by auto-generated PK,but i can tell you that I started the project by making the database and then I auto-generated the model with EF.I was the one setting the PK based on how I wanted my system to be.Regarding the use of LINQ,I am a new to LINQ queries and I have worked with it a little,but I didn't know how to approach this so I stayed on my track using sql queries.I tried using LINQ in making the registration,but I got confused about the inner join.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900