0

我手头有一个问题,我似乎无法弄清楚。我有这个触发 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();

}

/存储过程/

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
4

2 回答 2

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();
于 2012-09-22T16:32:08.187 回答
1

更短的替代方案:

message = cmd.Parameters["@error"].Value as string ?? "";
于 2012-09-22T17:04:02.393 回答