Get Auto Generated Max Id




     // collumn name,  0000000, 0000001, table name
        public string GetMaxId(string coloumName, string zeroPrefix, string initialValue, string tableName)
        {
            string maxId = "";
            string rightStringLength = (zeroPrefix.Length + 1).ToString();
            _selectQuery = "SELECT ISNULL(MAX(RIGHT(" + coloumName + ", " + rightStringLength + ")) + 1, " + initialValue + ") AS " + coloumName + " " + " FROM  " + tableName + " ";

            SQLDAL dal = new SQLDAL();
            DataTable dt = dal.Select(_selectQuery).Data;

            if (dt != null && dt.Rows.Count > 0)
            {
                maxId = decimal.Parse(dt.Rows[0][coloumName].ToString()).ToString(zeroPrefix);
            }

            return maxId;
        }

2.
      public string GetMaxId(string coloumName, string rightStringLength, string initialValue, string tableName)
        {
            string maxId = "";
            _selectQuery = "SELECT ISNULL(MAX(RIGHT(" + coloumName + ", " + rightStringLength + ")) + 1, " + initialValue + ") AS maxID " +
                               " FROM  " + tableName + " ";

            SQLDAL dal = new SQLDAL();
            DataTable dt = dal.Select(_selectQuery).Data;

            if (dt != null && dt.Rows.Count > 0)
            {
                maxId = dt.Rows[0][coloumName].ToString();
            }
         
            return maxId;
        }

Comments

Popular posts from this blog

Provision AWS EC2 Instance and RDS with Terraform, and Deploy Spring Boot App to EC2 Instance via GitHub Action Pipeline

JQuery UI Autocomplete, custom HTML structure for result?

Easy Ui Jquery easyui-textbox change onChange event