本文介绍了无法将参数值从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();









推荐答案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 11:41