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;
}