I'm starting a development on asp.net MVC C #.
What I need to know is how I can, from the controller, call a subroutine of the model. The idea is to pass as a parameter a key, and that it recovers the corresponding description that is in a database.
The query to the database is made with sqlcommand
.
I already created a public class ( public class buscadato
) but it does not do anything, I do not know what I'm doing wrong or what I'm missing.
Can you provide me with an example of how to define the class and how to call it from the controller to pass and return a string?
IN THE MODEL: public class search Country { public string WCVEPAI; public string WDESPAI;
public buscarPais(string WDATO1, string WDATO2)
{
WCVEPAI = WDATO1;
WDESPAI = WDATO2;
}
public string xbuscarPais()
{
WDESPAI = "x";
string CONBD = ConfigurationManager.ConnectionStrings["BDPROADIN"].ConnectionString;
var BDCON = new SqlConnection(CONBD);
BDCON.Open();
var INSSQL = "SELECT * FROM COAPI WHERE CVEPAI = " + WCVEPAI;
var CMDSQL = new SqlCommand(INSSQL, BDCON);
SqlDataReader reader = CMDSQL.ExecuteReader();
if (reader.Read())
{
WDESPAI = (string)reader["DesPai"];
}
else
{
WDESPAI = "No existe en catalogo.";
}
System.Console.WriteLine(WDESPAI);
reader.Close();
BDCON.Close();
WDESPAI = "AB";
return WDESPAI;
}
}
}
IN THE CONTROLLER:
CatPai BP = new CatPai();
buscarPais bp = new buscarPais(WCVEPAI,WDESPAI);