Thursday, November 5, 2009

Unable to cast object of type system.DBNull to type system.string

Last week, I got this error when I was working on my web apps.

The code which has error:

protected string returnURL(object paramID)
{

string m_paramID;
m_paramID = (string)paramID;
string retURL = "";
if (m_paramID.Contains("IWA"))
retURL = "IWTitleDetail.aspx?Identifier=" + paramID;
else
retURL = "MsTitleDetail.aspx?Identifier=" + paramID;

return retURL;
}

Then, I changed to this and it solved the problem:

protected string returnURL(object paramID)
{
string retURL = "";
string m_paramID;
if (paramID == DBNull.Value)
{ }
else
{
m_paramID = (string)paramID;
if (m_paramID.Contains("IWA"))
retURL = "IWTitleDetail.aspx?Identifier=" + paramID;
else
retURL = "MsTitleDetail.aspx?Identifier=" + paramID;
}

return retURL;
}

Happy Coding!!

No comments:

Post a Comment

Highly Appreciated your comments