Reputation: 11
I have used this code:
Stored procedure in SQL:
Alter PROCEDURE [dbo].[Get_Customize_Status_By_OrderId]
@Order_id varchar(50)
AS
BEGIN
select Customize_Status from dbo.ClientOrder where Order_id=@Order_id
END
GO
Calling that stored procedure in c#:
public static bool getcustomizestatusbyorderid(string orderid)
{
bool result = false;
try
{
Database db = new SqlDatabase(connectionstring);
DbCommand cmd1 = db.GetStoredProcCommand("[dbo].[Get_Customize_Status_By_OrderId]");
db.AddInParameter(cmd1, "@Order_id",DbType.String,orderid);
int RowAffected = db.ExecuteScalar(cmd1);
}
catch (Exception ex)
{
}
}
Please tell me how to get customize_status
value in c#?
Upvotes: 0
Views: 2439
Reputation: 1752
To return Boolean values to a C# front end in a way that they can be understood I do this:
SELECT CASE WHEN Customize_Status = 0 THEN 'false' ELSE 'true' END AS [Customize_Status]
Then, regardless of how you get hold of the data in the front end, you can write (let's pretend you returned it as a value in a datarow of a datareader) ...
bool Customize_Status = Convert.ToBoolean(dr["Customize_Status"].ToString());
That is the only way I have ever found to get bools out of SQL Server database and into a C# front end so it can be assigned to a bool.
Upvotes: 0
Reputation: 2754
You can do it in this way
bool exists = (int)db.ExecuteScalar(cmd1) == 1;
return exists;
Upvotes: 0
Reputation: 1063058
ExecuteScalar
returns the first cell selected, so in your example that will be what is returned; all you should need is to cast from object
(which is what ExecuteScalar
returns) to int
:
int status = (int)db.ExecuteScalar(cmd1);
(assuming that Customize_Status
is an int
, etc)
Note that you might want to check for null
which would result from zero rows:
object result = db.ExecuteScalar(cmd1);
if(result == null) {
// no rows
} else if (result is DBNull) {
// at least one row; first cell was a database null
} else {
// at least one row; first cell was non-null
int status = (int)result;
// ...
}
"rows affected" makes me think you are confused with ExecuteNonQuery
, where frankly the return value is rarely meaningful anyway.
Also: ent-lib? really? why?
Upvotes: 1
Reputation: 3844
Try this
bool customize_status = (RowAffected > 0);
If records found, it will return true
Upvotes: 0