我向access数据库插入数据时,向表中插入九个字段时没错误,但插入十个字段时就出错了,急求高手解决
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class UC_Reg : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {    }    protected void cmdReg_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string con = "Provider=Microsoft.Jet.Oledb.4.0;Data Source="+Server.MapPath("App_Data\\DB_CustomReg.mdb");
            StringBuilder sqltext = new StringBuilder("Insert into CustomReg (Name,TrueName,Sex,PWD,Email,Phone,Address,LikeSort,Usefor,Work) Values ('" + txtName.Text + "'");
            string str = Request.Params["txttruename"];
            if (str == "")
            {
                str = "没填写";
                sqltext .Append (",'"+ str +"'");            }
            else 
            {
                sqltext.Append(",'" + str + "'");
            }
            string sex;
            if (RadioButton1.Checked)
            {
                sex = RadioButton1.Text;
                 sqltext.Append(",'" + sex+ "'");
            }
            else if(RadioButton2.Checked )
            {
                sex = RadioButton2.Text;
                sqltext.Append(",'" + sex+ "'");
            }
            else 
            {
                sex="没有填写";
                sqltext.Append(",'" + sex+ "'");
            }            sqltext.Append(",'" + txtpwd .Text  + "'");
            string email = Request.Params["txtemail"];
            if (email == "")
            {
                email = "没有填写";
                sqltext.Append(",'" + email  + "'");
            }
            else
            {
                sqltext.Append(",'" + email  + "'");
            }
            string phone = Request.Params["txtphone"];
            if (phone == "")
            {
                phone = "没有填写";
                sqltext.Append(",'" + phone  + "'");
            }
            else
            {
                sqltext.Append(",'" + email + "'");
            }
            string address = Request.Params["txtadd"];
            if (address == "")
            {
                address  = "没有填写";
                sqltext.Append(",'" + address  + "'");
            }
            else
            {
                sqltext.Append(",'" + address  + "'");
            }
            string sort = lstSort.SelectedValue;
            string use = lstUse.SelectedValue;
            string work = lstWork.SelectedValue;
            sqltext.Append(",'"+ sort +"'");
            sqltext.Append(",'"+ use +"'");
            sqltext.Append(",'"+ work +"'");            sqltext.Append(")");
            bool success = insertDetail(con, sqltext.ToString());
            if (success)
            {
                Response.Write("<script>alert('用户注册成功');Window.location('Default.aspx');</script>");
            }
            else
            {
                Response.Write("<script>alert('注册失败!');Window.location('Register.aspx');</script>");
            }
        }
      else
        {
            Response.Write("<script>alert('用户资料填写错误!!');Window.location('Register.aspx');</script>");
        }        
        
        
    }
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtpwd.Text = "";
        txtrpwd.Text = "";
        
    }
    protected bool insertDetail(string context, string sql)
    {
        OleDbConnection con = new OleDbConnection(context);
        OleDbCommand cmd = new OleDbCommand(sql, con);
        try
        {
            if (con.State != System.Data .ConnectionState .Closed)
            {
                con.Close();
            }
            con.Open();
            int i = cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception e1)
        {
            string str = e1.Message;
            return false;
        }
        finally
        {
            con.Close();
        }
            
    }
}

解决方案 »

  1.   

    检察一下表里的work 字段, 跟踪一下是哪错了. 
      

  2.   

    string phone = Request.Params["txtphone"];
                if (phone == "")
                {
                    phone = "没有填写";
                    sqltext.Append(",'" + phone  + "'");
                }
                else
                {
                    sqltext.Append(",'" + email + "'");
                }这一句错了email  应该是 phone   吧