Object reference not set to an instance of an object
Hello,I had a grid view with edit item template with drop down lists and textboxes. When I am clicking "Update" button , I am getting an error "Object reference not set to an instance of an object" pointing at "UpdateRow " method. When I tried to debug I am getting null values in textboxes and dropdownlist .
Code behind:
protected void grdSupList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = grdSupList.DataKeys[e.RowIndex].Value.ToString();
TextBox txt_SupId = (TextBox)grdSupList.FooterRow.FindControl("txtSupId");
TextBox txt_SupName = (TextBox)grdSupList.FooterRow.FindControl("txtSupName");
TextBox txt_City = (TextBox)grdSupList.FooterRow.FindControl("txtCity");
TextBox txt_State = (TextBox)grdSupList.FooterRow.FindControl("txtState");
TextBox txt_Country = (TextBox)grdSupList.FooterRow.FindControl("txtCountry");
DropDownList txt_act = (DropDownList)grdSupList.FooterRow.FindControl("ddlActivity");
DropDownList txt_loc = (DropDownList)grdSupList.FooterRow.FindControl("ddlLocal");
int sqlid = Convert.ToInt32(id);
UpdateRow(sqlid, txt_SupId.Text, txt_SupName.Text, txt_City.Text, txt_State.Text, txt_Country.Text, txt_act.SelectedItem.Text, txt_loc.SelectedItem.Text);
grdSupList.EditIndex = -1;
FillGrid();
lblMsg.Text = "Record updated successfully!";
}
private void UpdateRow(int id, string SupId, string SupName, string city, string state, string country, string status, string location)
{
string connString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
int bitstatus = 0;
if (status == "Yes")
{
bitstatus = 1;
}
else
{
bitstatus = 0;
}
string sql = String.Format("update tblTBT_Vendors set SupplierID ='" + SupId + "', SupplierName= '" + SupName + "', City='" + city + "', StateOrProvince='" + state + "', Country='" + country + "', Inactive = '" + bitstatus + "', LocalRegion='" + location + "' where ID='" + id + "' ");
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.ExecuteNonQuery();
}
}
}