C#.net 这样写的话 为什么插入的数据是空??要怎样改呢??----------------------------
    protected void Button1_Click(object sender, EventArgs e)
    {        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select * from main";
        cmd.Connection = conn;
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmd;
        DataSet ds = new DataSet();
        sda.Fill(ds,"main");
        this.GridView1.DataSource = ds;
        this.GridView1.DataBind();      foreach(GridViewRow row in GridView1.Rows)
        {
        //string userID = row.Cells[0].Text;
        string m_name = row.Cells[2].Text;
        if (m_name != null)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
            con.Open();
            string cmdtext = "insert into main (m_name) values('" + m_name + "'  )";
            SqlCommand cmd1 = new SqlCommand(cmdtext, con);
            cmd1.ExecuteNonQuery();
        }    }
        bind();
    }

解决方案 »

  1.   

    string m_name = row.Cells[2].Text; 
    你跟一下断点,看看m_name取出来的值是不是空的
      

  2.   

    看这个功能的意思是将表里的数据显示到gv,之后又插入。这样成功了数据也重复。而且不知道你这个表有没有作唯一键处理,比如你的userid,如果是唯一,肯定不能插的吧,都为空了
      

  3.   

    string cmdtext = string.format("insert into main (m_name) values('{0}')",m_name)
      

  4.   

    断点:
    第一、string m_name = row.Cells[2].Text; 这里看看m_name值是多少
    第二、string cmdtext = "insert into main (m_name) values('" + m_name + "'  )"; 查看cmdtext此刻是什么,这条语句直接在库里执行是怎样的。
      

  5.   

    断点 
    第一 m_name值为:  null
    第二 值为:  ""
      

  6.   

    没打开数据连接啊
    你第一次没conn.Open();
    打开就行了
    真的不知道你怎么查的出数据不过不知道你为什么要这样去实现...
      

  7.   

    加了
    conn.Open();
    也是不行 
    我要把打开的多条数据 插入到一个表里面
      

  8.   


    那你的row.cell[2]是指的那个啊?我一般不会这么写,我会写成row.cells["列名"]
      

  9.   

    GridView里这个问题比较麻烦,直接找一般都是空,我研究了好久。
    你换成模版列,用label代替,在查找label就好用了。
      

  10.   

    row.cell[2]是 m_name 字段
    按你的写法写成“写成row.cells["列名"]”的话会出现这种报错
    ------------------------------------错误 2 参数“1”: 无法从“string”转换为“int” F:\tool\我的文档\delphi\project\c#\OAoffice\mainupdate1.aspx.cs 68 39 F:\...\OAoffice\
      

  11.   

    直接自己调试得了 
    这种不太难的问题需要自己解决  ...
    看看连接字符串对不对啊...查出来的数据对不对啊
    有没有执行sql啊...把每一步都仔细检查一下..
      

  12.   

    this.GridView1.DataSource = ds; 
    应该是this.GridView1.DataSource = ds.Table[0];之类的吧你的是BS? 
      

  13.   

      老兄,你单击按钮后main里面的数据能不能正确显示出来吖?我测试过你的程序了,我这里是可以将数据正确读出来而且row.Cells[2].Text里面的值也是可以正确读取到的哦。如果在上面绑定的时候数据不能正确显示请检查你的数据库连接,如果可以正常显示那可能是你的表字段数据类型或其它的的问题引起的吧。不过对你这种在按钮事件中读出来然后又直接将同样的数据插入回到数据库中的做法感到很疑惑。。如果这一行中有其它一些不能为空的字段怎么办呢?
      

  14.   

    谢谢先  我每列都绑定一个TEXTBOX  我先看一下问题在那里
      

  15.   

    修改了语句还是只插入空值???请大虾看一下截图:
    -------------------
    http://community.kingdee.com/images/www_mykingdee_com/leoch/c2.jpg
    http://community.kingdee.com/images/www_mykingdee_com/leoch/c1.jpg
    SQL语句
    --------------
    create table main
    (
    m_id int identity(1000,1) primary key,
    m_name varchar(50))
    insert into main  values('衬衫')用以下方法也是只插入空值
    ---------------------------------    protected void Button1_Click(object sender, EventArgs e)
        {        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
            conn.Open(); 
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select * from main";
            cmd.Connection = conn;
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds,"main");
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
            //string name = "";
            foreach (GridViewRow gr in GridView1.Rows)
            {
                if (gr.RowType == DataControlRowType.DataRow)            {
                    TextBox m_name = (TextBox)gr.FindControl("m_name");
                    //if (m_name != null)
                    {
                        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
                        con.Open();
                        string cmdtext = "insert into main (m_name) values('" + m_name + "')";
                        SqlCommand cmd1 = new SqlCommand(cmdtext, con);
                        cmd1.ExecuteNonQuery();
                       
                    }
                }
            }        bind();
        }
      

  16.   

    根本就不用Open();
    在使用SqlDataAdapter对象的时候,它会
    打开数据源,它是DataSet和数据源的一个桥梁,
    是一个桥接器!
      

  17.   

    m_name 是TextBox类型的变量,是不是应该是SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString); 
    string cmdtext = "insert into main (m_name) values('" + m_name.Text + "')";  //应该是Text属性值吧
    SqlCommand cmd1 = new SqlCommand(cmdtext, con);                            //习惯上应该是先设置好命令参数,在打开数据源
    con.Open(); cmd1.ExecuteNonQuery(); 
    con.Close();                          //应该要关闭数据源吧
      

  18.   

    1,row.Cells[2].Text这种取法我记得是空的啊2,循环开关数据库,数据库要被你折腾死掉的。lz应该把插入语句写好,一次性插入到数据库。
          foreach(GridViewRow row in GridView1.Rows) 
            { 
            //string userID = row.Cells[0].Text; 
            string m_name = row.Cells[2].Text; 
            if (m_name != null) 
            { 
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString); 
                con.Open(); 
                string cmdtext = "insert into main (m_name) values('" + m_name + "'  )"; 
                SqlCommand cmd1 = new SqlCommand(cmdtext, con); 
                cmd1.ExecuteNonQuery(); 
            } 
      

  19.   

    就是这条语句了this.GridView1.DataSource = ds; 你给控件指定的数据源不应该是数据集而是数据集中的具体的某个表所以这样改一下就好了
    this.GridView1.DataSource = ds.Tables ["main"];  
    试试吧。