【发布时间】:2015-03-14 04:10:42
【问题描述】:
当我尝试在我的网站中调用我的存储过程表单代码时出现此错误。我已经被困了很长一段时间,因为我不知道我在哪里将一个值转换或声明为整数。这是我的 SQL 语句:
create procedure GetRepPhoneID
@Rep nvarchar(100),
@phoneID nvarchar(100) output
as
set @phoneID = (select concat(CustomerRepPh, '~', cast(RepID as nvarchar(100))) as 'PhoneAndID'
from Reps
where CustomerRep=@Rep)
return @phoneID
go
然后从我后面的 c# 代码中我试图调用存储过程:
公共静态字符串 GetRepPhone(字符串 Rep) { 字符串连接 = WebConfigurationManager.ConnectionStrings["JDC_DatabaseConnectionString"].ConnectionString; SqlConnection sqlConnection = new SqlConnection(Connection);
//This funciton will take all of the values and create them.
try
{
sqlConnection.Open();
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConnection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetRepPhoneID"; //getting the procedure created in SQL.
SqlParameter CustomerParam = new SqlParameter();
CustomerParam.ParameterName = "Rep";
CustomerParam.SqlDbType = SqlDbType.NVarChar;
CustomerParam.Value = Rep;
CustomerParam.Direction = ParameterDirection.Input;
//We are using an output parameter not a return one because it is a string.
SqlParameter ReturnParam = new SqlParameter("phoneID", SqlDbType.NVarChar, 100);
ReturnParam.Direction = ParameterDirection.Output;
cmd.Parameters.Add(CustomerParam);
cmd.Parameters.Add(ReturnParam);
cmd.ExecuteNonQuery();
sqlConnection.Close();
return ReturnParam.Value.ToString();
}
我在我的代码中多次做同样的事情,但它们都返回整数,所以没有抛出错误,所以我知道它应该可以工作。错误被抛出cmd.ExecuteNonQuery() 行。确切的错误是:
Conversion failed when converting the nvarchar value '(111)222-6666~29' to data type int.
我知道我无法将该字符串转换为整数,但我在代码中没有看到我声明整数或尝试转换的任何地方。
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: c# sql asp.net sql-server