C#TextBox.text返回null

C#TextBox.text返回null,c#,asp.net,C#,Asp.net,我正在尝试为一个网页做一个编辑器。但是,每当我尝试从gridview中的文本框访问文本时,我都会收到错误“对象引用未设置为对象的实例”。我的代码如下所示 SqlConnection con = new SqlConnection(DatabaseClient.ConnectionString); try { int id = (int)CompoundTable.DataKeys[e.RowIndex].Value;

我正在尝试为一个网页做一个编辑器。但是,每当我尝试从gridview中的文本框访问文本时,我都会收到错误“对象引用未设置为对象的实例”。我的代码如下所示

 SqlConnection con = new SqlConnection(DatabaseClient.ConnectionString);
            try
            {
            int id = (int)CompoundTable.DataKeys[e.RowIndex].Value;



            TextBox name = CompoundTable.Rows[e.RowIndex].FindControl("name") as TextBox;
                TextBox cat = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("cation_quantity");
                TextBox catname = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("cation");
                TextBox an = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("anion");
                TextBox anName = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("anion_quantity");
                TextBox diff = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("difficulty_level");

                con.Open();
                SqlCommand command = new SqlCommand("UPDATE Compound SET compound_name='" + name.Text  + "' WHERE compound_name = @compound",con); //The line that the error is thrown.
                command.Parameters.Add("@compound", SqlDbType.NVarChar, CompoundName.Text.Trim().Length).Value = CompoundName.Text.Trim();
                command.ExecuteNonQuery();
                con.Close();


                CompoundTable.EditIndex = -1;
                BindGridView();
            }

编辑
取消
更新

看看
CompoundTable.Rows[e.RowIndex].FindControl(“name”)
返回的是什么。我不太了解webforms,但它表明
BoundField
并不是从
TextBox
继承的


使用
as
进行强制转换时,如果转换失败,则不会引发异常,但返回的对象为null。尝试访问空对象上的属性时出现异常。

请查看
CompoundTable.Rows[e.RowIndex].FindControl(“name”)
返回的内容。我不太了解webforms,但它表明
BoundField
并不是从
TextBox
继承的


使用
as
进行强制转换时,如果转换失败,则不会引发异常,但返回的对象为null。尝试访问null对象上的属性时出现异常。

,因为您的name.Text为null

如果您的目标通过单击“更新”按钮来更新对象,您可以通过以下简单方式进行更新:

                protected void YourGridIdHere_RowCommand(object sender, GridViewCommandEventArgs e)
                {
                    // If multiple buttons are used in a GridView control, use the
                    // CommandName property to determine which button was clicked.
                    if(e.CommandName=="Your command in CommandName")
                    {
                      // Convert the row index stored in the CommandArgument
                      // property to an Integer.
                      int index = Convert.ToInt32(e.CommandArgument);

                      // Retrieve the row that contains the button clicked 
                      // by the user from the Rows collection.
                      GridViewRow row = YourGridIdHere.Rows[index];

                      // Get data from current selected row.    
                      int ID_COL = 0; 
                      TableCell wantedData = row.Cells[ID_COL];
                      string str = wantedData.Text;

                      // So on....
                }
在html格式中,您可以通过添加以下内容来运行它:

OnRowCommand="YourGridIdHere_RowCommand"
致你的:

<asp:GridView ID="CompoundTable" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" PageSize="20" OnSorting="CompoundTable_Sorting" OnPageIndexChanging="CompoundTable_PageIndexChanging" OnRowCancelingEdit="CompoundTable_CancelEdit" OnRowEditing="CompoundTable_Edit" OnRowUpdating="CompoundTable_Update" CellPadding="4">

因为您的name.Text为空

如果您的目标通过单击“更新”按钮来更新对象,您可以通过以下简单方式进行更新:

                protected void YourGridIdHere_RowCommand(object sender, GridViewCommandEventArgs e)
                {
                    // If multiple buttons are used in a GridView control, use the
                    // CommandName property to determine which button was clicked.
                    if(e.CommandName=="Your command in CommandName")
                    {
                      // Convert the row index stored in the CommandArgument
                      // property to an Integer.
                      int index = Convert.ToInt32(e.CommandArgument);

                      // Retrieve the row that contains the button clicked 
                      // by the user from the Rows collection.
                      GridViewRow row = YourGridIdHere.Rows[index];

                      // Get data from current selected row.    
                      int ID_COL = 0; 
                      TableCell wantedData = row.Cells[ID_COL];
                      string str = wantedData.Text;

                      // So on....
                }
在html格式中,您可以通过添加以下内容来运行它:

OnRowCommand="YourGridIdHere_RowCommand"
致你的:

<asp:GridView ID="CompoundTable" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" PageSize="20" OnSorting="CompoundTable_Sorting" OnPageIndexChanging="CompoundTable_PageIndexChanging" OnRowCancelingEdit="CompoundTable_CancelEdit" OnRowEditing="CompoundTable_Edit" OnRowUpdating="CompoundTable_Update" CellPadding="4">


非常感谢,这修复了崩溃。非常感谢,这修复了崩溃。