bob jason tiamsic

bob jason tiamsic

  • NA
  • 12
  • 7.6k

Object Reference Not Set To An Instance Object in Gridview

Apr 2 2014 9:34 PM
Hi. I have a grid view that has gridviews in it.
 
here's the code for you to understand more.
 
[CODE]
 
<!--Main grid--> 
<asp:GridView ID="EmpLeaveAppGV2" runat="server" CssClass="datagrid gridfont" AutoGenerateColumns="False" GridLines="none"
CellPadding="4" ForeColor="#333333" EmptyDataText= "No current record found in the database." Width="868px" DataKeyNames="EmployeeNo" OnRowDataBound="gv2RowBound_GetLeaveDetails"><%----%>
<Columns>
<asp:TemplateField HeaderStyle-BorderStyle="None" ItemStyle-Width="10" ItemStyle-CssClass="tRight" ItemStyle-Horizontal>
<ItemTemplate>
<img title = "View Details" style="cursor: pointer" src="../Images/plus.png" />
<asp:Panel ID="pnlRegDetails" runat="server" Style="display:none;"> <!-- Plus sign panel -->
<div style="padding:2px 2px 2px 2px;">
<div style="border:1px solid #E0AB72;">
<!-- grid for leave details -->
<asp:GridView ID="gv2LeaveDetails" CellPadding="2" runat="server" ShowHeader="True" Width="1132px" GridLines="both" CssClass = "ChildGrid" DataKeyNames="LeaveTranId" OnRowDataBound="gv2RowBound_GetAddLeaveDetails"> <%--OnRowDataBound="gv2RowBound_GetAddLeaveDetails"--%>
<EmptyDataRowStyle BackColor="#E3EAEB" BorderColor="#1C5E55" HorizontalAlign="Center" ForeColor="#4B6C9E" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField ItemStyle-Height="18" DataField="LeaveType" HeaderText="Leave Type" HeaderStyle-CssClass="tLeft" ControlStyle-CssClass="tLeft" />
 ....other columns
 
 <!--grid for remarks and approvedby-->
<asp:TemplateField ItemStyle-Width="30px" ItemStyle-HorizontalAlign="center" HeaderText="Details" HeaderStyle-CssClass="tCenter">
<ItemTemplate>
<img title = "View Details" style="cursor: pointer" src="../Images/plus.png" />
<asp:Panel ID="pnlRegDetails" runat="server" Style="display:none;">
<div style="padding:5px 0px 5px 0px;">
<div style="border:1px solid #E0AB72; width:686px;">
 
<!--grid for remarks and approvedby-->
<asp:GridView ID="gv2LeaveDetails2" runat="server" Width="686px" GridLines="None" AutoGenerateColumns="false" CssClass = "ChildGrid">
<EmptyDataRowStyle BackColor="#E3EAEB" BorderColor="#1C5E55" HorizontalAlign="Center" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField ItemStyle-Height="18" DataField="Remarks" HeaderText="Leave Reason" HeaderStyle-CssClass="tLeft" ControlStyle-CssClass="tLeft" />
 
....................... 
 
    
 
[/CODE] 
 
 
Then im trying to populate the grids by these lines of codes in code behind:
 
[CODE]
//this block binds the main grid
 
protected void bindLeaveApplication3()
{
string from = cboCutOffSelect.SelectedValue.Split('-')[0];
string to = cboCutOffSelect.SelectedValue.Split('-')[1];
using (SqlConnection con = new SqlConnection(DAO.ConnectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("dbo.DisplayAdminTimeKeeping_OTLeaveView", con))
{
cmd.Parameters.AddWithValue("@Status", 55);
cmd.Parameters.AddWithValue("@DateFrom", from);
cmd.Parameters.AddWithValue("@DateTo", to);
//sql = "Select empApproved.LeaveTranId, empApproved.EmployeeNo, empApproved.eName, empApproved.LeaveType,empApproved.ApplicationDate ,MIN(empDate.LeaveDate) as LeaveDateFrom, MAX(empDate.LeaveDate) as LeaveDateTo, empApproved.WholeOrPartial, empApproved.Status, empApproved.DateApproved, empApproved.Remarks,(Select (empInfo.LastName + ', ' + empInfo.FirstName) as ApprovedBy from dbo.EmpPersonalInfo empInfo where empInfo.UserName = empApproved.ApprovedBy) as ApprovedBy from ISMPayroll.dbo.EmpLeaveDates empDate inner join ISMPayroll.dbo.viewEmployeeLeaveDatesForApproved empApproved on empDate.LeaveTranId = empApproved.LeaveTranId where empDate.LeaveDate BETWEEN (Select ISMPayroll.dbo.CutOff_Period.CutOff_From from ISMPayroll.dbo.CutOff_Period where CutOffID='" + cutoffid + "') AND (Select ISMPayroll.dbo.CutOff_Period.CutOff_to from ISMPayroll.dbo.CutOff_Period where CutOffID='" + cutoffid + "') and empApproved.Status ='Approved' and empApproved.EmployeeNo='" + empNo + "' group by empApproved.LeaveTranId,empApproved.EmployeeNo, empApproved.eName, empApproved.LeaveType , empApproved.WholeOrPartial, empApproved.Status, empApproved.DateApproved, empApproved.Remarks, empApproved.ApprovedBy,empApproved.ApplicationDate";
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dt.DefaultView.Sort = "EmpName Asc";
EmpLeaveAppGV2.DataSource = dt;
EmpLeaveAppGV2.DataBind();
}
}
}
 
