Reputation: 5
I am trying to make an editor for a webpage. However, whenever I try and access the text from my textboxes in a gridview, I receive the error "object reference not set to an instance of an object." My code is as follows.
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();
}
<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">
<Columns>
<asp:BoundField DataField="id" HeaderText="Compound" SortExpression="name" />
<asp:BoundField DataField="name" HeaderText="Compound" SortExpression="name" />
<asp:BoundField DataField="cation" HeaderText="Cation" SortExpression="cation" />
<asp:BoundField DataField="cation_quantity" HeaderText="Cation Quantity" SortExpression="cation" />
<asp:BoundField DataField="anion" HeaderText="Anion" SortExpression="anion" />
<asp:BoundField DataField="ANION_QUANTITY" HeaderText="Anion Quantity" SortExpression="anion" />
<asp:BoundField DataField="difficulty_level" HeaderText="Difficulty" SortExpression="difficulty_level" />
<asp:HyperLinkField DataNavigateUrlFields="compound_id" DataNavigateUrlFormatString="DeleteCompound.aspx?compound={0}" Text="Delete Compound" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LBEdit" runat="server" CommandName="Edit" >Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LBCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
<asp:LinkButton ID="LBUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Upvotes: 0
Views: 514
Reputation: 107
Because your name.Text is null.
If your target is updating the object by clicking the button update, you could do it by doing the following simple way:
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....
}
In form html, you could run it by adding:
OnRowCommand="YourGridIdHere_RowCommand"
to your:
<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">
Upvotes: 0
Reputation: 25370
Look at what CompoundTable.Rows[e.RowIndex].FindControl("name")
is returning. I don't know webforms too well, but a quick look at the docs shows that BoundField
does not inherit from TextBox
.
When you cast using as
, if the cast fails, no exception is thrown, but the returned object is null. You are getting the exception when trying to access the property on the null object.
Upvotes: 2