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

  • Home
  • SEARCH
  • 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 800019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:13:30+00:00 2026-05-14T23:13:30+00:00

Is DataRow.NewRow() insufficient as the only row in a DataTable? I would expect this

  • 0

Is DataRow.NewRow() insufficient as the only row in a DataTable? I would expect this to work, but it doesn’t. It’s near the end of my Page_Load inside my If(!Postback) block. gridCPCP is GridView

DataTable dt = new DataTable();
dt.Columns.Add("ID", int.MinValue.GetType());
dt.Columns.Add("Code", string.Empty.GetType());
dt.Columns.Add("Date", DateTime.MinValue.GetType());
dt.Columns.Add("Date2", DateTime.MinValue.GetType());
dt.Columns.Add("Filename", string.Empty.GetType());

//code to add rows

if (dt.Rows.Count > 0)
{
    gridCPCP.DataSource = dt;
    gridCPCP.DataBind();
}
else
{
    dt.Rows.Add(dt.NewRow());
    gridCPCP.DataSource = dt;
    gridCPCP.DataBind(); //EXCEPTION
    int TotalColumns = gridCPCP.Rows[0].Cells.Count;
    gridCPCP.Rows[0].Cells.Clear();
    gridCPCP.Rows[0].Cells.Add(new TableCell());
    gridCPCP.Rows[0].Cells[0].ColumnSpan = TotalColumns;
    gridCPCP.Rows[0].Cells[0].Text = "No Record Found";   
}

The exception throws on gridCPCP.DataBind() and only when execution reaches the else block. If there were rows added above via dt.Rows.Add(new object[] { ... } binding works.

System.ArgumentOutOfRangeException: Length cannot be less than zero. Parameter name: length

  • 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-14T23:13:31+00:00Added an answer on May 14, 2026 at 11:13 pm

    Exception is because of the empty row. You should:

    1. Make a new .NewRow()
    2. Add desired fields
    3. Add into the DataTable.

    –

    myRow = dt.NewRow();
    myRow["ID"] = 1001;
    myRow["Code"] = "YourCode";
    
    dt.Rows.Add(myRow);
    gridCPCP.DataSource = dt;
    gridCPCP.DataBind(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ADO.NET has the notorious DataRow class which you cannot instantiate using new. This is
I've found myself increasingly unsatisfied with the DataSet/DataTable/DataRow paradigm in .Net, mostly because it's
Howdy, I have a DataRow pulled out of a DataTable from a DataSet. I
I have a DataSet with a DataTable that correctly fills a single DataRow through
i just use this code to insert value in datatable and pass this table
Is it possible to add a new row to a datatable in c# with
I have a DataRow and I am getting one of the elements which is
I have a DataRow with columns col1 and col2. Each column has a value:
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
When retrieving values from a DataRow is it better to use the column name

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.