Get last number from database column and increment it using C# in ASP.Net

micah
 
on Oct 15, 2021 10:55 PM
324 Views

I have this code that increments numbers ones button recipt its clicked.

In a way to generate recipt numbers i use this code to generate numbers as recipt.

But once the numbers reach 100 it stops and it keeps repeating 100 instead 101,102, 103 etc.

Please how do i remove this limitation.

protected void btncallrecipt_Click(object sender, EventArgs e)
{
    string previousIdQuery = "SELECT MAX(Receipt) UniqueId FROM TransID";
    string newId = GenerateNewId(connStr, previousIdQuery);
    txtrecipt.Text = newId;
    txtpin.Text = newId;
    string insertQuery = "INSERT INTO TransID VALUES(" + newId + ")";
    using (SqlConnection con = new SqlConnection(connStr))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(insertQuery, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}
private string GenerateNewId(string connection, string query)
{
    string newId = string.Empty;
    using (SqlConnection con = new SqlConnection(connection))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            string i = dr[0].ToString();
            if (string.IsNullOrEmpty(i))
            {
                newId = "1";
            }
            else
            {
                int j = Convert.ToInt32(i);
                j = j + 1;
                newId = j.ToString();
            }
        }
        con.Close();
    }
 
    return string.Concat(newId);
}

this is how it repeats 100

this is what its inserting 98 99 100 100 100 100 100

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 16, 2021 02:08 AM

Use below code.

protected void btncallrecipt_Click(object sender, EventArgs e)
{
    string connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string previousIdQuery = "SELECT TOP 1 UniqueId FROM TransID ORDER BY CustomerID DESC";
    string newId = GenerateNewId(connStr, previousIdQuery);
    txtrecipt.Text = newId;
    txtpin.Text = newId;
    string insertQuery = "INSERT INTO TransID VALUES(" + newId + ")";
    using (SqlConnection con = new SqlConnection(connStr))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(insertQuery, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

private string GenerateNewId(string connection, string query)
{
    string newId = string.Empty;
    using (SqlConnection con = new SqlConnection(connection))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(query, con);
        string i = Convert.ToString(cmd.ExecuteScalar());
        if (string.IsNullOrEmpty(i))
        {
            newId = "1";
        }
        else
        {
            int j = Convert.ToInt32(i);
            j = j + 1;
            newId = j.ToString();
        }
        con.Close();
    }

    return newId;
}