Connection string I have done separate in database.cs
here while generating NewID I'm getting error on
string lastId = Convert.ToString(command.ExecuteScalar());
as Object reference not set to an instance of an object.
& also after save it is not auto incrementing the Pur_code
private void btnsave_Click(object sender, EventArgs e)
{
DateTime date_inv = date_bill.Value;
double sgst, cgst;
if (cmbbilltype.Text == "Purchase")
{
strsql = "select top 1 pur_code from tblpurchaseorder order by Pur_code Desc";
GenrateNewId();
strsql = "insert into tblpurchaseorder values('" + txtinvoiceno.Text + "','" + date_inv.ToString("yyyy-MM-dd") + "','" + prop.G_party_code.ToString() + "','" + txtparyname.Text + "')";
db.SaveData(strsql);
}
}
public void GenrateNewId()
{
string NewId = string.Empty;
string lastId = Convert.ToString(command.ExecuteScalar());
if (string.IsNullOrEmpty(lastId))
{
NewId = txtinvoiceno.Text.Trim();
}
else
{
string prefix = lastId.Substring(0, lastId.Length - 3);
string number = lastId.Substring(lastId.Length - 3, 3);
int j = Convert.ToInt32(number);
j = j + 1;
NewId = prefix + j.ToString().PadLeft(3, '0');
}
return;
}