求助!“System.Data.SqlClient.SqlException”类型的异常在 System.Data.dll 中发生,但未在用户代码中进行处理

时间:2020-12-14 20:29:15
项目是搭了三层写的,报错一直解决不了,在添加信息的时候报错,希望前辈能够指点一下。拜谢。

报错信息:
“System.Data.SqlClient.SqlException”类型的异常在 System.Data.dll 中发生,但未在用户代码中进行处理。其他信息: 列名或所提供值的数目与表定义不匹配。


DAL层:
public int InsertStudent(Student stu)
        {
            string sql = "insert into Student values(@StuNum,@StuName,@StuClass,@Subject,@StuAge,@StuPhone,@StuGender)";
            SqlParameter[] paras = new SqlParameter[]
            {
               
                new SqlParameter("@StuNum",stu.StuNum),
                 new SqlParameter("@StuName",stu.StuName),
                new SqlParameter("@StuClass",stu.StuClass),
               
                new SqlParameter("@Subject",stu.Subject),
                new SqlParameter("@StuAge",stu.StuAge==null?DBNull.Value:(object)stu.StuAge),
                new SqlParameter("@StuPhone",stu.StuPhone),
                new SqlParameter("@StuGender",stu.StuGender)

            };
            int count = SqlHelper.ExecuteNonQuery(sql, paras);
            return count;
        }

BAL层:
public bool InsertStudent(Student stu)
        {
            return dal.InsertStudent(stu) > 0;
        }


UI层:
protected void btnInsert_Click(object sender, ImageClickEventArgs e)
        {
            string stuNum = txtStuNum.Text.Trim();
            string stuName = txtStuName.Text.Trim();
            string stuClass = txtStuClass.Text.Trim();
            string subject = txtSubject.Text.Trim();
            if (string.IsNullOrEmpty(stuNum))
            {
                Response.Write("<script>alert('学号不能为空')</script>");
            }
            else if (string.IsNullOrEmpty(stuName))
            {
                Response.Write("<script>alert('学号不能为空')</script>");
            }
            else if (string.IsNullOrEmpty(stuClass))
            {
                Response.Write("<script>alert('班级不能为空')</script>");
            }
            else if (string.IsNullOrEmpty(subject))
            {
                Response.Write("<script>alert('学科不能为空')</script>");
            }
            else
            {
                if (bll.SelectCount(stuNum))
                {
                    Response.Write("<script>alert('学号重复 ')</script>");
                }
                else
                {
                    int age;
                    Student stu = new Student();
                 
                    stu.StuAge = Int32.TryParse(txtStuAge.Text.Trim(), out age) ? (int?)age : null;
                    stu.StuClass = txtStuClass.Text.Trim();
                    stu.StuGender = radbtnB.Checked ? "男" : (radbtnG.Checked ? "女" : "");
                    stu.StuName = stuName;
                    stu.StuNum = stuNum;
                    stu.Subject = subject;
                    stu.StuPhone = txtStuPhone.Text.Trim();
                    bool isOK = bll.InsertStudent(stu);
                    if (isOK)
                    {
                        Response.Write("<script>alert('修改成功 ')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('修改失败')</script>");
                    }
                }
            }
        }

Web.config
<connectionStrings>
<add name="sqlconstr" connectionString="Data Source=BAILANG; Initial Catalog=itcast; Integrated Security=SSPI"/>
</connectionStrings>

1 个解决方案

#1


你插入的sql字段,和你表里定义的字段类型可能不匹配

#1


你插入的sql字段,和你表里定义的字段类型可能不匹配