0

I have a gridview and linkbuttons at every row of this gridview. When i click one of the buttons, i get the values of the courseName,courseCode and term column values of this row. Here is my code:

 int row = -1;
        int.TryParse(e.CommandArgument as string, out row);

        GridViewRow gdrow = instCourses.Rows[row];

        // *** Get the underlying data item - in this case a DataRow
        DataRow dr = ((DataTable)this.instCourses.DataSource).Rows[gdrow.DataItemIndex];

        // *** Retrieve our context
        string courseCode = dr["CourseCode"].ToString();
        string courseNumber = dr["CourseNumber"].ToString();
        string term = dr["Term"].ToString();
        SqlConnection con = new SqlConnection();
        con.ConnectionString = Userfunctions.GetConnectionString();
        con.Open();
        SqlCommand cmd=new SqlCommand("select RegisterTable.StudentID,StudentTable.Name,StudentTable.Surname from RegisterTable INNER JOIN StudentTable where RegisterTable.StudentID=StudentTable.StudentID and RegisterTable.CourseCode=@courseCode and RegisterTable.Term=@term and RegisterTable.CourseNumber=@courseNumber",con);
        cmd.Parameters.AddWithValue("@courseCode", courseCode);
        cmd.Parameters.AddWithValue("@courseNumber", courseNumber);
        cmd.Parameters.AddWithValue("@term", term);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        studList.DataSource = dt;
        studList.DataBind();

The values return as i expect, but i think there is something wrong with my query, it says Incorrect syntax near the keyword 'where'. Can anyone see the problem?

Thanks

5 Answers 5

5

Format your sql query and you'll see the problem:

select RegisterTable.StudentID,StudentTable.Name,StudentTable.Surname 
from RegisterTable INNER JOIN StudentTable 
where RegisterTable.StudentID=StudentTable.StudentID 
and RegisterTable.CourseCode=@courseCode 
and RegisterTable.Term=@term 
and RegisterTable.CourseNumber=@courseNumber

There is no ON clause in the join.

from RegisterTable INNER JOIN StudentTable 
    ON RegisterTable.StudentID=StudentTable.StudentID 
where ....

By the way, in C# you can use a verbatim string literal to format your sql:

string sql = @"
    SELECT registertable.studentid, 
           studenttable.name, 
           studenttable.surname 
    FROM   registertable 
           INNER JOIN studenttable 
                   ON registertable.studentid = studenttable.studentid 
    WHERE  registertable.coursecode = @courseCode 
           AND registertable.term = @term 
           AND registertable.coursenumber = @courseNumber";

Then it's easier to locate errors in your sql-queries.

Sign up to request clarification or add additional context in comments.

Comments

1

The query syntax is not correct. Try:

SELECT 
    RegisterTable.StudentID,
    StudentTable.Name,
    StudentTable.Surname 
FROM RegisterTable 
INNER JOIN StudentTable ON RegisterTable.StudentID = StudentTable.StudentID
WHERE 
    RegisterTable.CourseCode = @courseCode 
    AND RegisterTable.Term = @term 
    AND RegisterTable.CourseNumber = @courseNumber

You need to change the WHERE to ON to complete the JOIN, and then you should move the rest of the criteria to the WHERE clause.

1 Comment

Thanks, that worked the only thing is the name of the column in student table is ID, not StudentID when i fixed it that worked, thank you!
1

You have an inner join with out ON

select RegisterTable.StudentID,StudentTable.Name,StudentTable.Surname from RegisterTable INNER JOIN StudentTable ON RegisterTable.studentid = StudentTable .id  where RegisterTable.StudentID=StudentTable.StudentID and RegisterTable.CourseCode=@courseCode and RegisterTable.Term=@term and RegisterTable.CourseNumber=@courseNumber

Comments

1

You need to specify which column from each table to join StudentTable on using ON instead of WHERE:

SELECT RegisterTable.StudentID, StudentTable.Name, StudentTable.Surname
FROM RegisterTable
INNER JOIN StudentTable ON RegisterTable.StudentID = StudentTable.StudentID
WHERE RegisterTable.CourseCode = @courseCode
    AND RegisterTable.Term = @term
    AND RegisterTable.CourseNumber = @courseNumber

Comments

1

You're missing the join condition here

FROM RegisterTable INNER JOIN StudentTable

Your inner join should looks like this

from RegisterTable INNER JOIN StudentTable 
   ON RegisterTable.StudentID = StudentTable.StudentId

According your where clause, your query could be written without using inner join, simply using a cartesian product.

select 
        RegisterTable.StudentID,
        StudentTable.Name,
        StudentTable.Surname 
from    RegisterTable, StudentTable 
where   RegisterTable.StudentID=StudentTable.StudentID and 
        RegisterTable.CourseCode=@courseCode and 
        RegisterTable.Term=@term and 
        RegisterTable.CourseNumber=@courseNumber

1 Comment

You only need one = in that SQL statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.