无法将参数值从User转换为Int32 [英] Failed to convert parameter value from a User to Int32

查看:67
本文介绍了无法将参数值从User转换为Int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何克服"无法转换参数值用户到Int32"错误?我在我的数据库中检查过,@ createdBy参数是Int数据类型。

下面是我的用户界面代码的一部分, 另一个是参数传递我的存储过程, 

                    _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);    
                            

参数传递

                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();









推荐答案

看看你的CompositeScoreData.CreatedBy对象。是User的类型吗?那就是问题所在。您正在尝试将复杂对象传递给需要int的参数。因此,您需要在User上使用表示用户的整数
ID而不仅仅是CreatedBy的属性。

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


这篇关于无法将参数值从User转换为Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