Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Variable From Code To Stored Procedure

I am passing a value to a stored procedure, but it keeps giving me an error of error converting data type nvarchar to int. What needs to be altered to remove the error? Below is my actual call, and the stored procedure

private static string ReturnPhoneNumber()
{
string username = "Roy Orbinson";
string databaseConnection = "Data Source=EmployeeServer;Initial Catalog=StoreInfo;Integrated Security=True;MultipleActiveResultSets=True"; 
SqlConnection connection = new SqlConnection(databaseConnection);
SqlCommand command = new SqlCommand("GimmeThatPhoneNumber", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@username", username);

connection.Open();
var result = Convert.ToString(command.ExecuteScalar());
connection.Close();

return result;
}

SQL:

[dbo].[GimmeThatPhoneNumber]
(
@username int
)

as

Set NoCount On

SELECT phonenumber
FROM personelinformation
where employeename = @username
like image 437
MasterOfStupidQuestions Avatar asked Feb 10 '23 02:02

MasterOfStupidQuestions


1 Answers

The issue is that your input parameter has an int type - but needs to be a string type:

[dbo].[GimmeThatPhoneNumber]
(
    @username int
)

@username should be:

@username nvarchar(200) --pick an appropriate size
like image 122
Dave Bish Avatar answered Feb 13 '23 03:02

Dave Bish