Avatar of MarkH12518
MarkH12518

asked on 

long record insert into Access using c#

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);
        }
C#Microsoft Access

Avatar of undefined
Last Comment
MarkH12518
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

You do NOT fill an autoincrement field yourself, not even with a null value.  Access fills that field for you.
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.
Avatar of MarkH12518
MarkH12518

ASKER

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);
        }
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of MarkH12518
MarkH12518

ASKER

thanks
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo