Reputation: 329
I am new to Entity Framework. I create a stored procedure and I added it to my .edmx
file but problem is that when I call this, it causes an exception
The specified cast from a materialized 'System.Int32' type to a nullable 'System.Boolean' type is not valid.
This is in .edmx
file
public ObjectResult<Nullable<global::System.Boolean>> uspLoginWindowsApp(global::System.String userName, global::System.String password, global::System.String userType, ObjectParameter result)
{
ObjectParameter userNameParameter;
if (userName != null)
{
userNameParameter = new ObjectParameter("UserName", userName);
}
else
{
userNameParameter = new ObjectParameter("UserName", typeof(global::System.String));
}
ObjectParameter passwordParameter;
if (password != null)
{
passwordParameter = new ObjectParameter("Password", password);
}
else
{
passwordParameter = new ObjectParameter("Password", typeof(global::System.String));
}
ObjectParameter userTypeParameter;
if (userType != null)
{
userTypeParameter = new ObjectParameter("UserType", userType);
}
else
{
userTypeParameter = new ObjectParameter("UserType", typeof(global::System.String));
}
return base.ExecuteFunction<Nullable<global::System.Boolean>>("uspLoginWindowsApp", userNameParameter, passwordParameter, userTypeParameter, result);
}
This part is my code where I am calling the function:
private void btnLogin_Click(object sender, EventArgs e)
{
if (_context == null)
_context = new ATSdbEntities();
ObjectParameter userTypeParameter = new ObjectParameter("result", false);
// This line here is causing the error
Boolean userExists = (Boolean)_context.uspLoginWindowsApp(txtLogin.Text.Trim(), txtPswd.Text.Trim(), cmbType.SelectedItem.ToString().Trim(), userTypeParameter).Single();
if (userExists)
{
Employee emp = new Employee();
emp.Show();
this.Hide();
}
else
{
MessageBox.Show("Incorrect Credential ");
}
}
my stored procedure are below are looking like below
ALTER PROCEDURE [dbo].[uspLoginWindowsApp]
@UserName varchar(50),
@Password varchar(50),
@UserType varchar(50),
@result bit output
Upvotes: 1
Views: 261
Reputation: 754418
We don't know yet what your stored procedure really looks like - assuming it just takes three varchar
parameters and returns the result as an OUTPUT
parameter:
CREATE PROCEDURE dbo.uspLoginWindowsApp
@UserName VARCHAR(50),
@Password VARCHAR(50),
@UserType VARCHAR(50),
@Result BIT OUTPUT
I then added this stored procedure to a new .edmx
in Visual Studio 2012 and created the function import.
When I call this stored procedure, what I get back from Entity Framework is the return value of the stored procedure (always an INT
), and the result you're looking for will be in the ObjectParameter
that you pass in as the last parameter to your call:
// this parameter will hold the @Result value
ObjectParameter resultParam = new ObjectParameter("result", false);
int returnStatus = ctx.uspLoginWindowsApp("marc_s", "testpwd", "normal", resultParam);
// get the value of the @Result OUTPUT Parameter
bool userExists = resultParam.Value;
and now you have your result - whether or not the user exists.
Upvotes: 1