I have gridview using asp.net with c# .
It show data of table in sql server . When I updated certain row I got this message:
Object reference not set to an instance of an object.
I tried many codes, but non worked ..
can you help me to figure out this problem. Thanks.
This is gridview code
<asp:GridView ID="gvIqamaAlert" runat="server" AutoGenerateColumns="False"
DataKeyNames="EmpNo" onrowcancelingedit="gvIqamaAlert_RowCancelingEdit"
onrowediting="gvIqamaAlert_RowEditing" onrowupdating="gvIqamaAlert_RowUpdating"
Width="828px">
<Columns>
<asp:BoundField DataField="Nationality" HeaderText="الجنسيه" ReadOnly="True"
SortExpression="Nationality" />
<asp:TemplateField ControlStyle-BorderStyle="NotSet"
HeaderText="تاريخ الميلاد الهجري">
<EditItemTemplate>
<asp:Label ID="Label21" runat="server"
Text='<%# GetHijri(String.Format("{0:dd-MM-yyyy}", Eval("DOB"))) %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# GetHijri(String.Format("{0:dd-MM-yyyy}", Eval("DOB"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="تاريخ الميلاد" SortExpression="DOB">
<EditItemTemplate>
<asp:Label ID="Label11" runat="server"
Text='<%# String.Format("{0:dd-MM-yyyy}", Eval("DOB")) %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# String.Format("{0:dd-MM-yyyy}", Eval("DOB")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="جهة الاصدار" SortExpression="IDIssPlace">
<EditItemTemplate>
<asp:TextBox ID="txtIssP" runat="server" Text='<%# Eval("IDIssPlace") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("IDIssPlace") %>'></asp:Label>
</ItemTemplate>
<asp:TemplateField HeaderText="الاسم" SortExpression="FName">
<EditItemTemplate>
<asp:TextBox ID="txtfname" runat="server" Text='<%# Eval("FName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("FName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="الرقم الوظيفي">
<EditItemTemplate>
<asp:Label ID="Labe1" runat="server" Text='<%# Eval("EmpNo") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("EmpNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lbtnUpdate" RunAt="server" CommandName="Update"
Text="موافق" />
<asp:LinkButton ID="lbtnCancel" RunAt="server" CommandName="Cancel"
Text="الغاء" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" RunAt="server" CommandName="Edit" Text="تعديل" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
private SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=dbHrSys;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployeeDetails();
}
}
protected void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd = new SqlCommand(" Select IDNo, EmpNo, Nationality, IDIssDate, IDExpDate, IDIssPlace, FName, DOB, Flag FROM Employee WHERE (IDExpDate <= DATEADD(day, 30, GETDATE())) AND (Flag = 2)", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvIqamaAlert.DataSource = ds;
gvIqamaAlert.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvIqamaAlert.DataSource = ds;
gvIqamaAlert.DataBind();
int columncount = gvIqamaAlert.Rows[0].Cells.Count;
gvIqamaAlert.Rows[0].Cells.Clear();
gvIqamaAlert.Rows[0].Cells.Add(new TableCell());
gvIqamaAlert.Rows[0].Cells[0].ColumnSpan = columncount;
gvIqamaAlert.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void gvIqamaAlert_RowEditing(object sender, GridViewEditEventArgs e)
{
gvIqamaAlert.EditIndex = e.NewEditIndex;
BindEmployeeDetails();
}
protected void gvIqamaAlert_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label l = (Label)gvIqamaAlert.Rows[e.RowIndex].FindControl("Label1");
string jobId = gvIqamaAlert.DataKeys[e.RowIndex].Value.ToString();
TextBox fname = (TextBox)gvIqamaAlert.FindControl("txtfname");
TextBox IssP = (TextBox)gvIqamaAlert.FindControl("txtIssP");
////TextBox Issd = (TextBox)gvIqamaAlert.FindControl("txtIssd");
////temp1 = ((TextBox)gvIqamaAlert.FindControl("txtIssd")).Text;
//DateTime Iss = DateTime.ParseExact(Greg(((TextBox)gvIqamaAlert.FindControl("txtIssd")).Text), "dd/MM/yyyy", null);
//TextBox Expd = (TextBox)gvIqamaAlert.FindControl("txtExpd");
//temp2 = Greg(Expd.ToString());
//DateTime Exp = DateTime.ParseExact(temp2, "dd/MM/yyyy", null);
con.Open();
SqlCommand cmd = new SqlCommand("Update Employee set FName='" + fname.Text + "',IDIssPlace='" + IssP.Text + "' where EmpNo='" + Convert.ToString(l.Text) + "'", con);
cmd.Parameters.AddWithValue("@id", jobId);
//,IDIssDate = @iss, IDExpDate = @exp
//cmd.Parameters.AddWithValue("@iss",Iss);
//cmd.Parameters.AddWithValue("@exp", Exp);
cmd.ExecuteNonQuery();
con.Close();
gvIqamaAlert.EditIndex = -1;
BindEmployeeDetails();
}
protected void gvIqamaAlert_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvIqamaAlert.EditIndex = -1;
BindEmployeeDetails();
}