Generate and save auto incremented number in database using C# in ASP.Net

micah
 
on Oct 05, 2021 05:25 AM
777 Views

I want to generate recipt number by button click and store each recipt generated into table

like 101,102,103, etc

HTML

<asp:Label ID="lblrecipt" runat="server" Text="Recipt"></asp:Label>
<asp:LinkButton ID="btncallrecipt" runat="server">Call Receipt</asp:LinkButton>

 

protected void btncallrecipt_Click(object sender, EventArgs e)
{
    int inserted = 0;
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = str;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                 
                cmd.CommandText = "INSERT INTO ReciptTable (Recipt) VALUES(@Recipt)";
                //cmd.Parameters.AddWithValue("@StaffID", HttpContext.Current.User.Identity.Name);
                cmd.Parameters.AddWithValue("@Recipt", lblrecipt.Text.Trim());
                
 
                con.Open();
                inserted = cmd.ExecuteNonQuery();
                con.Close();
                BindGrid();
                string message = string.Empty;
                {
                    // lblMessage2.Visible = true;
                    /// lblMessage2.Text = "Data Submitted Successfully";
                }
                ScriptManager.RegisterClientScriptBlock(btnsave, this.GetType(), "alert", "<script>alert('Data Submitted Successfully ... !!')</script>", false);
                // lblMessage2.Text = "Inserted Quantity Is Higher than  stock " + GetQuantity().ToString();
            }
        }

 

protected void Insert(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("SELECT MAX(ID) ID FROM TestDemoUser WHERE Users =@Users", con);
    cmd.Parameters.AddWithValue("@Users", txtUserName.Text);
    con.Open();
    int id = Convert.ToInt32(cmd.ExecuteScalar().ToString() == "" ? 0 : cmd.ExecuteScalar());
    if (id == 0)
    {
        cmd = new SqlCommand("INSERT INTO TestDemoUser(ID, CityId, Users) VALUES(@ID, @CityId, @Users)", con);
        cmd.Parameters.AddWithValue("@ID", 1);
        cmd.Parameters.AddWithValue("@Users", txtUserName.Text);
        cmd.Parameters.AddWithValue("@CityId", ddlCityId.SelectedItem.Text);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    else
    {
        id++;
        cmd = new SqlCommand("INSERT INTO TestDemoUser(ID, CityId, Users) VALUES(@ID, @CityId, @Users)", con);
        cmd.Parameters.AddWithValue("@ID", id);
        cmd.Parameters.AddWithValue("@Users", txtUserName.Text);
        cmd.Parameters.AddWithValue("@CityId", ddlCityId.SelectedItem.Text);
        cmd.ExecuteNonQuery();
    }
    cmd = new SqlCommand("SELECT * FROM TestDemoUser", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    this.gvUserDetails.DataSource = dt;
    this.gvUserDetails.DataBind();
    con.Close();

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 06, 2021 01:31 AM

Refer below code.

protected void Insert(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("SELECT MAX(ID) ID FROM TestDemoUser WHERE Users =@Users", con);
    cmd.Parameters.AddWithValue("@Users", txtUserName.Text);
    con.Open();
    string id = Convert.ToString(cmd.ExecuteScalar());
    if (!string.IsNullOrEmpty(id))
    {
        id = (Convert.ToInt32(id) + 1).ToString();
    }
    else
    {
        id = "100";
    }

    cmd = new SqlCommand("INSERT INTO TestDemoUser(ID, CityId, Users) VALUES(@ID, @CityId, @Users)", con);
    cmd.Parameters.AddWithValue("@ID", id);
    cmd.Parameters.AddWithValue("@CityId", ddlCityId.SelectedItem.Text);
    cmd.Parameters.AddWithValue("@Users", txtUserName.Text);
    cmd.ExecuteNonQuery();
    con.Close();

    cmd = new SqlCommand("SELECT * FROM TestDemoUser", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    this.gvUserDetails.DataSource = dt;
    this.gvUserDetails.DataBind();
}