这是GridView里的
<asp:TemplateField HeaderText="数量">
       <ItemTemplate>
         <asp:TextBox ID="TextBox1" runat="server" Height="18px" Width="49px" Text="1"></asp:TextBox>
       </ItemTemplate>
</asp:TemplateField>
--------------------------------
在GridView里自己定义了一个按钮.
这是事件 
protected void ImageButton1_Command(object sender, CommandEventArgs e)
    {
        TextBox ceshi = ((TextBox)this.GridView1.FindControl("TextBox1"));
        string strsql;
        strsql = "insert into OrderItems(ProductID,Quantity,UnutCost,UserName)values('" 
            + this.GridView1.Rows[0].Cells[1].Text.ToString() + "','"
            + ceshi.Text.ToString() + "','"
            + this.GridView1.Rows[0].Cells[4].Text.ToString() + "','"
            + Session["name"].ToString() + "')";
        database.execsql(strsql);
    }
----------------------
错误:
System.NullReferenceException: 。-------------
请帮忙看看~~~100分别嫌少.

解决方案 »

  1.   

    错误:
    System.NullReferenceException: 未将对象引用设置到对象的实例。
      

  2.   

    你这个方法不行。
    这个方法只用再databinding和databinded等事件中。
    你可以采用e的属性访问里面控件的值,但是这么写我忘了,几天就忘
      

  3.   

    GridView编辑时获取编辑框中的值
    string quantity = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "check")
            {
                string index= e.CommandArgument.ToString();
                GridViewRow row = GridView1.Rows[index];
                string id = row.Cells[1].Text;
            }
        }
      

  4.   

    最好有个CheckBox列 !!
    这样才知道选择了几行 !!!
    要不就是
       this.GridView1.DataKeyNames = new string[] { "mm_ID" };
     
            GridView1.DataBind();
    设置主键值............
    然后取的时候int id = Convert.ToInt32(this.GridView1.DataKeys[i][0].ToString());
    要是你的模板列像..实现删除,修改等????
    那就直接在RowDeleting,等事件操作 ..
    觉得你的思路错了 ...
      

  5.   

        protected void Button1_Command(object sender, CommandEventArgs e)
        {
            string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
            TextBox ceshi = ((TextBox)this.GridView1.FindControl("TextBox1 "));
            string sql = "insert   into   OrderItems(ProductID,Quantity,UnutCost,UserName)values( ' "
                    + this.GridView1.Rows[0].Cells[1].Text.ToString() + " ', ' "
                    + ceshi.Text.ToString() + " ', ' "
                    + this.GridView1.Rows[0].Cells[4].Text.ToString() + " ', ' "
                    + Session["name "].ToString() + " ') ";
            
            SqlConnection conn = new SqlConnection(strCon);
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            SqlCommandBuilder scb = new SqlCommandBuilder(da);
            DataSet ds = new DataSet();
            da.Fill(ds, "OrderItems");
            ds.Tables[0].NewRow();
            DataRow sdr = ds.Tables[0].NewRow();
        }
    已编译 没问题
      

  6.   

    出现 System.NullReferenceException: 错误,估计是你的
    Session[ "name "].ToString()没有获得值。你代码看着没问题。
      

  7.   

        protected void ImageButton1_Command(object sender, CommandEventArgs e)
        {
            TextBox ceshi = ((TextBox)this.GridView1.FindControl("TextBox1 "));
            string strsql = "insert into OrderItems (ProductID,Quantity,UnutCost,UserName) " +
                        "values ('"
                    + this.GridView1.Rows[0].Cells[1].Text + " ', ' "
                    + this.GridView1.Rows[0].Cells[4].Text + " ', ' "
                    + this.GridView1.Rows[0].Cells[4].Text + " ', ' "
                    + Session["name"].ToString() + " ') ";
            database.execsql(strsql);
        }这样的话就没问题.
    这样明显可以看出来这是GridView中TemplateField里TextBox的取值问题~
    谁能告诉我怎么取得TextBox值啊....
      

  8.   

    结贴!
        
    protected void ImageButton1_Command(object sender, CommandEventArgs e)
        {        
            TextBox txt1 = this.GridView1.Rows[0].FindControl("TextBox1") as TextBox;
            string strsql = "insert into OrderItems (ProductID,Quantity,UnutCost,UserName) " +
                        "values ('"
                    + this.GridView1.Rows[0].Cells[1].Text + " ', ' "
                    + txt1.Text + " ', ' "
                    + this.GridView1.Rows[0].Cells[4].Text + " ', ' "
                    + Session["name"].ToString() + " ') ";
            database.execsql(strsql);
         }这样就OK了.