As the title suggests, I am looking for solutions to add data to multiple tables that have FK constraints all on one form.
Basically I click the magic button and it fires the data over to the relevant tables. See the screenshot for table relationships so you can see the general idea:
I am getting this error:
Cannot insert NULL into BookingID
for the Payments
table. This is a FK relationship, but basically when I press Enter on the Windows form, I want it to generate the primary key in the Booking
table and link it so it saves it all as one booking.
I have inserted an image of the actual booking screen (it's very basic at the moment) but it highlights the tables etc.
I am trying to figure out how i can explain this in a better way, so sorry if it is not very clear. But Basically, The bookings table has CustomerID
and PaymentsID
FK's in it. When I press enter, I want it to send the data over so that if I ran a query on the specific booking ID it would display all the information (Cust name, Payments etc).
Oh and I am doing the Windows Forms in C# which I am very very new to so please be gentle with me.
I was asked to upload some code so here is a little bit. The problem I have is I have no idea how to handle FK constraints using C#.
using (var cmd = new SqlCommand("INSERT INTO Payments (PaymentMethod, PaymentAmount, PaymentDate, PaymentReceived, Discount) VALUES (@PayMethod,@Cost,@PayDate,@PaymentReceived,@Discount)"))
{
cmd.Connection = con;
cmd.Parameters.Add("@PayMethod", SqlDbType.VarChar).Value = PayMeth.Text;
cmd.Parameters.Add("@Cost", SqlDbType.Money).Value = Cost.Text;
cmd.Parameters.Add("@PayDate", SqlDbType.DateTime).Value = PayDatePick.Text;
cmd.Parameters.Add("@PaymentReceived", SqlDbType.VarChar).Value = PayReceived.Text;
cmd.Parameters.Add("@Discount", SqlDbType.VarChar).Value = DiscountText.Text;
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("Record inserted");
}
else
{
MessageBox.Show("Payment failed");
}
}
Is your Booking.BookingId an auto increment or Unique field?
If not when you insert the booking record, you will need to set the value (so its not null).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.