Object cannot be cast from DBNull to other types.

protected void RadGrid1_RowClick(object sender, GridCommandEventArgs e)
            //grids clicking row shows detailsview.
            if (e.Item is Telerik.Web.UI.GridDataItem)
                Telerik.Web.UI.DataKey dataKey = RadGrid1.MasterTableView.DataKeyValues[e.Item.ItemIndex];

                if (e.CommandName == "RowClick")
if ((dataKey["attachment"] != DBNull.Value) || (Convert.ToInt32(dataKey["attachment"]) != 0)) ---->>>> this row gives error.

my attachment column can be null it is datatype is int..
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed 👽Commented:
What do you see if you put a breakpoint on the line that errors and you enter the following in the Immediate Window (Ctrl-Alt-I)?

you would have to change that || to an && otherwise it evaluates both arguments effectively referencing the dbnull value when there is one
1jawsAuthor Commented:
kaufmed, I get this
 GenericParameterAttributes: 'dataKey["attachment"].GetType().GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
    GenericParameterPosition: 'dataKey["attachment"].GetType().GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'

And, P_davis, but I have used same statement like that on ItemDataBound event of the grid when looping to all items.. didnt throw error that time...
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

kaufmed 👽Commented:
p_davis is correct. In your last question, you were checking for "xxx == DBNull || Convert.ToInt32(xxx)", and that made sense. Here, you don't want the second condition to fire if the first one is true, so you need an &&. Points to p_davis.
maybe you didn't have a dbnull value but im positive this is your problem....

if ((dataKey["attachment"] != DBNull.Value)// if this is false you have a dbnull value
 || (Convert.ToInt32(dataKey["attachment"]) != 0))//this will be evaluated b/c of the or and since you have a dbnull value you will receive this error b/c you are trying to use it in a convert..... rather just b/c you are using it.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kaufmed 👽Commented:

The reason why it made sense in your last question is that if "xxx" were NOT DBNull, then the value in the DB was not NULL and you can safely cast it to an integer.

Also, the only reason you would have used it elsewhere an it not have thrown an error is that no row was DBNull.
1jawsAuthor Commented:
Thank you both!! I appreacite a lot!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.