Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:48:56+00:00 2026-06-11T18:48:56+00:00

I have a problem at hand here that I can’t seem to figure out.

  • 0

I have a problem at hand here that I can’t seem to figure out. I have this Button which fires a OnClick. On this event I’m calling a stored procedure in the SQL server. Before creating a new record I have a check in place to see if the already exist. If it does then I’m outputting an error. This part works fine but when record doesn’t exist I get this error in ASP.NET:

“Unable to cast object of type ‘System.DBNull’ to type ‘System.String’.”

Any help would be really appreciated

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:48:57+00:00Added an answer on June 11, 2026 at 6:48 pm

    Try the short version of IF

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

    or simply

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

    or

    var result = cmd.Parameters["@error"].Value;
    message = ((result == DBNull.Value) || (result == DBNull.Value)) ? string.Empty : result.ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi guys I have a problem at hand that I can't seem to figure
Hope someone will give me a hand with this problem I have. So here
I have a problem that is getting embarrassingly out of hand. I'm working with
I hope someone can give me a hand figuring this out. I have been
Problem : I have a hand held device that scans those graphic color barcodes
Hoping someone can give me a hand here.... I have the DataTables jquery plugin
Here is the problem. I have rectangular canvas that have size of 1. So
I have a problem at hand which requires me to spawn a command prompt
I have a problem at hand, at first sight it looks easy and it
I have a problem in my android application, I have a hand page, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.