views:

328

answers:

2

I have a gridview and i am converting gridview rows to a datatable... But i cant able to get the value of a hiddenfield in cell[0] inside the gridview....

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)));
    dt.Columns.Add(new DataColumn("FromDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("ToDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("DaysPresent", typeof(decimal)));
    dt.Columns.Add(new DataColumn("OpeningAdvance", typeof(double)));
    dt.Columns.Add(new DataColumn("AdvanceDeducted", typeof(double)));
    dt.Columns.Add(new DataColumn("RemainingAdvance", typeof(double)));
    dt.Columns.Add(new DataColumn("SalaryGiven", typeof(double)));
    dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));

    foreach (GridViewRow row in gridEmployee.Rows) 
    {
        DataRow dr = dt.NewRow();
        dr["EmpId"] = Convert.ToInt64(row.Cells[0].Text);
        dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
        dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
        dr["DaysPresent"] = Convert.ToDecimal(row.Cells[4].Text);
        dr["OpeningAdvance"] = Convert.ToDouble(row.Cells[5].Text);
        dr["AdvanceDeducted"] = Convert.ToDouble(row.Cells[6].Text);
        dr["RemainingAdvance"] = Convert.ToDouble(row.Cells[7].Text);
        dr["SalaryGiven"] = Convert.ToDouble(row.Cells[8].Text);
        dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
        dt.Rows.Add(dr);
    }

I got the error in the line,

dr["EmpId"] = Convert.ToInt64(row.Cells[0].Text);

Input String was not in a correct format

Note:

Cells[0] is a hiddenfield which contains EmpId....

 <asp:TemplateField >
   <HeaderStyle Width="1%" />
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>
   <asp:HiddenField ID="HiddenId" runat="server" value='<%#Eval("Emp_Id") %>' />
     <asp:Label ID="LblHiddenId" runat="server" Text='<%#Eval("Emp_Id") %>'></asp:Label>
  </ItemTemplate>
  <ItemStyle Width="1%" CssClass="GridCs" HorizontalAlign="Left" />
  </asp:TemplateField>

My gridview, alt text

+1  A: 

This error means that cells[0].Text doesn't contain a number.

Check the value of row.Cells[0].Text in the debugger.

SLaks
@Slaks it shows `""`
Pandiya Chendur
@SLaks look at my edited picture...
Pandiya Chendur
+2  A: 

You need to add a conditional to make sure you're not parsing the header and the footer:

EDIT: Working result (leaving the other one because it may also apply to similar situations

foreach (GridViewRow row in gridEmployee.Rows) 
{
    DataRow dr = dt.NewRow();
    dr["EmpId"] = Convert.ToInt64(((Label)cells[0].FindControl("LblHiddenId")).Text);
    dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
    dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
    dr["DaysPresent"] = Convert.ToDecimal(row.Cells[4].Text);
    dr["OpeningAdvance"] = Convert.ToDouble(row.Cells[5].Text);
    dr["AdvanceDeducted"] = Convert.ToDouble(row.Cells[6].Text);
    dr["RemainingAdvance"] = Convert.ToDouble(row.Cells[7].Text);
    dr["SalaryGiven"] = Convert.ToDouble(row.Cells[8].Text);
    dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
    dt.Rows.Add(dr);
}  

EDIT: Since I don't have studio on this to correct myself

foreach (GridViewRow row in gridEmployee.Rows) 
{
    if(row.RowType == DataControlRowType.DataRow)
    {
        DataRow dr = dt.NewRow();
        dr["EmpId"] = Convert.ToInt64(row.Cells[0].Text);
        dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
        dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
        dr["DaysPresent"] = Convert.ToDecimal(row.Cells[4].Text);
        dr["OpeningAdvance"] = Convert.ToDouble(row.Cells[5].Text);
        dr["AdvanceDeducted"] = Convert.ToDouble(row.Cells[6].Text);
        dr["RemainingAdvance"] = Convert.ToDouble(row.Cells[7].Text);
        dr["SalaryGiven"] = Convert.ToDouble(row.Cells[8].Text);
        dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
        dt.Rows.Add(dr);
    }
}  
Joel Etherton
@joel look at my image i am looping through my gridview rows in a button click and not in a gridview event...
Pandiya Chendur
@Pandiya: that was just a paste from an event online. Let me edit for your EXACT example.
Joel Etherton
@Joel ok man go ahead ....
Pandiya Chendur
@Joel even that too didnt work for me... Same error...
Pandiya Chendur
@joel The same i used but didnt work...
Pandiya Chendur
@Pandiya: Is this just a boundfield like the others with the attribute Visible="False"?
Joel Etherton
@joel Cells count shows 9 exactly... But i cant get the value there...
Pandiya Chendur
@joel see my edit its a template field...
Pandiya Chendur
@Pandiya: Try ((Label)cells[0].FindControl("LblHiddenId")).Text
Joel Etherton
@joel can you edit it in your answer...
Pandiya Chendur
@joel that worked for me...
Pandiya Chendur
@joel everything works fine until `dr["RemainingAdvance"] = Convert.ToDouble(row.Cells[7].Text);` I calculating this textbox value using javascript....
Pandiya Chendur
@Pandiya: If you're storing it in a textbox you'll need to use the findcontrol method again. I'm not sure this will work though because the JavaScript won't edit the control itself in ViewState. You may be able to access the textbox's value by using its name and going through HttpContext.Current.Request[txtBoxName].
Joel Etherton
@joel i got the answer... When i used rowdatabound event..
Pandiya Chendur