I am having trouble verifying a field inserted into a table in a SQL database with a parameter

0

In the part of else where it says

//Si tiene registros vamos a comparar el parámetro con el campo rfc 

I have doubts if if the comparison is made like this, the query if it brings me the data well, and the rfc that I try to enter is one that already exists in the bd, but in the comparison of if (RFC == (DR["rfc"]).ToString()) , it seems that he does not find coincidences, I'm wrong, because he does not enter that if, he flies to the else below.

    [HttpGet]
    public Respuesta InsertRazonSocial(string RFC, string Regimen, string Nombre, Guid llaveEmp)
    {
        Respuesta re = new Respuesta();
        try
        {
            if (miConexion.State == ConnectionState.Closed)
            {
                miConexion.Open();
            }
            SqlCommand cmd = new SqlCommand("select top 1 * from RazonesSociales where rfc = '" + RFC + "'", miConexion);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds, "RazonesSociales");
            DataRow DR;
            //var tabla = ds.Tables["RazonesSociales"];
            if (ds.Tables[0].Rows.Count == 0)
            {
                //Si está vacía la tabla inserta!
                SqlCommand comando = new SqlCommand("insert into RazonesSociales (rfc,nombre,Regimen1,llaveEmp,llave) values (@rfc,@nombre,@Regimen1,@llaveEmp, newid())", miConexion);
                comando.Parameters.AddWithValue("@rfc", RFC);
                comando.Parameters.AddWithValue("@nombre", Nombre);
                comando.Parameters.AddWithValue("@Regimen1", Regimen);
                comando.Parameters.AddWithValue("@llaveEmp", llaveEmp);
                comando.ExecuteReader();
                re.Estatus = true;
            }
            else
            {
                //Si tiene registros  vamos a comparar el parámetro con el campo rfc y si son iguales no inserta
                DR = ds.Tables["RazonesSociales"].Rows[0];
                if (RFC == (DR["rfc"]).ToString())
                {
                    re.Estatus = false;
                    re.Mensaje = "RFC ya registrado.";
                }
                else
                {
                    //Si son diferentes lo inserta
                    SqlCommand comando = new SqlCommand("insert into RazonesSociales (rfc,nombre,Regimen1,llaveEmp,llave) values (@rfc,@nombre,@Regimen1,@llaveEmp, newid())", miConexion);
                    comando.Parameters.AddWithValue("@rfc", RFC);
                    comando.Parameters.AddWithValue("@nombre", Nombre);
                    comando.Parameters.AddWithValue("@Regimen1", Regimen);
                    comando.Parameters.AddWithValue("@llaveEmp", llaveEmp);
                    comando.ExecuteReader();
                    re.Estatus = true;
                }
                miConexion.Close();
            }
        }
        catch (Exception ex)
        {
            re.Error = ex;
            re.Estatus = false;
            miConexion.Close();
        }
        return re;
    }
    
asked by Oscar Navarro 01.11.2017 в 00:39
source

1 answer

0

I could already do it, you see that accessing the DataRow itemarray in position 0 and converting it to string, to be able to make the comparison, I have no idea because with the method I had before it did not work, according to me, it was correct

                string RFCBD = DR.ItemArray[0].ToString();
                if (RFCBD == RFC)
                {
                    re.Estatus = false;
                    re.Mensaje = "El RFC ingresado, ya fue registrado.";
                }
    
answered by 01.11.2017 в 19:12