Insert into foreign key table. [Resolved]

Posted by Osadalakimdi under C# on 5/19/2012 | Points: 10 | Views : 2934 | Status : [Member] | Replies : 2
Hello Friends,
I want to insert data into foreign key Table with reference to the last entered primary key.

here is my code:

cmd.CommandText = string.Format("INSERT INTO Guest([FirstName],[LastName],[Adults],[Childs],[PhoneNo]) values(@fname,@lname,@Adults,@Childs,@PhNo) where [ReservationNo]==(Select @@IDENTITY From [Reservation])");
cmd.Parameters.Add("@fname", OleDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@lname", OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@Adults", OleDbType.Numeric).Value = textBox3.Text;
cmd.Parameters.Add("@Childs", OleDbType.Numeric).Value = textBox4.Text;
cmd.Parameters.Add("@PhNo", OleDbType.VarChar).Value = textBox5.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Data Insert!!");
con.Close();

Here is an error:
Syntax error (missing operator) in query expression '[ReservationNo]==(Select @@IDENTITY From [Reservation])'.

Note: I am using VS10, MSACCESS, C#

princeshils


Responses

Posted by: Ajay.Kalol on: 5/21/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
@@IDENTITY return Last affected Tables Primary key which is single.

but if you write "Select @@IDENTITY From [Reservation])"

then value of identity remain same but it will display many time up to records of the Tables.

SO Just Write "Select @@IDENTITY"
AND syntax error is
write
[ReservationNo]=(Select @@IDENTITY From [Reservation])
instead of
[ReservationNo]==(Select @@IDENTITY From [Reservation])

Try it.


Ajay
ajaypatelfromsanthal.blogspot.in

Osadalakimdi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Osadalakimdi on: 5/22/2012 [Member] Starter | Points: 25

Up
0
Down
ThankYou! I Did it.

princeshils

Osadalakimdi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response