无法将类型为“System.DBNull”的对象强制转换为“System.String”。

时间:2012-09-22 16:28:18

标签: asp.net .net sql tsql

我手边有一个问题,我似乎无法弄明白。我有这个按钮可以触发OnClick。在这个事件上,我正在SQL服务器中调用一个存储过程。在创建新记录之前,我会检查是否已经存在。如果是,那我输出错误。这部分工作正常但是当记录不存在时我在ASP.NET中出现了这个错误:

“无法将'System.DBNull'类型的对象强制转换为'System.String'。”

任何帮助都会非常感激

protected void createloctype_Click(object sender, EventArgs e)
{
    con.Open();
    cmd = new SqlCommand("sp_CREATE_LOCATION_TYPE", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@lt_code", SqlDbType.VarChar, 2).Value = loctype.Text;
    cmd.Parameters.Add("@lt_description", SqlDbType.VarChar, 50).Value = loctypedescription.Text;
    cmd.Parameters.Add("@lt_putaway_zone", SqlDbType.VarChar, 5).Value = putaway_zone_txt.Text;
    cmd.Parameters.Add("@lt_rule1", SqlDbType.VarChar, 5).Value = rule1_txt.Text;
    cmd.Parameters.Add("@lt_rule2", SqlDbType.VarChar, 5).Value = rule2_txt.Text;
    cmd.Parameters.Add("@lt_rule3", SqlDbType.VarChar, 5).Value = rule3_txt.Text;
    cmd.Parameters.Add("@lt_rule4", SqlDbType.VarChar, 5).Value = rule4_txt.Text;
    cmd.Parameters.Add("@lt_rule5", SqlDbType.VarChar, 5).Value = rule5_txt.Text;
    cmd.Parameters.Add("@lt_misc1", SqlDbType.VarChar, 500).Value = misc1_txt.Text;
    cmd.Parameters.Add("@lt_misc2", SqlDbType.VarChar, 500).Value = misc2_txt.Text;
    cmd.Parameters.Add("@lt_misc3", SqlDbType.VarChar, 500).Value = misc3_txt.Text;
    cmd.Parameters.Add("@lt_misc4", SqlDbType.VarChar, 500).Value = misc4_txt.Text;
    cmd.Parameters.Add("@lt_misc5", SqlDbType.VarChar, 500).Value = misc5_txt.Text;
    cmd.Parameters.Add("@lt_user", SqlDbType.VarChar, 10).Value = user;
    cmd.Parameters.Add("@error", SqlDbType.VarChar, 250);
    cmd.Parameters["@error"].Direction = ParameterDirection.Output;
    cmd.ExecuteNonQuery();                 
    message = (string)cmd.Parameters["@error"].Value;

    error_message.Text = message;

    con.Close();

}

/ STORED PROCEDURE /

USE [1_WMS]

GO

ALTER PROCEDURE [dbo].sp_CREATE_LOCATION_TYPE
@LT_CODE VARCHAR(5)
, @LT_DESCRIPTION VARCHAR(250)
, @LT_PUTAWAY_ZONE VARCHAR(5)
, @LT_RULE1 VARCHAR(5)
, @LT_RULE2 VARCHAR(5)
, @LT_RULE3 VARCHAR(5)
, @LT_RULE4 VARCHAR(5)
, @LT_RULE5 VARCHAR(5)
, @LT_MISC1 VARCHAR(20)
, @LT_MISC2 VARCHAR(20)
, @LT_MISC3 VARCHAR(20)
, @LT_MISC4 VARCHAR(20)
, @LT_MISC5 VARCHAR(20)
, @LT_USER VARCHAR(20)
, @ERROR VARCHAR(250) OUT

AS
DECLARE
    @LT_DATE VARCHAR(10)
    , @LT_TIME VARCHAR(8)
    , @LT_ROW_COUNT INT

SET @LT_DATE = CONVERT(VARCHAR(10), GETDATE(),101)
SET @LT_TIME = CONVERT(VARCHAR(8), GETDATE(),114)   

/* CHECK FOR EXISTING */
SELECT @LT_ROW_COUNT = COUNT(*)
    FROM LOC_TYPE(NOLOCK)
WHERE lt_code = @LT_CODE

/*  */
IF @LT_ROW_COUNT = 0
    BEGIN
        INSERT INTO LOC_TYPE
            (
                lt_code             , lt_description            , lt_putaway_zone               , lt_rule_1
                , lt_rule_2         , lt_rule_3                 , lt_rule_4                     , lt_rule_5
                , lt_misc1          , lt_misc2                  , lt_misc3                      , lt_misc4
                , lt_misc5          , lt_created_date           , lt_created_time               , lt_created_by
                , lt_modify_date    , lt_modify_time            , lt_modify_by  
            )
            VALUES
            (
                @LT_CODE            , @LT_DESCRIPTION           , @LT_PUTAWAY_ZONE              , @LT_RULE1
                , @LT_RULE2         , @LT_RULE3                 , @LT_RULE4                     , @LT_RULE5
                , @LT_MISC1         , @LT_MISC2                 , @LT_MISC3                     , @LT_MISC4
                , @LT_MISC5         , @LT_DATE                  , @LT_TIME                      , @LT_USER
                , @LT_DATE          , @LT_TIME                  , @LT_USER 
            )
    END

ELSE IF @LT_ROW_COUNT = 1
    BEGIN

        SET @ERROR = 'LOCATIOIN TYPE ALREADY EXIST'

        EXEC sp_CREATE_ERROR_MESSAGE
            'CRT_LTY'       , @ERROR            , @LT_CODE          , @LT_PUTAWAY_ZONE
            , @LT_RULE1     , @LT_RULE2         , @LT_RULE2         , @LT_RULE3
            , @LT_RULE4     , @LT_RULE5         , ''                , ''        
            , @LT_USER
    END


GO

2 个答案:

答案 0 :(得分:3)

试用IF

的简短版本
var result = cmd.Parameters["@error"].Value;
message = (result == DBNull.Value) ? string.Empty : result.ToString();

或只是

var result = cmd.Parameters["@error"].Value;
message = (result == DBNull.Value) ? string.Empty : result.ToString();

var result = cmd.Parameters["@error"].Value;
message = ((result == DBNull.Value) || (result == DBNull.Value)) ? string.Empty : result.ToString();

答案 1 :(得分:1)

更短的选择:

message = cmd.Parameters["@error"].Value as string ?? "";