user1591535
user1591535

Reputation: 1

I have a error in update query...Check please my code

I have a four class Student,Course,Registration and UpdateScore

In StudentClass:

        public class Student
{
    public string studID  { get; set; }
    public string name { get; set; }

    public Student() { }

    public Student(string StudID,string Name)
    {
        this.studID = StudID;
        this.name = Name;
    }
}

In CourseClass

    public class Course
{
    public string courseID { get; set; }
    public string title { get; set; }
    public int credits { get; set; }

    public Course()
    {

    }

    public Course(string CourseID, string Name, int Credits)
    {
        this.courseID = CourseID;
        this.title = Name;
        this.credits = Credits;
    }
}

In Registration Class

     public class Registration 
{
    public Student studentData { get; set; }
    public Course courseData { get; set; } 
    public DateTime DOEnroll { get; set; } 

    public Registration ()
    {
    }

    public Registration (Student sData, Course cData, DateTime doe)
    {
        this.studentData = sData;
        this.courseData = cData;
        this.DOEnroll = doe;

    }

}

In UpdateScore Class

    public class UpdateScore 
{
    public Registration enrollData { get; set; }
    public int score { get; set; }

    public UpdateScore () { }

    public UpdateScore (Registration enrData, int sco)
    {
        this.enrollData = enrData;
        this.score = sco;
    }
}

And Now I was doing update with this query but shows null data in studentID and CourseID

Code is :

     public static void Update(UpdateScore score)
    {
        SqlConnection conn = DB.GetConnection();

        try
        {
            conn.Open();
            string selectStm = "UPDATE EnrollmentTable set Score = @Score where StudentID = @StudentID AND  CourseID = @CourseID";

            SqlCommand command = new SqlCommand(selectStm, conn);

            SqlParameter param1 = new SqlParameter();
            SqlParameter param2 = new SqlParameter();
            SqlParameter param3 = new SqlParameter();

            param1.ParameterName = "@StudentID";
            param2.ParameterName = "@CourseID";
            param3.ParameterName = "@Score";


            Student st = new Student();
            Course cr = new Course();

            Registration enr = new Registration ();

            enr.courseData = cr;
            enr.studentData = st;
            score.enrollData = enr;

            param1.Value = st.studID ;
            param2.Value = cr.courseID;
            param3.Value = score.score;


            command.Parameters.Add(param1);
            command.Parameters.Add(param2);
            command.Parameters.Add(param3);



            int i = command.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();//Close Connection
        }

    }

it gives me this exception

The parameterized query '(@StudentID nvarchar(4000),@CourseID nvarchar(4000),@Score int)U' expects the parameter '@StudentID', which was not supplied.

Can you please tell me how to get the value in StudentID and CourseID?

Upvotes: 0

Views: 90

Answers (2)

Alvaro Rodriguez
Alvaro Rodriguez

Reputation: 2848

Eric gave you a correct explanation to your problem. I want to point out another issue.

In your class Student, the autoproperty studID which you later assign to param1.Value is a string. Strings in C# are objects so their default value is null. Your Update code does not assign any value to studID, so when you assign it to param1.Value you are in fact assigning null to param1.Value. This is probably not what you want.

Same thing with CourseID.

Assuming the UpdateScore you receive in the Update method has been properly constructed with all data inside it as suggested by your object model, you should not create new Student or Course objects. Instead, do something like

param1.Value = score.enrollData.studentData.studID;

and something similar for the other information.

Keep coding!

Note: This looks a lot like homework, so you should also take this chance to think about what the code is doing, also run it with a debugger to see how values get assigned, etc. I've outlined some help but I won't do your whole work for you :-)

Upvotes: 0

Eric J.
Eric J.

Reputation: 150158

You should not have the @ in the parameter names themselves. Change

param1.ParameterName = "@StudentID";
param2.ParameterName = "@CourseID";
param3.ParameterName = "@Score";

to

param1.ParameterName = "StudentID";
param2.ParameterName = "CourseID";
param3.ParameterName = "Score";

Also,

  • You are catching an Exception only to throw it again. Don't catch an Exception you won't do something useful with.
  • It is cleaner to use the using keyword rather than closing your connection manually in a finally block.

Upvotes: 1

Related Questions