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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With