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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Resolved the problem of the ''object cannot be cast from dbnull to other types''.
Next
Answered
Lea Taylor
  • 17
Lea Taylor
Asked: May 18, 20222022-05-18T19:43:35+00:00 2022-05-18T19:43:35+00:00In: csharp

Resolved the problem of the ”object cannot be cast from dbnull to other types”.

  • 17

. Advertisement .

..3..

. Advertisement .

..4..

I am tired of fixing the problem: object cannot be cast from dbnull to other types in the csharp; even if I get the reference from another forum, it still returns an error:

*** Start Block *** 
Object cannot be cast from DBNull to other types

To identify the problem, I will show you the detail here:

public Boolean Create(DataTO DataTO)
 {
  IDbTrans transaction = null;
  IDbCmd IDbCmd;
 
  string EncryptedPassword = Encrypt(DataTO.txtPwd);
  Base dataAccCom = null;
 
  try
  {
  dataAccCom = Factory.Create();
  dataAccCom.OpenConnection();
  transaction = dataAccCom.BeginTransaction();
  IDbCmd = dataAccCom.CreateCommand("sp_Register", true);
  dataAccCom.AddParameter(IDbCmd, "op_Id", DbType.Int64, 0, ParameterDirection.Output);
  dataAccCom.AddParameter(IDbCmd, "p_dlstTitle", DbType.String, ReplaceNull(DataTO.dlstTitle));
  dataAccCom.AddParameter(IDbCmd, "p_txtFirstName", DbType.String, ReplaceNull(DataTO.txtFirstName));
  dataAccCom.AddParameter(IDbCmd, "p_txtMiddleName", DbType.String, ReplaceNull(DataTO.txtMiddleName));
  dataAccCom.AddParameter(IDbCmd, "p_txtLastName", DbType.String, ReplaceNull(DataTO.txtLastName));
  dataAccCom.AddParameter(IDbCmd, "p_txtDob", DbType.DateTime, DataTO.txtDob);
  dataAccCom.AddParameter(IDbCmd, "p_txtDesig", DbType.String, ReplaceNull(DataTO.txtDesig));
  dataAccCom.AddParameter(IDbCmd, "p_txtOFlatNo", DbType.String, ReplaceNull(DataTO.txtOFlatNo));
  dataAccCom.AddParameter(IDbCmd, "p_txtOBuild", DbType.String, ReplaceNull(DataTO.txtOBuild));
  dataAccCom.AddParameter(IDbCmd, "p_txtOPost", DbType.String, ReplaceNull(DataTO.txtOPost));
  dataAccCom.AddParameter(IDbCmd, "p_txtOArea", DbType.String, ReplaceNull(DataTO.txtOArea));
  dataAccCom.AddParameter(IDbCmd, "p_txtOCity", DbType.String, ReplaceNull(DataTO.txtOCity));
  dataAccCom.AddParameter(IDbCmd, "p_txtRBuild", DbType.String, ReplaceNull(DataTO.txtRBuild));
  dataAccCom.AddParameter(IDbCmd, "p_txtRPost", DbType.String, ReplaceNull(DataTO.txtRPost));
  dataAccCom.AddParameter(IDbCmd, "p_txtUserID", DbType.String,ReplaceNull(DataTO.txtUserID));
  dataAccCom.AddParameter(IDbCmd, "p_txtPwd", DbType.String, ReplaceNull(EncryptedPassword));
  dataAccCom.ExecuteNonQuery(IDbCmd);
  DataTO.Id = Convert.ToInt64(dataAccCom.GetParameterValue(IDbCmd, "op_Id"));
  transaction.Commit();
  return true;
 
 
 
  }
  catch (System.Exception ex)
  {
  if (transaction != null)
  {
  transaction.Rollback();
  }
  throw ex;
  }
  finally
  {
  transaction = null;
  if (dataAccCom != null)
  {
  dataAccCom.CloseConnection();
  }
  dataAccCom = null;
  IDbCmd = null;
  }
 }
 
 public string ReplaceNull(string value)
 {
  if (value == null)
  {
  return "";
  }
  else
  {
  return value;
  }
 }
 
 public DateTime ReplaceNull(DateTime value)
 {
  if (value == null)
  {
  return DateTime.Now;
  }
  else
  {
  return value;
  }
 }
 
 public double ReplaceNull(double value)
 {
  if (value == null)
  {
  return 0.0;
  }
  else
  {
  return value;
  }
 }

How do I do that? Could you support me in improving this problem?

cannot be cast from dbnull to other types
  • 2 2 Answers
  • 109 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-25T14:58:46+00:00Added an answer on June 25, 2022 at 2:58 pm

    The cause:

    After researching your problem for hours, I found that the parameter of your output is returning a value of DBNull. This is the reason why you have got the: ”object cannot be cast from dbnull to other types” error.

    Solution:

    The easiest solution for this error is adding a check for DBNull value as the following:

    var outputParam = dataAccCom.GetParameterValue(IDbCmd, "op_Id");
    if(!(outputParam is DBNull))
    DataTO.Id = Convert.ToInt64(outputParam);

    Another way that is equally effective is using the Convert.IsDBNull method:

    if (!Convert.IsDBNull(dataAccCom.GetParameterValue(IDbCmd, "op_Id"))
    {
    DataTO.Id = Convert.ToInt64(dataAccCom.GetParameterValue(IDbCmd, "op_Id"));
    }
    else
    {
    DataTO.Id = ...some default values or perform some cases of management error
    }
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Lucas Le Roux
    2022-05-25T20:56:52+00:00Added an answer on May 25, 2022 at 8:56 pm

    It is possible that the line has been retracted.

    DataTO.Id = Convert.ToInt64(dataAccCom.GetParameterValue(IDbCmd, "op_Id"));

    This is the root cause of the problem. It is possible that the op_Id value has been set to null by a stored procedure.

    Use the Convert.IsDBNull method to protect yourself. Take this example:

    if (!Convert.IsDBNull(dataAccCom.GetParameterValue(IDbCmd, "op_Id"))
    {
     DataTO.Id = Convert.ToInt64(dataAccCom.GetParameterValue(IDbCmd, "op_Id"));
    }
    else 
    {
     DataTO.Id = ...some default value or perform some error case management
    }
    • 22
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.