I have defined output parameter as shown below:
C#:
scom.Parameters.Add("@User_ID",SqlDbType.VarChar,8).Direction = ParameterDirection.Output ;
Sql:
@User_ID varchar(8) output
I am getting complete string when executing procedure in Sql Server, but getting only first character in C#. I searched a lot and ensured that size is defined in both C# and Sql. Even I tried with fixed length character (Char(8)), but still getting only first character in C#. Please let me know what is the issue.
C# Code:
public bool CheckPhone(string phoneNumber)
{
SqlConnection myconn=new SqlConnection(connectionString);
try
{
myconn.Open();
SqlCommand scom = new SqlCommand("AuthenticatePhone", myconn);
scom.CommandType = CommandType.StoredProcedure;
scom.Parameters.Add("@phoneNumber", SqlDbType.BigInt).Value = Convert.ToInt64(phoneNumber);
scom.Parameters.Add("@User_ID", SqlDbType.Char, 8).Direction = ParameterDirection.Output;
scom.Parameters.Add("@User_Name", SqlDbType.Char, 120).Direction = ParameterDirection.Output;
scom.ExecuteNonQuery();
if (scom.Parameters["@User_Name"] == null)
{
return false;
}
else
{
UserID = (string)scom.Parameters["@User_ID"].Value;//.ToString();
UserName = (string)scom.Parameters["@User_Name"].Value;//.ToString();
myconn.Close();
return true;
}
}
catch (Exception e)
{
string error = e.InnerException + e.Message;
}
finally
{
myconn.Close();
}
return false;
}
Sql:
Create procedure dbo.AuthenticatePhone
@phoneNumber numeric(11,0) ,
@User_ID varchar(8) output ,
@User_Name varchar(120) output
as
begin
Select @User_ID = convert(varchar(8),[User_ID]) ,
@User_Name = [User_Name]
from dbo.NRE_Users
where PhoneNumber = @phoneNumber
;
print @User_ID
print @User_Name
end
SqlDbType.VarChar?Chardoesn't match your procedure definition.