运行网站时出现异常消息
“无法将‘System.DBNull’类型的对象转换为‘System.String’类型。”
从'return (string)c.Parameters["@vat"].Value;
以下代码中的 ' 的 'value' 部分出现。我检查了存储过程以查看它是否执行查询,它确实执行并且它的输出也是一个字符串。任何人都可以帮助..??
namespace CamOnlineAccess
{
public class Utilities
{
public SqlConnection GetConnection()
{
SqlConnection c = new SqlConnection();
c.ConnectionString = ConfigurationManager.ConnectionStrings["CamOnlineConnectionString"].ConnectionString;
return c;
}
public SqlCommand GetCommandSP(string SPName)
{
SqlCommand c = new SqlCommand();
c.Connection = GetConnection();
c.CommandType = CommandType.StoredProcedure;
c.CommandText = SPName;
c.CommandTimeout = 150;
return c;
}
}
}
public string ViewUserVat(string code)
{
CamOnlineAccess.Utilities u = new CamOnlineAccess.Utilities();
SqlCommand c = u.GetCommandSP("dbo.ViewUserVat");
c.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {
new System.Data.SqlClient.SqlParameter("@code", System.Data.SqlDbType.VarChar,50),
new System.Data.SqlClient.SqlParameter("@vat",SqlDbType.VarChar, 50, System.Data.ParameterDirection.Output, false, ((byte)(0)), ((byte)(0)), "", System.Data.DataRowVersion.Current, null)});
c.Parameters["@code"].Value = code;
c.Connection.Open();
c.ExecuteScalar();//because we have output parameters
c.Connection.Close();
return (string)c.Parameters["@vat"].Value;
}
循序渐进的程序
ALTER PROCEDURE [dbo].[ViewUserVat]
-- Add the parameters for the stored procedure here
@code varchar,
@vat varchar(50) output
AS
SELECT top 1 @vat=vattable from dbo.portfolio
where owner=@code