Hi,
change your code for inserting bill:
internal static int InsertBill(Bills bill)
{
try
{
const string query = "INSERT INTO billdata(Party,BillNo, BillDt,Amt,DueDt,PaidOn,Remarks) VALUES(@Party, @BillNo,@BillDt,@Amt,@DueDt,@PaidOn,@Remarks) returning id;";
var args = new Dictionary<string, object>
{
{"@Party", bill.Party},
{"@BillNo", bill.BillNo},
{"@BillDt", bill.BillDt},
{"@Amt", bill.Amt2},
{"@DueDt", bill.DueDt},
{"@PaidOn", bill.PaidOn},
{"@Remarks", bill.Remarks},
};
var id = ExecuteWrite(query, args);
bill.AId = id;
return id;
}
catch (Exception ex)
{
throw ex;
}
}
Another version:
internal static SQLiteConnection GetConnection() { return new SQLiteConnection("Data Source=test.db");}
internal static int InsertBill(Bills bill)
{
int numberOfRowsAffected;
using (var con = GetConnection())
{
con.Open();
using (var cmd = new SQLiteCommand("INSERT INTO billdata(Party,BillNo, BillDt,Amt,DueDt,PaidOn,Remarks) VALUES(@Party, @BillNo,@BillDt,@Amt,@DueDt,@PaidOn,@Remarks);", con))
{
cmd.Parameters.AddWithValue("@Party", bill.Party);
cmd.Parameters.AddWithValue("@BillNo", bill.BillNo);
cmd.Parameters.AddWithValue("@BillDt", bill.BillDt);
cmd.Parameters.AddWithValue("@Amt", bill.Amt2);
cmd.Parameters.AddWithValue("@DueDt", bill.DueDt);
cmd.Parameters.AddWithValue("@PaidOn", bill.PaidOn);
cmd.Parameters.AddWithValue("@Remarks", bill.Remarks);
numberOfRowsAffected = cmd.ExecuteNonQuery();
}
if (numberOfRowsAffected == 1) using (var cmd = new SQLiteCommand("SELECT last_insert_rowid();", con))
bill.Id = (int)(Int64)cmd.ExecuteScalar();
}
return numberOfRowsAffected;
}