ASP.Net ExecuteScalar Error: Object reference not set to an instance of an object

vrindavani
 
on Aug 31, 2021 09:02 AM
374 Views

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;
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Andrea
 
on Aug 31, 2021 09:03 AM
Hi @vrindavani,
Please try the following

Error when converting Session to String in ASP.Net: Object reference not set to an instance of an object.

It might help you.

Cheers Andrea.
vrindavani
 
on Aug 31, 2021 09:21 AM

Not getting it.

Can you guide what changes I will have to do in second line of the function GenrateNewId()

 

dharmendr
 
on Aug 31, 2021 11:31 PM

Hi vrindavani,

Refer the modified code.

private void btnsave_Click(object sender, EventArgs e)
{
    DateTime date_inv = date_bill.Value;
    double sgst, cgst;
    if (cmbbilltype.Text == "Purchase")
    {
        // Code to get the last invoiceno from database Start.
        // Replace code with your database.cs
        string strsql = "select top 1 pur_code from tblpurchaseorder order by Pur_code Desc";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
        SqlCommand cmd = new SqlCommand(strsql, con);
        con.Open();
        string lastId = Convert.ToString(cmd.ExecuteScalar());
        con.Close();
        // Code to get the last invoiceno from database End.

        // Call the GenrateNewId to get the latest invoiceno.
        string newId = GenrateNewId(lastId, txtinvoiceno.Text.Trim());
        strsql = "insert into tblpurchaseorder values('" + txtinvoiceno.Text + "','" + date_inv.ToString("yyyy-MM-dd") + "','" + prop.G_party_code.ToString() + "','" + txtparyname.Text + "')";
        db.SaveData(strsql);
    }
}

private string GenrateNewId(string lastId, string invoiceno)
{
    string newId = string.Empty;
    if (string.IsNullOrEmpty(lastId))
    {
        newId = invoiceno;
    }
    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 newId;
}