hello,
I am following this link Update Null value in database when ASP.Net DropDownList is empty using C# and VB.Net
when i try to change from
<asp:BoundField DataField="id" HeaderText="Customer Id" />
to
<asp:TemplateField HeaderText="م">
<ItemTemplate>
<asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
</ItemTemplate>
<ControlStyle Width="30px" />
<ItemStyle HorizontalAlign="Center" Width="30px" />
</asp:TemplateField>
i get error when i press update
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error:
Line 884:
Line 885:
Line 886: string id = (row.FindControl("lblRowNumber") as Label).Text.Trim();
Line 887: string name = (row.FindControl("nametxt") as Label).Text.Trim();
Line 888: string civilid = (row.FindControl("civilidtxt") as Label).Text.Trim();
|
Source File: C:\Users\kanko\OneDrive\Documents\Visual Studio 2019\gsc\gsc\secretarialop\WebForm5.aspx.cs Line: 886
C#
protected void Update(object sender, EventArgs e)
{
GridViewRow gvRow = (sender as Button).NamingContainer as GridViewRow;
int id = int.Parse(gvRow.Cells[0].Text);
string country = (gvRow.FindControl("ddlCountries") as DropDownList).SelectedItem.Text;
string conString = ConfigurationManager.ConnectionStrings["kankonConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("UPDATE Table_infoname SET groups = @groups WHERE id = @Id", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", id);
if (!string.IsNullOrEmpty(country))
{
cmd.Parameters.AddWithValue("@groups", country);
}
else
{
cmd.Parameters.AddWithValue("@groups", DBNull.Value);
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindGrid();
}