//this block is for rowbound of main grid
protected void gv2RowBound_GetLeaveDetails(object sender, GridViewRowEventArgs e)
{
int cutoffid = getCutOffID(cboCutOffSelect.SelectedValue);
if (e.Row.RowType == DataControlRowType.DataRow)
{
string empNo = EmpLeaveAppGV2.DataKeys[e.Row.RowIndex].Value.ToString(); //get datakey based on datakeyname of Main Grid (EmpLeaveAppGV2)
GridView gv2LeaveDetails = e.Row.FindControl("gv2LeaveDetails") as GridView; //find the grid view within Main grid which is to be populated
using (SqlConnection con = new SqlConnection(DAO.ConnectionString))
{
con.Open();
string sql;
sql = "Select empApproved.LeaveTranId, empApproved.EmployeeNo, empApproved.EmpName, empApproved.LeaveType,empApproved.ApplicationDate ,MIN(empDate.LeaveDate) as LeaveDateFrom, MAX(empDate.LeaveDate) as LeaveDateTo, empApproved.WholeOrPartial, empApproved.Status, empApproved.DateApproved, empApproved.Remarks,(Select (empInfo.LastName + ', ' + empInfo.FirstName) as ApprovedBy from dbo.EmpPersonalInfo empInfo where empInfo.UserName = empApproved.ApprovedBy) as ApprovedBy from ISMPayroll.dbo.EmpLeaveDates empDate inner join ISMPayroll.dbo.viewEmployeeLeaveDatesForApproved empApproved on empDate.LeaveTranId = empApproved.LeaveTranId where empDate.LeaveDate BETWEEN (Select ISMPayroll.dbo.CutOff_Period.CutOff_From from ISMPayroll.dbo.CutOff_Period where CutOffID='" + cutoffid + "') AND (Select ISMPayroll.dbo.CutOff_Period.CutOff_to from ISMPayroll.dbo.CutOff_Period where CutOffID='" + cutoffid + "') and empApproved.Status ='Approved' and empApproved.EmployeeNo='" + empNo + "' group by empApproved.LeaveTranId,empApproved.EmployeeNo, empApproved.EmpName, empApproved.LeaveType , empApproved.WholeOrPartial, empApproved.Status, empApproved.DateApproved, empApproved.Remarks, empApproved.ApprovedBy,empApproved.ApplicationDate";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
gv2LeaveDetails.DataSource = dt;
gv2LeaveDetails.DataBind();
}
}
}
 
 
//this block is for datarowbound of second grid to get the remarks and approvedby
 
protected void gv2RowBound_GetAddLeaveDetails(object sender, GridViewRowEventArgs e)
{
int cutoffid = getCutOffID(cboCutOffSelect.SelectedValue);
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv2LeaveDetails = EmpLeaveAppGV2.FindControl("gv2LeaveDetails") as GridView; //find gv2LeaveDetails
string LeaveTranId = gv2LeaveDetails.DataKeys[e.Row.RowIndex].Value.ToString(); //getEmpNo DataKey based on DataKeyName from the found control (gvLeaveDetails)  -----------------IM Getting the error here
GridView gv2LeaveDetails2 = e.Row.FindControl("gv2LeaveDetails2") as GridView; //find gvLeaveDetails2 (for remarks and approved by)
using (SqlConnection con = new SqlConnection(DAO.ConnectionString))
{
con.Open();
string sql;
sql = "Select empApproved.LeaveTranId, empApproved.EmployeeNo, empApproved.eName, empApproved.LeaveType,empApproved.ApplicationDate ,MIN(empDate.LeaveDate) as LeaveDateFrom, MAX(empDate.LeaveDate) as LeaveDateTo, empApproved.WholeOrPartial, empApproved.Status, empApproved.DateApproved, empApproved.Remarks,(Select (empInfo.LastName + ', ' + empInfo.FirstName) from dbo.EmpPersonalInfo empInfo where empInfo.UserName = empApproved.ApprovedBy) as ApprovedBy from ISMPayroll.dbo.EmpLeaveDates empDate inner join ISMPayroll.dbo.viewEmployeeLeaveDatesForApproved empApproved on empDate.LeaveTranId = empApproved.LeaveTranId where empDate.LeaveDate BETWEEN (Select ISMPayroll.dbo.CutOff_Period.CutOff_From from ISMPayroll.dbo.CutOff_Period where CutOffID='" + cutoffid + "') AND (Select ISMPayroll.dbo.CutOff_Period.CutOff_to from ISMPayroll.dbo.CutOff_Period where CutOffID='" + cutoffid + "') and empApproved.Status ='Approved' and empApproved.LeaveTranId='" + LeaveTranId + "' group by empApproved.LeaveTranId,empApproved.EmployeeNo, empApproved.eName, empApproved.LeaveType , empApproved.WholeOrPartial, empApproved.Status, empApproved.DateApproved, empApproved.Remarks, empApproved.ApprovedBy,empApproved.ApplicationDate";
//sql = "Select LeaveTranId, Remarks, (Select (empInfo.LastName + ', ' + empInfo.FirstName) from dbo.EmpPersonalInfo empInfo where empInfo.UserName = empApproved.ApprovedBy) as ApprovedBy from ISMPayroll.dbo.viewEmployeeLeaveDatesForApproved empApproved where empApproved.LeaveTranId = '" + LeaveTranId + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
gv2LeaveDetails2.DataSource = dt;
gv2LeaveDetails2.DataBind();
}
}
}
 
 
[/CODE] 
 
I cant track why i am getting this error.
 
Please help me, any would be appreciated!
 
Thanks and God bless!! 

Answers (4)