2
Answers

How do i increment a number using c# after i have saved it into the database

george wambui

george wambui

12y
8.8k
1
Hi team,

Am having the following the code in c# and i want to increment the number through text box, so when i save the number it should increment automatically, and also when i open the web page again after i have closed it, it should retrieve the previous number which i saved in the database last.

Here is the code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.Odbc;

public partial class staffcode : System.Web.UI.Page
{
            int num = 001;
            OdbcCommand cmd;
            OdbcConnection conn = new OdbcConnection("dsn=parcel;uid=dba;pwd=sql");

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Page.IsPostBack)
        //{        
        //}
         
     //getLastID();
        }
 protected void Btnsave_Click(object sender, EventArgs e)
    {
           OdbcCommand cmd;
            OdbcConnection conn = new OdbcConnection("dsn=parcel;uid=dba;pwd=sql");
            conn.Open();
            cmd = new OdbcCommand("insert into tbl_staffcode(staffcode,fullname,desgination) values ('" +      Txtcode.Text + "','" + txtname.Text + "','" + DropDownList1.Text + "')", conn);
           
            //if (System.Windows.Forms.MessageBox.Show("confrim the data", "prompt", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            cmd.ExecuteNonQuery();
 insert();
            conn.Close();
 }
    private void insert()
{

    string count = Txtcode.Text;
    if (count.Length == 000)
    {
        Txtcode.Text = "001";
    }
    else
    {
        int count1 = Convert.ToUInt16(count);
        int countAdd = count1 + 001;
        Txtcode.Text = countAdd.ToString();
    }
}



Regards
George
Answers (2)