Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 632685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:06:12+00:00 2026-05-13T20:06:12+00:00

I have a gridview and i am converting gridview rows to a datatable… But

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:06:13+00:00Added an answer on May 13, 2026 at 8:06 pm

    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);
        }
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have GridView with Template Column.Inside the template column i have asp:hiddenfield. I am
Have a gridview control which will show X amount of rows. I want to
I have a gridview and and I am creating it dynamically, I get parent
I have a gridview and sqldatasource. I'm trying to insert new records in database
I have a problem with Gridview sorting that is similar to others but I'm
I have gridview which contains 60 rows and each row has 4 radiobutton option
I have GridView and this grid has setup paging, but this paging not show
I have GRIDVIEW in ASP.NET Page and I am converting it into infragistics webdata
I have gridview with ajax rating control where i am updating rating value on
I have gridview which contains merged cell and columns. I am trying to devide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.