Я хочу получить последний идентификатор после нажатия кнопки в основном, это BillForm, который покупает вещи, поэтому мне нужно, чтобы идентификатор билла был автоинкрементом.У меня есть 2 таблицы, одна с идентификатором, а другая не для BillDetails
У меня есть этот запрос, чтобы получить последний счет, и он отлично работает в FormLoad для первого добавления Билла
command.CommandText = "select top 1 (purchase_id)+1 from Purchase_bill order by purchase_id desc";
и этот код кнопки для вставки нового счета
private void btn_save_Click(object sender, EventArgs e)
{
try
{
insert.insert_Purchase_bill(Convert.ToDecimal(txt_total.Text),
Convert.ToDateTime(dateTimeBills.Text)
, 1, Convert.ToDecimal(txt_discount.Text),
Convert.ToInt32( combo_supplier.SelectedValue),
Convert.ToInt32(combo_stockroom.SelectedValue));
for (int i = 0; i < data_cashier.Rows.Count; ++i)
{
insert.insert_Purchase_details(Convert.ToInt32(txt_billNumber.Text),
Convert.ToInt32(data_cashier.Rows[i].Cells["item_id"].Value),
Convert.ToInt32(data_cashier.Rows[i].Cells["Quantity"].Value),
Convert.ToDecimal(data_cashier.Rows[i].Cells["Price"].Value),
Convert.ToInt32(data_cashier.Rows[i].Cells["depart_id"].Value)).ToString();
}
MessageBox.Show("تم اضافة الفاتورة بنجاح", "اضافة", MessageBoxButtons.OK, MessageBoxIcon.Information);
resetbill(); //this func to clear the form
}
catch (Exception)
{
MessageBox.Show("لم يتم اضافة الفاتورة", "فشل", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
insert.execute_query(); // this from my INSERT class to close con
}
txt_billNumber.Text = Convert.ToString(select.getLatestPurchaseBill()); //this the same code line which in FormLoad
}
это на самом деле работает нормально, но после добавления нового счета и устранения этой ошибки появляется
нарушение ограничения первичного ключа не может вставить дубликатключ