I am getting the following error, even though I have checked many times I cannot fathom why would such thing pop up when I am only searching by a string, I already checked that when filling the Local object below the properties are correct.
[TestMethod]
public void TestSearchLocalName()
{
try
{
IEnumerable lstLocales = gestor.searchLocal("Local1");
Assert.IsNotNull(lstLocales);
Console.Write("Se ha conseguido una lista de locales.");
}
catch (Exception ex)
{
Assert.Fail(ex.ToString());
}
}
Method at gestor:
public IEnumerable<Local> searchLocal(String name)
{
try
{
return LocalRepository.Instance.getByName(name);
}
catch (DataAccessException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new Exception(String.Format("Error: {0}", ex.Message));
}
}
Repository:
public IEnumerable<Local> getByName(String name) {
List<Local> lista = new List<Local>();
try
{
DataTable tablaResultado = DataBaseAccess.advanceStoredProcedureRequest("pa_local_buscar_nombre", new SPP[] {
new SPP("loc_nombre", name.ToString())
});
if (tablaResultado.Rows.Count > 0)
{
foreach (DataRow fila in tablaResultado.Rows)
{
lista.Add(new Local(
int.Parse(fila.ItemArray[0].ToString()),
fila.ItemArray[1].ToString(),
fila.ItemArray[2].ToString(),
fila.ItemArray[3].ToString(),
fila.ItemArray[4].ToString(),
int.Parse(fila.ItemArray[5].ToString()),
int.Parse(fila.ItemArray[6].ToString())
));
}
}
else
{
lista.Add(new Local());
}
}
catch (Exception ex)
{
throw new Exception(String.Format("SQL Error: {0}", ex.Message));
}
return lista;
}
The stored procedure does work when I test it directly using the same name in he unit test, leading me to believe the error is not here but just in case:
CREATE PROCEDURE [dbo].[pa_local_buscar_nombre]
@loc_nombre as nchar(20)
AS
BEGIN
SET NOCOUNT ON;
SELECT
loc_id, loc_nombre, loc_url, loc_telefono,
loc_descripcion, loc_preferencia, loc_provincia
FROM
Locales
WHERE
loc_nombre LIKE '%' + @loc_nombre + '%'
END
Exception:
Test Name: TestSearchLocalNombre
Test FullName: UT.UnitTestLocales.TestSearchLocalNombre
Test Outcome: Failed
Test Duration: 0:00:00.0568857
Result Message:Assert.Fail failed. System.Exception: Error: SQL Error: Error converting data type nvarchar to int.
at BLL.GestorLocales.searchLocal(String name)
EDIT:
Advanced stored procedure:
public static DataTable advanceStoredProcedureRequest(String name, SPP[] parameters = null)
{
String storedProcedure = "dbo." + name;
SqlCommand cmd = new SqlCommand(name, getConection);
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null && parameters.Length > 0)
{
foreach (SPP p in parameters)
{
cmd.Parameters.AddWithValue("@" + p.Name, p.Value);
}
}
DataTable table = new DataTable();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(table);
return table;
}
EDIT2:
SSP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class SPP
{
public String Name { get; set; }
public String Value { get; set; }
/**
* Stored Procedure Parameter
*/
public SPP(String name, String value)
{
this.Name = name;
this.Value = value;
}
}
}
Debugger;
SOLVED:
In the end it was human stupidity and careleness which led to this whole conundrum, I thank you all who helped me reach the right conclusion.