无法将字符串从存储过程转换为整数

时间:2018-07-30 01:15:23

标签: c# sql-server

我需要帮助确定什么原因导致我的代码错误。 “将varchar值'Undergraduate'转换为数据类型int时,转换失败。”当我点击添加按钮时。 我正在努力寻找问题所在。起初它起作用了,但是突然间,错误总是发生。

这是完整的代码;

private void btnadd_Click(object sender, EventArgs e)
        {

            using (SqlConnection conncurr = new SqlConnection(Properties.Settings.Default.connectionstring))
            using (SqlCommand cmdcurr = new SqlCommand("curriculumadd", conncurr))
            {
                try
                {
                    conncurr.Open();
                    var newaccountparam1 = new SqlParameter("@dccode", SqlDbType.VarChar);
                    var newaccountparam2 = new SqlParameter("@dcdesc", SqlDbType.VarChar);
                    var newaccountparam3 = new SqlParameter("@currtitle", SqlDbType.VarChar);
                    var newaccountparam4 = new SqlParameter("@units", SqlDbType.Int);
                    var newaccountparam5 = new SqlParameter("@labunits", SqlDbType.Int);
                    var newaccountparam6 = new SqlParameter("@lecunits", SqlDbType.Int);
                    var newaccountparam7 = new SqlParameter("@yearlevel", SqlDbType.Int);
                    var newaccountparam8 = new SqlParameter("@sem", SqlDbType.Int);
                    var newaccountparam9 = new SqlParameter("@labhrs", SqlDbType.Int);
                    var newaccountparam10 = new SqlParameter("@lechrs", SqlDbType.Int);
                    var newaccountparam11 = new SqlParameter("@acadlevel", SqlDbType.Int);
                    var newaccountparam12 = new SqlParameter("@subjcode", SqlDbType.VarChar);
                    var newaccountparam13 = new SqlParameter("@subjdesc", SqlDbType.VarChar);
                    var newaccountparam14 = new SqlParameter("@subjcat", SqlDbType.VarChar);
                    var newaccountparam15 = new SqlParameter("@curreffectvty", SqlDbType.VarChar);
                    newaccountparam1.Value = txtcoursecode.Text;
                    newaccountparam2.Value = txtdcourse.Text;
                    newaccountparam3.Value = currtitle;
                    newaccountparam4.Value = totalunits;
                    newaccountparam5.Value = txtlabunits.Text;
                    newaccountparam6.Value = txtlecunits.Text;
                    newaccountparam7.Value = yearlevel;
                    newaccountparam8.Value = semestr;
                    newaccountparam9.Value = labhrs;
                    newaccountparam10.Value = lecthrs;
                    newaccountparam11.Value = acadlevel;
                    newaccountparam12.Value = txtsubjcode.Text;
                    newaccountparam13.Value = txtdesctitle.Text;
                    newaccountparam14.Value = cmbsubjfield.Text;
                    newaccountparam15.Value = txteffectivity.Text;
                    cmdcurr.Parameters.Add(newaccountparam1);
                    cmdcurr.Parameters.Add(newaccountparam2);
                    cmdcurr.Parameters.Add(newaccountparam3);
                    cmdcurr.Parameters.Add(newaccountparam4);
                    cmdcurr.Parameters.Add(newaccountparam5);
                    cmdcurr.Parameters.Add(newaccountparam6);
                    cmdcurr.Parameters.Add(newaccountparam7);
                    cmdcurr.Parameters.Add(newaccountparam8);
                    cmdcurr.Parameters.Add(newaccountparam9);
                    cmdcurr.Parameters.Add(newaccountparam10);
                    cmdcurr.Parameters.Add(newaccountparam11);
                    cmdcurr.Parameters.Add(newaccountparam12);
                    cmdcurr.Parameters.Add(newaccountparam13);
                    cmdcurr.Parameters.Add(newaccountparam14);
                    cmdcurr.Parameters.Add(newaccountparam15);
                    cmdcurr.CommandType = CommandType.StoredProcedure;
                    cmdcurr.ExecuteNonQuery();
                }
                finally
                {
                    conncurr.Close();
                    conncurr.Dispose();
                    userlogsmonitor.ActiveForm.Refresh();
                }
            }
        }

将字符串转换为整数的代码;

private void cmbacadlevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbacadlevel.SelectedItem.ToString().Trim())
            {
                case "Undergraduate":
                    acadlevel = 1;
                    break;
                case "Masteral":
                    acadlevel = 2;
                    break;
                case "Doctorate":
                    acadlevel = 3;
                    break;
                case "Senior High School":
                    acadlevel = 4;
                    break;
            }

我尝试编辑代码,但似乎还不够幸运。您能仔细检查一下我的代码,然后尝试给出如何处理它的想法吗?

1 个答案:

答案 0 :(得分:0)

验证以下内容

  • 参数的顺序和类型与存储过程中的相同
  • 使用断点来验证int参数的值
  • 或者使用SQL Server Profiler来检查int参数是否具有有效值。