title: Unable to cast object of type 'System.DBNull' to type 'System.String'.
body: <p>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: </p>
  
  <p>"Unable to cast object of type 'System.DBNull' to type 'System.String'."</p>
  
  <p>Any help would be really appreciated</p>
  
  <pre><code>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();
  
  }
  </code></pre>
  
  <p>/STORED PROCEDURE/</p>
  
  <pre><code>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
  </code></pre>