Page 1 of 1

How to auto FK when import from excel

Posted: Wed Mar 20, 2024 11:32 am
by tomkris

Hi,

how to fill FK in detail when import data from xls?
list not show the import data because FK is NULL, i don't want to add FK in xls it is possible?

Thanks.


Re: How to auto FK when import from excel

Posted: Thu Mar 21, 2024 7:15 am
by MichaelG

You can use the Row_Import server event to set up the foreign key.


Re: How to auto FK when import from excel

Posted: Thu Mar 21, 2024 4:25 pm
by tomkris

i've try but not work

Here is my table

T_Header

IDH (PK)
FreeText

-T_Detail

IDD (PK)
IDH (FK)
Timestamp
Order
Measure

Excel File Header only 3 column without IDH from T_Header

Timestamp | Order | Measure

I try to use Row_Import

public bool Row_Import(Dictionary<string, object> row, int cnt) {
//Log(cnt); // Import record count
//Log(row); // Import row
//return false; // Return false to skip import
row["IDH"] = ExecuteScalar("SELECT IDH FROM T_Header WHERE IDH ='" + AdjustSql(row["IDH"]) + "'");
}

What i want in T_Detail auto fill IDH

IDD | IDH | Timestamp | Order | Measure


Re: How to auto FK when import from excel

Posted: Fri Mar 22, 2024 7:21 am
by MichaelG

row["IDH"] = ExecuteScalar("SELECT IDH FROM T_Header WHERE IDH ='" + AdjustSql(row["IDH"]) + "'");

The code does not do anything. If row["IDH"] exists, you are just getting the same value. If it does not exist, you does not get anything.

If you need to retrieve the FK, you need to have something in the row data which can relate to the FK.


Re: How to auto FK when import from excel

Posted: Fri Mar 22, 2024 8:22 pm
by tomkris

MichaelG wrote:

row["IDH"] = ExecuteScalar("SELECT IDH FROM T_Header WHERE IDH
='" + AdjustSql(row["IDH"]) + "'");

The code does not do anything. If row["IDH"] exists, you are just getting
the same value. If it does not exist, you does not get anything.

If you need to retrieve the FK, you need to have something in the row data which can
relate to the FK.

Ok Thank you, i develop another app to modified excel file.

is it possible to upload only created table on database?

Example :
In excel i have 5 column
Col1 | Col2 | Col3 | Col4 | Col5

In database i have only 3
Col1 | Col4 | Col5


Re: How to auto FK when import from excel

Posted: Sat Mar 23, 2024 6:48 am
by MichaelG

Use the Page_Importing server event to set up the headers and skip the first header record.


Re: How to auto FK when import from excel

Posted: Sun Mar 24, 2024 10:31 pm
by tomkris

MichaelG wrote:

Use the
Page_Importing
server event to set up the headers and skip the first header record.

Thank you for ur help.