Last post Apr 16, 2019 08:16 AM by Ryan_
Apr 12, 2019 10:24 AM|Ryan_|LINK
New to coding in both C# and MVC, working on a project to create a login form. Read a few things online and not seen much which is applied in the way i would like to do it. I am using SQL Server to store the a hashed password and a stored procedure to validate
user input. This works with test data in SQL. I would like to take user input and use it to run the stored procedure and relay the response message back to the user.
Apr 12, 2019 11:00 AM|PatriceSc|LINK
And so the problem is about how to call this SP from ADO.NET ? Or you could use Entity Framework or maybe even https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/introduction-to-aspnet-identity#aspnet-identity
The later would allow to have a defined API for handling that allowing to replace or use the same model in all your app regardless of how user data are handled behing the scene.
Apr 15, 2019 07:48 AM|Brando ZWZ|LINK
According to your description, I suggest you could try to use ADO.NET in MVC to call the SP and get the output parameter.
More details, you could refer to below codes:
//Read the connection string from Web.Config file
string ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnectionString))
//Create the SqlCommand object
SqlCommand cmd = new SqlCommand("uspLogin", con);
//Specify that the SqlCommand is a stored procedure
cmd.CommandType = System.Data.CommandType.StoredProcedure;
//Add the input parameters to the command object
//Add the output parameter to the command object
SqlParameter outPutParameter = new SqlParameter();
outPutParameter.ParameterName = "@responseMessage";
outPutParameter.SqlDbType = System.Data.SqlDbType.Int;
outPutParameter.Direction = System.Data.ParameterDirection.Output;
//Open the connection and execute the query
//Retrieve the value of the output parameter
string ResponseMessage = outPutParameter.Value.ToString();
Apr 16, 2019 08:16 AM|Ryan_|LINK
Hi Brando, Thanks for the reply. This works perfectly for calling the stored procedure in and manages to get a response message from the database, However, i am not sure where to place it? I had read it is bad practice to have it in the Model, but i think
where i have it (controller) is also bad practice. Where should this be placed? I am also unsure how to store user input from a textbox to then pass into the cmd.Parameters.AddWithValue command object. Any help would be greatly appreciated. Cheers, Ryan.