locked
Failed to convert parameter value from a User to Int32 RRS feed

  • Question

  • Hi,

    How can I overcome "Failed to convert parameter value from a User to Int32" error? I have checked inside my db, that @createdBy parameter is Int data type.

    Below is some part of code from my UI and  another is Parameter passing in my stored procedure, 

                        _newNCSData.WeightingEducationalQualification = Convert.ToDecimal(txtWEQ.Text);
                        _newNCSData.WeightingEducationalAdmissionTest = Convert.ToDecimal(txtWAT.Text);
                        _newNCSData.Status="Active";
                        _newNCSData.IntendedCourse = new Course();
                        _newNCSData.IntendedCourse.CourseID = _registerdCourseId;
                   
                        _newNCSData.EnrolmentIntendedCourse = new Course();//added
                        _newNCSData.EnrolmentIntendedCourse.CourseID = _selectedIntendedCourse; //added
                        _newNCSData.RegisteredCourse = _returnCourse;//added
    
                        _newNCSData.CreatedBy = new User();
                        _newNCSData.CreatedBy.UserId = AuthenticatedUser.UserSingleton.GetSingletonObject().userID;
    
                        if (resultGrid.RowCount > 0)
                        {
                            _newNCSSpecialFactorData= new List<NormalisedCompositeScoreSpecialFactorSetup>();
                            
                            for (int i = 0; i < resultGrid.Rows.Count-1; ++i)
    
                            {
                                NormalisedCompositeScoreSpecialFactorSetup specialFactorSetUp = new NormalisedCompositeScoreSpecialFactorSetup();
    
                                if (resultGrid.Rows[i].Cells["NCSSpecialFactorSetupFoundationID"].Value.ToString() != string.Empty)
                                {
                                    specialFactorSetUp.NormalisedCompositeScoreSpecialFactorSetupFoundationID = int.Parse(resultGrid.Rows[i].Cells["NCSSpecialFactorSetupFoundationID"].Value.ToString());
                                }
    
                                specialFactorSetUp.IntendedCourse = new Course();
                                specialFactorSetUp.IntendedCourse.CourseID = _registerdCourseId;
                                
                                specialFactorSetUp.RegisteredCourse = _returnCourse;
                                specialFactorSetUp.EnrolmentIntendedCourse = new Course();
                                specialFactorSetUp.EnrolmentIntendedCourse.CourseID = _selectedIntendedCourse;
                                specialFactorSetUp.SpecialFactor = Convert.ToDecimal(resultGrid["SpecialFactor", i].Value);
                                specialFactorSetUp.SpecialFactorDescription = Convert.ToString(resultGrid["Description", i].Value);
                                specialFactorSetUp.Status = "Active";
                                specialFactorSetUp.CreatedBy = new User();
                                specialFactorSetUp.CreatedBy.UserId = AuthenticatedUser.UserSingleton.GetSingletonObject().userID;
          
                                    _newNCSSpecialFactorData.Add(specialFactorSetUp);    
                                

    Parameter passing

                    SqlCommand command = new SqlCommand("P_Foundation_AddNormalisedCompositeScoreSetup", connection);
                    command.CommandType = CommandType.StoredProcedure;
    
                    command.Parameters.Add("@enrolmentIntendedCourseId", SqlDbType.Int);
                    command.Parameters["@enrolmentIntendedCourseId"].Value = CompositeScoreData.EnrolmentIntendedCourse.CourseID;
    
    command.Parameters.Add("@intendedCourseId", SqlDbType.Int);
                    command.Parameters["@intendedCourseId"].Value = CompositeScoreData.IntendedCourse.CourseID;
    
                    command.Parameters.Add("@weightingEducationalQualification", SqlDbType.Decimal);
                    command.Parameters["@weightingEducationalQualification"].Value = CompositeScoreData.WeightingEducationalQualification;
    
                    command.Parameters.Add("@weightingAdmissionTest", SqlDbType.Decimal);
                    command.Parameters["@weightingAdmissionTest"].Value = CompositeScoreData.WeightingEducationalAdmissionTest;
    
                    command.Parameters.Add("@status", SqlDbType.VarChar);
                    command.Parameters["@status"].Value = CompositeScoreData.Status;
    
                    command.Parameters.Add("@createdBy", SqlDbType.Int);
                    command.Parameters["@createdBy"].Value = CompositeScoreData.CreatedBy;
    
    
                    command.ExecuteNonQuery();

                    SqlCommand command = new SqlCommand("P_Foundation_AddNormalisedCompositeScoreSpecialFactorSetup", connection);
                    command.CommandType = CommandType.StoredProcedure;
    
    command.Parameters.Add("@enrolmentIntendedCourseId", SqlDbType.Int);
                    command.Parameters["@enrolmentIntendedCourseId"].Value = CompositeScoreData.EnrolmentIntendedCourse.CourseID;
    
    command.Parameters.Add("@intendedCourseId", SqlDbType.Int);
                    command.Parameters["@intendedCourseId"].Value = CompositeScoreData.IntendedCourse.CourseID;
    
                    command.Parameters.Add("@specialFactor", SqlDbType.Decimal);
                    command.Parameters["@specialFactor"].Value = CompositeScoreData.SpecialFactor;
    
                    command.Parameters.Add("@specialFactorDescription", SqlDbType.VarChar);
                    command.Parameters["@specialFactorDescription"].Value = CompositeScoreData.SpecialFactorDescription;
    
                    command.Parameters.Add("@status", SqlDbType.VarChar);
                    command.Parameters["@status"].Value = CompositeScoreData.Status;
    
                    command.Parameters.Add("@createdBy", SqlDbType.Int);
                    command.Parameters["@createdBy"].Value = CompositeScoreData.CreatedBy;
    
    
                    command.ExecuteNonQuery();


    Thursday, November 22, 2018 10:30 AM

All replies

  • Take a look at your CompositeScoreData.CreatedBy object you have. Is it of type User? That is the problem. You're trying to pass a complex object to a parameter that requires an int. So you need to use the property on User that represents the user's integral ID instead of just CreatedBy.

    command.Parameters["@createdBy"].Value = CompositeScoreData.CreatedBy.Id;
    
    //FYI, easier way to create parameters
    //command.Parameters.Add("@createdBy", SqlDbType.Int);
    //command.Parameters["@createdBy"].Value = CompositeScoreData.CreatedBy.Id;
    
    command.Parameters.AddWithValue("@createdBy", CompositeScoreData.CreatedBy.Id);
    


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, November 22, 2018 4:21 PM
  • Yes, you are right CoolDadTx. Thank you.
    Wednesday, November 28, 2018 10:01 AM
  • Yes, you are right CoolDadTx. Thank you.

    Well, reward the poster and mark his post as 'answered'.
    Wednesday, November 28, 2018 10:13 AM