long record insert into Access using c#

MarkH12518
MarkH12518 used Ask the Experts™
on
Hi, I have a long record i'm trying to add to access using c#

but when I use the table.rows.add using an object it fails at the bottom,

The data in the array lines up with my columns in the datatable, although I'm not 100% confident I'm handling the autoincrement ID in the first field. I'm using "null" bc I'm expecting the database to generate it.

Can you help?

private static DataRow addDR(DataTable prtab, double[] dtAr, int[] CrvID, Int64 sims, double[,] crNs, int rNum)
        {

           
            int ln = dtAr.Length;
            object[] arr = new object[ln+2];
                for (int j = 0; j < ln + 2; j++)
                {
                    if (j == 0)
                    {
                        arr[j] = null;
                    }
                    else if (j == 1)
                    {
                        arr[j] = rNum;
                    }
                    else
                    {
                        arr[j] = crNs[rNum, j - 2];
                    }
                   
                }
            prtab.Rows.Add(arr);  fails here

            return prtab.Rows.Add(arr);
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
You do NOT fill an autoincrement field yourself, not even with a null value.  Access fills that field for you.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Note that you can fill an AutoIncrement field if you must, but it must be unique in the column and as Andy said, it can never be NULL. That said, there is almost never a reason to fill an AutoNumber field - about the only time I could see doing so would be when you need to replicate a table for some reason.

Author

Commented:
I just modified this code to fill a datarow by not touching the ID field. Should this generate a returnable datarow? I'm going to have to reboot my computer as it is not allowing me to copy my updated dll over to the folder I need it. Any advice helpful though.


private static DataRow addDR(DataTable prtab, double[] dtAr, int[] CrvID, Int64 sims, double[,] crNs, int rNum)
        {

           
            int ln = dtAr.Length;
            object[] arr = new object[ln+2];
                for (int j = 1; j < ln + 2; j++)
                {
                    if (j == 1)
                    {
                        arr[j] = rNum;
                    }
                    else
                    {
                        arr[j] = crNs[rNum, j - 2];
                    }
                   
                }
            prtab.Rows.Add(arr);

            return prtab.Rows.Add(arr);
        }
Freelance programmer / Consultant
Commented:
>>Should this generate a returnable datarow?
Yes (providing it doesn't throw an exception = adding failed)
ps.  You are adding the array twice to your table in that code - creating two rows.

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial