Reputation: 95
I have a function in a class inside BLL which return a bool status but I also want to get the value of output parameter that it gets from Store procedure but how?
public Boolean InsertPersonalInfo()
{
SqlCommand SqlCom = new SqlCommand("InsertPersonalInfo",
DatabaseConnection.OpenConnection());
SqlCom.CommandType = CommandType.StoredProcedure;
SqlParameter SqlParameterPersonalInfoID = new SqlParameter("@PersonalInfoID_Returned",
SqlDbType.Int);
SqlCom.Parameters.Add(SqlParameterPersonalInfoID);
SqlParameterPersonalInfoID.Direction = ParameterDirection.Output;
SqlCom.ExecuteNonQuery();
DatabaseConnection.CloseConnection();
return ReturnStatus;
}
Now I want to get SqlParameterPersonalInfoID
's value in a .cs page from where I am calling this but it also should return bool value as it does.
Upvotes: 1
Views: 84
Reputation: 2602
You need to use an SqlDataReader to read that values returned from your query or just gram the single value like this:
public Boolean InsertPersonalInfo(string NIC, string Name, string FatherHusbandName, DateTime DOB, bool Gender, string Religion, string Domicile, string PhoneResidence,
string PhoneOffice, string Mobile, bool MaritalStatus, string Address, string EmailAddress, bool ComputerLiterate, short UserID)
{
SqlCommand SqlCom = new SqlCommand("InsertPersonalInfo", DatabaseConnection.OpenConnection());
SqlCom.CommandType = CommandType.StoredProcedure;
SqlParameter SqlParameterPersonalInfoID = new SqlParameter("@PersonalInfoID_Returned", SqlDbType.Int);
SqlCom.Parameters.Add(SqlParameterPersonalInfoID);
SqlParameterPersonalInfoID.Direction = ParameterDirection.Output;
var yourVar = SqlCom.Parameters["@PersonalInfoID_Returned"].Value; //<---
SqlCom.ExecuteNonQuery();
DatabaseConnection.CloseConnection();
return ReturnStatus;
}
Upvotes: 1
Reputation: 26856
You can return it with out
parameter in C# almost similar to usage of it in SQL.
Something like:
public Boolean InsertPersonalInfo(your_parameters, out int personalInfoID)
{
//your code
personalInfoID = SqlCom.Parameters["@PersonalInfoID_Returned"].Value;
return ReturnStatus;
}
and then you're calling it like:
int id;
bool result = InsertPersonalInfo(...., out id);
See MSDN for reference about out
parameter modifier.
Upvotes: 2