我想在点击按钮后得到最新的ID,它的BillForm买东西所以我需要比尔ID作为Autoincrement。我有2个表,一个带有身份证,另一个不带BillDetails
我有这个查询来获取最新的账单,并在第一个Bill添加的Form Load中正常工作
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
}
这实际上工作正常但添加新帐单后清除此错误出现
这个代码没有错,因为连接问题总是打开并需要强制关闭“con.close”使用try和catch in the func
public int getLatestBill()
{
DataTable dt = new DataTable();
try
{
command.CommandType = CommandType.Text;
command.CommandText = "getLatestBill";
SqlDataAdapter adapt = new SqlDataAdapter(command);
connect.Open();
adapt.Fill(dt);
}
catch (Exception)
{
throw;
}
finally
{
connect.Close();
}
return Convert.ToInt32(dt.Rows[0][0]);
}