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);
}
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);
}
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.
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);
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks