INSERT 语句与 FOREIGN KEY 约束冲突FK_Kupovina_Kupac
。冲突发生在数据库OnlineApoteka
、表dbo.Kupac
、列“ID”中。该语句已终止。
我真的很困惑我不知道为什么我们不允许?我在 throw ex 中报告了一个错误;?
public static void Kupi(long lekID, int kolicina, double cena, long nacinIsporukeID, string korisnickoIme)
{
SqlConnection con = new SqlConnection();
try
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["OnlineApotekaConnectionString"].ConnectionString;
con.Open();
string updateLager = @"
UPDATE Lager
SET Kolicina=Kolicina-@Kolicina
WHERE LekID=@LekID";
SqlCommand cmd = new SqlCommand(updateLager, con);
cmd.Parameters.AddWithValue("@LekID", lekID);
cmd.Parameters.AddWithValue("@Kolicina", kolicina);
cmd.ExecuteNonQuery();
string insertIntoKupovina=@"
INSERT INTO Kupovina (KupacID, LekID, Datum, Kolicina, Cena, NacinIsporukeID)
VALUES (@KupacID, @LekID, @Datum, @Kolicina, @Cena, @NacinIsporukeID)";
cmd = new SqlCommand(insertIntoKupovina, con);
cmd.Parameters.AddWithValue("@KupacID", KupacAdapter.GetID(korisnickoIme));
cmd.Parameters.AddWithValue("@LekID", lekID);
cmd.Parameters.AddWithValue("@Datum", DateTime.Now.Date);
cmd.Parameters.AddWithValue("@Kolicina", kolicina);
cmd.Parameters.AddWithValue("@Cena", cena);
cmd.Parameters.AddWithValue("@NacinIsporukeID", nacinIsporukeID);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
我有一个购买按钮的例行事件。当您单击 Kupi 时,应从表中删除 Lager 卷并放置在 Kupac Kupovina
protected void kupiButton_Click(object sender, EventArgs e)
{
KupovinaAdapter.Kupi(Convert.ToInt64(kupovinaGreedView.SelectedDataKey["LekID"].ToString()),
Convert.ToInt32(kolicinaTextBox.Text),
Convert.ToInt64(kupovinaGreedView.SelectedDataKey["Cena"].ToString()),
Convert.ToInt64(nacinIsporukeDropDownList.SelectedValue),
User.Identity.Name);
}
protected void kupovinaGreedView_SelectedIndexChanged(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(kupovinaGreedView.SelectedDataKey["Lek"].ToString()))
{
LekLabel.Text = kupovinaGreedView.SelectedDataKey["Lek"].ToString();
}
if (!String.IsNullOrEmpty(kupovinaGreedView.SelectedDataKey["Kolicina"].ToString()))
{
kolicinaValidator.MaximumValue = kupovinaGreedView.SelectedDataKey["Kolicina"].ToString();
}