0

I have 2 set of data's like Master and Details. I Stored both details separately in two tables eg Table1 & Table2. I have query to insert data's into Table1 and then want to get UniqueId from Table1 and then using that UniqueId Table2 is get stored.

Example

INSERT INTO Table1(TempId, Name, Address, Language) VALUES (1, "XYZ", "ABCD", "English");

//Now Table is like this
UniqueId   TempId   Name   Address        Language
1          1        Ram    3rd Street     Hindi
2          2        Paul   1st Street     Hindi
3          1        XYZ    ABCD           English

Here UniqueId is Primary Key Auto Increment. After storing Table1, I get UniqueId using TempId & Language. Then insert data's into Table2.

int id=UniqueId; // This value comes from MS Access Database
INSERT INTO Table2(UniqueId, Detail1, Details2, Detail3) VALUES (id, "Det1", "Det2", "Det3");

This is my task. Now problem is I have code to get UniqueId that part didn't work, Stores Table1 and then directly stores Tables2. When I open Table2 in Access Database UniqueId set to zero.

If I use break point to check the errors, now line by line execute and it retrieve UniqueId from Table1 after it stores and then all data's stored in Table2 successfully. What is problem here ? While storing we cont able to get data? If I use break point it will works fine why? Help me if possible.


UniqueId is Auto Increment Primary key in Table1 It generate if we store a new row into Table1.

INSERT INTO Table1()VALUES().... 

thn i have code to get UniqueId SELECT UniqueId FROM Table1 WHERE MyCondition.....

INSERT INTO Table2(Id,...)VALUES(id,...)....
Srihari
  • 2,387
  • 9
  • 51
  • 82
  • can you please provide what is the error? – İsmet Alkan Oct 30 '14 at 14:40
  • It wont show any errors, Simply store 0 in Access database UniqeId – Srihari Oct 31 '14 at 05:40
  • how do you get UniqueId when inserting to Table2? can you provide the code parts? – İsmet Alkan Oct 31 '14 at 07:35
  • I updated my question. Check is it clear or ? – Srihari Oct 31 '14 at 08:19
  • what is _yourcondition_? can you get the UniqueId successfully with that? as I understand you can get the UniqueId when debugging, but other times it stores 0 as UniqueId. am I right? this is not a hard problem, but I'm just making random guesses here. if you can provide some ACTUAL code, I can point you to some direction. – İsmet Alkan Oct 31 '14 at 08:23
  • Yeah perfect. Get the UniqueId when debugging, but other times it stores 0 as UniqueId. – Srihari Oct 31 '14 at 08:33
  • 1
    you do the insert operation, then select the UniqueId. insert operation hasn't been finished yet, so you get 0 as default. you need to submit your change to the database; make sure the insertion is complete, only then select the UniqueId. while debugging, process is slow and insertion is submitted, but while running normally, you try to select while/before the insertion. please provide the C# code to point you to the right direction. – İsmet Alkan Oct 31 '14 at 08:45

1 Answers1

0

I have only a little experience in MS Access, but you can try this:

INSERT INTO Table2(Detail1, Details2, Detail3) VALUES ("Det1", "Det2", "Det3");

As ID is automatically assigned. This is the case in most of the databases I've used.

İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64
  • No my requirement is want to get `id` from **Table1** and use that `id` and store into **Table2**. – Srihari Oct 30 '14 at 14:21
  • This all process must done in a button click. – Srihari Oct 30 '14 at 14:21
  • if you will do the insertion only by that machine, it won't matter. just do the insertion to table2 after table1, as they will be holding the same Id and giving the same seed. – İsmet Alkan Oct 30 '14 at 14:38