Trying to checking if some user exists in my database - c#

I'm trying to check if some user exists in my database or not using this stored procedure:
ALTER PROCEDURE see_if_writer_exsists_or_not
#username nvarchar(50),
#password nvarchar(50)
AS
select count(*)
from writers
where username = #username and password = #password
RETURN
Then I invoke it as a method using Linq-to-SQL with this code:
int b = DS.see_if_writer_exsists_or_not(username.Text, password.Text);
if (b > 0)
{ // the rest of the code...
but a conversion error appears
Cannot implicitly convert type'System.Data.Linq.ISingleResult' to 'int'
How to fix this ?
Note: I've tried Convert.ToInt32 method and it didn't work!

there is no problem with the stored procedure! if u want to check about if this username and password exists
just check if your method returns values or not by using .count()
if (DS.see_if_writer_exsists_or_not(username.Text, password.Text).count() > 0)
{
// yourcode...
}

I have found a solution for this also to extract my retrieved record from the database by using .ElementAt(0)method ,and here is the update in my code :
DS.see_if_writer_exsists_or_not(username.Text, password.Text).ElementAt(0)
by using this I could get the first element retrieved and that's what I wanted

It's just like the error message says:
Your call to see_if_writer_exsists_or_not returns an ISingleResult instance.
A call of ToString() returns "System.Data.Linq.ISingleResult" (which of course cannot be cast to an int).
You must return the query result from your stored proc and regenerate the see_if_writer_exsists_or_not method to accomplish what you want.

ISingleResult is an enumerable. You can loop through it using foreach. You can also do something like this:
ISingleResult<int> results = YourMethodCall();
int returnValue;
if(results.Count()>0){
returnValue = results.First<int>();
}
Also, please remove ToString call from your method call.

Related

Calling Stored Procedure In Entity Framework

I have the following part in the end of a SQL Server stored procedure:
if(#someValue < 0)
begin
SELECT #resultIsSuccess = 0
Return #resultIsSuccess
end
else
begin
SELECT #resultIsSuccess = 1
Return #resultIsSuccess
end
where #resultIsSuccess is of type bit.
So, basically I am returning a bool to indicate if the procedure yielded the intended result.
On the EF side, I configured the Function Import's return type as boolean.
When I call:
bool isSuccess = context.MyFunctionImport(arg1, arg2).FirstOrDefault().Value;
I get the following exception:
The data reader returned by the store data provider does not have
enough columns for the query requested.
What is the problem here?
If you are returning a value, you need a return parameter. Therefore you aren't using a datareader to get your value. You aren't selecting anything therefore you would need to have access to that return parameter.
Instead select your values to populate the datareader since you are probably getting an error when you try to read values from a noninstantiated datareader through EF.
if(#someValue < 0)
SELECT 0
else
SELECT 1
I'm unsure if return values are supported, reference, so you may be trying to do the impossible unless fixed in a newer version.
You have to keep in mind that you are NOT returning a bit datatype from your procedure. The return datatype of a stored procedure is int.
You could greatly simplify your code to a single line return statement.
return case when #someValue < 0 then 0 else 1 end
Then in your code you would need to parse the 0 or 1 to a boolean.
--EDIT--
Since you are looking for the first value you will need to use a select statement in your procedure. Something like this.
Select isSuccess = case when #someValue < 0 then 0 else 1 end

Can't get stored procedure results with Entity Framework 6

I have a stored procedure which returns a 0 or a 1 depending on whether or not a specified email address exists in my database:
CREATE PROCEDURE [DatabaseSchema].[EmailAddressIsDuplicate] (#emailAddress nvarchar(255))
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(
SELECT *
FROM [DatabaseSchema].[EmailUpdatesRegistrant]
WHERE EmailAddress = #emailAddress
)
RETURN 1
ELSE
RETURN 0
RETURN 0
END
GO
And I'm trying to derive the results of this stored procedure from an Entity Framework 6 database context:
using (DatabaseContext dbContext = new DatabaseContext())
{
ObjectParameter param = new ObjectParameter("emailAddress", typeof(bool));
var result = dbContext.EmailAddressIsDuplicate(emailAddress);
}
I'm getting lots of errors.
Error #1: Using the code above, var result is always set to -1.
Error #2: I tried navigated to Edit Function Import and set the Returns a Collection Of to a Boolean scalar value. This throws the following error:
The data reader returned by the store data provider does not have enough columns for the query requested.
Error #3: I went back and set the Edit Function Import return value to None. Then I tried the following code from this answer:
using (DatabaseContext dbContext = new DatabaseContext())
{
var p = new SqlParameter("#emailAddress", emailAddress);
var result = dbContext.Database.SqlQuery<bool>("DatabaseSchema.EmailAddressIsDuplicate", p);
}
No immediate errors thrown, but I have no idea whether or not I can derive useful data from var result. Trying to cast result to bool throws the following error:
Cannot convert type 'System.Data.Entity.Infrastructure.DbRawSqlQuery' to 'bool'
Any ideas on how I can see the results of this stored procedure (0 or 1)?
You could try adding an output parameter (#result) in the stored procedure signature:
CREATE PROCEDURE [DatabaseSchema].[EmailAddressIsDuplicate]
(#emailAddress nvarchar(255), #result bit out)
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT *
FROM [DatabaseSchema].[EmailUpdatesRegistrant]
WHERE EmailAddress = #emailAddress)
SET #result = 1
ELSE
SET #result = 0
RETURN #result
END
GO
(you'll have to re-define your EF Model Function definition accordingly)
using (DatabaseContext dbContext = new DatabaseContext())
{
ObjectParameter isDuplicate = new ObjectParameter("isDuplicate", typeof(bool));
var result = dbContext.EmailAddressIsDuplicate(emailAddress, isDuplicate);
bool emailIsDuplicate = (bool)isDuplicate.Value;.
}
If you want to call the stored procedure directly with an out parameter you could follow this suggestion:
Database.SqlQuery calling stored procedure that has multiple output parameters
REASON - The template builder for EF (including v6) incorrectly sets the SP up as returning an INT containing the row count rather than the return value because it incorrectly calls the wrong ObjectContext.ExecuteFunction (found in the template-generated class YourDatabaseEntities that is the child of the DBContext).
Why wrong ExecuteFunction? - The result set incorrectly says the row count of changed rows rather than the return value or output parameters because it calls a different ExecuteFunction that discards the results. The flyover intellisense hint of the ObjectContext.ExecuteFunction says "Executes a stored procedure ….; discards any results returned from the function; and returns the number of rows affected by the execution" rather than the usual "Executes a stored procedure …. with the specified parameters".
WHY -1: I believe the SET NOCOUNT ON is causing the SP to return no count result and that Microsoft's ExecuteFunction returns that as error code.
SP FIXES - 1) You have to comment out SET NOCOUNT ON .
2) You have to change stored procedure to do the SELECT command as last statement instead of the RETURN command.
SOLUTION FIX - 1) After fixing SP, delete SP from Function Imports folder and the Data Store's SP folder. 2) Reload the SP into the EDMX by using the "Update Model from Database" 3) Rebuild all of your data project where the EDMX resides. 4) Exit Visual Studio and return. 5) Rebuild overall solution.
See: Entity Framework (Database first) has incorrect return result from stored procedure
Implement the stored procedure in C# to a value using parameters.
Resource: https://msdn.microsoft.com/en-us/library/yy6y35y8(v=vs.110).aspx
This way, the values can be stored to a variable from the ExecuteReader.
Add the value to model similar to adding a value to a property. The stored procedure could be called from ActionResult. Though this may require adding the stored procedure to a separate layer, that simply runs the stored procedure and adds the value to model afterwards.
try this
CREATE PROCEDURE [DatabaseSchema].[EmailAddressIsDuplicate] (#emailAddress nvarchar(255))
AS
BEGIN
SELECT *
FROM [DatabaseSchema].[EmailUpdatesRegistrant]
WHERE EmailAddress = #emailAddress
SELECT ##ROWCOUNT
END
GO
using (DatabaseContext dbContext = new DatabaseContext())
{
var result = dbContext.Database.SqlQuery<int32>("exec DatabaseSchema.EmailAddressIsDuplicate {0}", emailAddress).FirstOrDefault();
}
Anything other 0 in the return value indicates there is a match and the number indicates the number of matches

SqlQuery return null every time

I'm trying to execute a store procedure like this:
public bool Test(){
var searchItem=base.Database.SqlQuery<QueryEntity>("exec nameOfMyProcedure #param1={0}",param13).FirstOrDefault();
if(searchItem!=null){
return searchItem.Output1;
}
else{
return false;
}
private class QueryEntity{
public bool Output1{get;set;}
}
I've one Return in the stored procedure
Unfortunately, searchItem always returns null.
Have you an idea how I can resolve that?
I'm using SQL server and C#.
Your stored procedure is returning more that one columns. That can be the reason for the error you are getting.
You can try something like this
base.Database.SqlQuery<IEnumerable<string>>("exec nameOfMyProcedure #param1={0},#param2={1},#param3={2}",param1,param2,param3)
basically this is an example and you need to do is, look into the structure of what your stored procedure returns and then use suitable object for parsing.
Here i am assuming all the returns values are string.
Find the solution.
It's not really proper but.. It's work
I create a new stored procedure which return 1 output like this
Select #Return_value as Return
instead of
Return #Return_value

check to see if a login exists in sql from c#

Within a class I am creating log-ins into sql which works fine. I wanted to add a little error checking to first check to see if the login already existing in SQL. If it does then don't try to add the login but map to the tables etc.
In sql I check for login using the following:
select name from sys.server_principals where name = 'viewer2'
I tired to use this in my class as follows
protected static bool CheckForExistingUser(DbContext context, string userName)
{
string checkForUser =
#" SET NOCOUNT ON
DECLARE #userName AS nvarchar(max) = {0}
EXEC('
SELECT name FROM sys.server_principals where name = ['+ #userName +']
')";
return Convert.ToBoolean(context.Database.ExecuteSqlCommand(checkForUser, userName));
}
However when I call this method I get an exception that the column (being whatever username was passed in) is invalid
$exception {"Invalid column name 'viewer2'."} System.Exception {System.Data.SqlClient.SqlException}
Any ideas as to what is causing this and is there a better way to check to see if a login exists within a sql db from code?
Cheers
You should use ' instead of [] around your value. Otherwise, SQL server treat it as a column name.
string checkForUser =
#" SET NOCOUNT ON
DECLARE #userName AS nvarchar(max) = {0}
EXEC('
SELECT name FROM sys.server_principals where name = ''' + #userName +'''
')";
As #MarcinJuraszek has answered, you can't have '[' around your string parameters. They have to have single quotes or be quoted automatically if sent as parameters.
Since it looks like you're using Entity Framework, I think you can make it more readable by executing a simpler query. In .NET Framework 4.0 and above you could do something like this:
string checkForUser = "SELECT count(1) FROM sys.server_principals where name = {0}";
return context.ExecuteStoreQuery<int>(checkForUser, userName).First() > 0;
Give this a shot:
protected static bool CheckForExistingUser(DbContext context, string userName)
{
return Convert.ToBoolean(context.Database.ExecuteSqlCommand("SELECT name FROM sys.server_principals where name = {0}", userName));
}
count will return a 0 or a 1 depending on whether the username exists satisfying the bool conversion... also removed all the trim in your code. If it doesn't work let me know.

Assign a stored procedure result as a bool variable on C#

Hello Is it possible to store the result of a stored procedure as a true/false value in a bool variable? Something like:
1)
bool var = ExecuteScalar("sp_name", parameters);
or
2)
bool var = ClassName.getValue(parameters);
where
getValue() is a method that has this code:
return dataBase.ExecuteDataSet("sp_name", parameters);
I want to know if a customer has a card or not. I was thinking of using what I have right now which is basically the second option above, and then create a condition whether the row count is more than zero. But I also want to know if there are any other methods I can use.
Thanks in advance.
You can simply do:
DataSet ds = dataBase.ExecuteDataSet("sp_name", parameters);
return ds.Tables[0].Rows.Count>0; //true if record found; false if no rows
Assuming your proc is simply doing a select using the parameters in the where clause. Something like:
select col1 from table a where condition=#parameter
You don't say which DBMS you are using. SQL Server, for example, does not have a bool data type, but you can make your sproc return an int, which you can test for nonzero, e.g.
bool result = ((int) ExecuteScalar("sp_name", parameters)) != 0;
NOTE: I have not tested this, but you could try making the sproc return a bit instead. This is still an integer value as far as SQL Server is concerned, but ADO.NET should detect it and automatically cast it to bool for you. It certainly does for sproc parameters.
ExecuteScalar returns an Object. So no, #1 above would throw an error. Assuming your stored procedure returns a value that can be converted to true/false, you'd have to do something like this:
bool var;
bool.TryParse(ExecuteScalar("sp_name", paramaters).ToString(), out var);

Categories

Resources