Reputation:
I am authenticating account using if exists query and print true if the account is authenticate else print false.
if exists (select email, password
from client
where email = '[email protected]' and password = '123')
print 'true'
else
print 'false';
It prints true or false in SQL Server Management Studio, but when I authenticate account from my website, the data table rows and columns count is always 0. What is the problem, and how can I fix it?
public bool ExecuteReader(string query)
{
bool result = false;
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(query, con);
con.Close();
dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 1 && dt.Columns.Count == 1)
{
if (dt.Rows[0][0].ToString() == "true")
{
result = true;
}
else
{
result = false;
}
}
return result;
}
Upvotes: 0
Views: 262
Reputation: 4824
try this one and also the close code after the everything is done.
SELECT
ISNULL((SELECT
'true'
FROM client
WHERE email = '[email protected]'
AND password = '123')
, 'false')
Upvotes: 0
Reputation: 1
You query uses print 'true' else print 'false'. The print statement does not return a row of data; use select 'true' else select 'false' instead.
In addition, you are closing the connection before executing the command which fills the data table; try moving the con.Close() after the da.Fill(dt).
Upvotes: 0
Reputation: 46213
PRINT
returns a message rather than a result set (which requires a SqlConnection.InfoMessage event handler). USE a SELECT
statement instead to load the result into a DataTable
:
IF EXISTS(SELECT 1
FROM dbo.client
WHERE email = '[email protected]' AND password = '123'
)
SELECT 'true'
ELSE
SELECT 'false';
Upvotes: 2