protected void Button1_Click(object sender, EventArgs e)
    {
        CommonClass Comm = new CommonClass();
        
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
       // string sqlStr = "insert into MemberUpCornInfo(作物类型,区域,时期,发布时间,作物苗情,苗情分析,生产管理建议,专家名)values(" + zwlx.Text + "," + fbqy.Text + "," + fysq.Text + "," + fbsj.Text + "," + zwmq.Text + "," + mqfx.Text + "," + scgljy.Text + "," + expertName.Text + ")";
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "insert into ExpertUpAdvice(作物类型,区域,时期,发布时间,作物苗情,苗情分析,生产管理建议,专家名)values(" + zwlx.Text + "," + fbqy.Text + "," + fysq.Text + "," + fbsj.Text + "," + zwmq.Text + "," + mqfx.Text + "," + scgljy.Text + "," + expertName.Text + ")";
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
           
                       
           if (cmd.ExecuteNonQuery() != 0)
            {
                Response.Write(Comm.MessageBox("恭喜你!添加成功"));
            }
           else 
            {
                //Response.Write(Comm.MessageBox("恭喜你")); 
            }
            cmd = null;
            conn.Close();
            conn = null;        }
        catch (Exception ex)
        {
            //Response.Write(Comm.MessageBox(ex.ToString()));
            Console.WriteLine(ex.Message);
            Response.Write(Comm.MessageBox("抱歉!未能添加成功,请稍后重试"));
            cmd = null;
            conn.Close();
            conn = null;        }    }
上传按钮中触发事件抛出异常“,”附近有错误,用查询生成器检查说是列表值字段问题,望大家给我指点一下。

解决方案 »

  1.   

    肯定是取到的值和SQL语句拼接处产生的问题,
    打断点跟踪一下生成的最终SQL语句
      

  2.   

    插入字符串,注意一下单引号.
            cmd.CommandText = "insert into [ExpertUpAdvice]([作物类型],[区域],[时期],[发布时间],[作物苗情],[苗情分析],[生产管理建议],[专家名])values('" + zwlx.Text + "','" + fbqy.Text + "','" + fysq.Text + "','" + fbsj.Text + "','" + zwmq.Text + "','" + mqfx.Text + "','" + scgljy.Text + "','" + expertName.Text + "')"; 
      

  3.   

    推荐用@参数
    ...
    cmd.CommandText="insert into table (field,...) values (@value,...)";
    cmd.Parameters.AddWithValue("@value",textBox1.Text);
    ...
      

  4.   

    如果是String类型的拼接的时候要有单引号 
    string sql="insert into a(a) values('"+ba+"')";
      

  5.   

    OleDbConnection MyConnection2 = new OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source="+Server.MapPath(".")+"..\\data\\#cccc.mdb;" );
    string strInsert = "Insert Into yjlb (y_name,y_nr) Values (@T1,@T2)";
    OleDbCommand cmdInsert = new OleDbCommand( strInsert, MyConnection2 );
    cmdInsert.Parameters.Add(new OleDbParameter("@T1", 1150));
    cmdInsert.Parameters["@T1"].Value =TextBox1.Text ;
    cmdInsert.Parameters.Add(new OleDbParameter("@T2", 1150));
    cmdInsert.Parameters["@T2"].Value =TextBox2.Text ;
    MyConnection2.Open();
    cmdInsert.ExecuteNonQuery();
    MyConnection2.Close();以上代码,比你的可读性好多了。
      

  6.   

    明显是SQL语句的问题,注意字段的类型等。最好就是用参数。就像3楼说的。
      

  7.   

    把sql拿到 查询分析器里边执行下就知道了 
      

  8.   

    sql语句错误
    查询分析器里面执行下
      

  9.   

    ('" + zwlx.Text + "','" + fbqy.Text + "','" + fysq.Text + "','" + fbsj.Text + "','" + zwmq.Text + "','" + mqfx.Text + "','" + scgljy.Text + "','" + expertName.Text + "'); 
    还有就是那些数据库的字段用英文比较好点。