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..
1jawsAsked:
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.

käµfm³d 👽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)?

?dataKey["attachment"].GetType()
0
p_davisCommented:
you would have to change that || to an && otherwise it evaluates both arguments effectively referencing the dbnull value when there is one
0
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...
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

käµfm³d 👽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.
0
p_davisCommented:
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.

                    {
}
0

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
käµfm³d 👽Commented:
P.S.

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.
0
1jawsAuthor Commented:
Thank you both!! I appreacite a lot!!
0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.