Auto Increment id in c# with text


Auto Genrate ID With String

Exp:- PONO123

1) Declar string str = "";

2) Copy Past Following Code in Form Load Event / Form Button Event

 //autoid with string
public string Purchase_Order_Autoid()
    {
        try
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["jeeyaappreal"].ConnectionString);
            con.Open();
            string userqry = "";
            userqry = "Select max(substring(Po_No,5,20))from Po_Master";
            int i = 0;
            SqlCommand cmd = new SqlCommand(userqry, con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    memberid = dr[0].ToString();
                }
                if (memberid == "")
                {
                    str = "PONO0000001";
                }
                else
                {
                    i = Convert.ToInt32(memberid);
                    if (i >= 1 && i < 9)
                    {
                        i = i + 1;
                        str = "PONO000000" + i;
                    }
                    else if (i >= 9 && i < 99)
                    {
                        i = i + 1;
                        str = "PONO00000" + i;
                    }
                    else if (i >= 99 && i < 999)
                    {
                        i = i + 1;
                        str = "PONO0000" + i;
                    }
                    else if (i >= 999 && i < 9999)
                    {
                        i = i + 1;
                        str = "PONO000" + i;
                    }

                    else if (i >= 9999 && i < 99999)
                    {
                        i = i + 1;
                        str = "PONO00" + i;
                    }

                    else if (i >= 99999 && i < 999999)
                    {
                        i = i + 1;
                        str = "PONO0" + i;
                    }
                    else if (i >= 999999 && i < 9999999)
                    {
                        i = i + 1;
                        str = "PONO" + i;
                    }
                }
            }

        }

        catch (SqlException es)
        {
            es.GetBaseException();
        }

        finally
        {
            con.Close();
        }
        return str;
    }
     

3) Copy Past Following Code Button Click Event

txtcompanycode.Text = str.Tostring();

0 comments:

Post a Comment