autogenerate alphanumeric code string sql server c # asp .net RRS feed

  • Question

  • Good morning

    I need your help I'm creating an application which should generate an automatic code alphanumeric
    I have made this query, but I put error I need to help me the best way to do it and be able to have it ideally is alphanumeric NCL and then the consecutive number I keep two tables one called customer which show the code and another call Gcodigo which is the one that will have the operation I leave the code to help me and there is a way to do it with a stored procedure would be better I am a beginner is this and it is my project of the university.

    using System;
    using System.Collections.Generic;
    using System<g class="gr_ gr_55 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" data-gr-id="55" id="55">.Linq</g>;
    using System<g class="gr_ gr_56 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" data-gr-id="56" id="56">.Text</g>;
    using System.Threading.Tasks;

    using System.Data;
    using System.Data.SqlClient;

    namespace CapaLogicaNegocio.BL
        public class clsGCodigoCliente
            SqlConnection _SqlConnection = null;//me permite establecer comunicacion con bbdd
            SqlCommand _sqlCommand = null; // me permite ejecutar comandos SQL
            SqlDataAdapter _sqlDataAdapter = null; //me permite adaptar conjuntod de datos SQL
            string stConexion = string.Empty;//cadena de conexion 

            SqlParameter sqlParameter = null;

            public clsGCodigoCliente()
                ClsConexion obclsConexion = new ClsConexion();
                stConexion = obclsConexion.getConexion();

            //VALIDAR USUARIO
            public string GeneradorCodigo(string nomtabla)
                SqlConnection cn = instacia.CadenaConexion();
                string resultado = "";
                int ceros = 0;
                string Codigo = "";
                DataSet ds = new DataSet();
                SqlDataAdapter consulta = new SqlDataAdapter("select NUMERO from GCodigoCliente where NombTabla='" + nomtabla + "'", cn);
                consulta.Fill(ds, "GCodigoCliente");
                if(ds.Tables["GCodigoCliente"].Rows.Count > 0)
                    resultado = ds.Tables["GCodigoCliente"].Rows[0][0] + 1.ToString().Trim();
                    ceros = 4 - resultado.Length;
                    switch (ceros)
                        case 0:
                            Codigo = "NCL0000" + resultado;
                        case 1:
                            Codigo = "NCL000" + resultado;
                        case 2:
                            Codigo = "NCL00" + resultado;
                        case 3:
                            Codigo = "NCL0" + resultado;

                return Codigo;


    Thursday, February 14, 2019 6:47 PM

All replies

  • Hi friend,

    Thanks for posting here.

    This forum is about Tests in visual studio.

    As your issue is more related to the asp.net development, I suggest that you could open an new thread on the stackoverflow or Asp.net forum with Asp.net tag. More community members on that forum may further look at your issue and provide more suggestions.

    And if you have some questions about tests in Visual Studio, please feel free to contact us. We will try our best to give you a solution.
    Thanks for your understanding and cooperation. 

    Best Regards


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 15, 2019 7:32 AM