This is the error I am getting when I submit the form.
Here is the relevant piece of code:
SqlParameter paraID = new SqlParameter("@ID", SqlDbType.Int, 3);
paraID.Direction = ParameterDirection.Output;
oCommand.Parameters.Add(paraID);
ID = Convert.ToInt32(paraID.Value);
//ID is int type
pl pl help me out resolve this issue... This is taking a lot of time
if (paraID.Value == DBNull.Value)
{
// You need to decide what you want to do if the param is null, here I set ID to 0
ID = 0;
}
else
{
ID = Convert.ToInt32(paraID.Value);
}
DBNull is something that represents a NULL value in your underlying database. If your database layers returned a c# "null" then you wouldn't know if params were coming back "null" as your code was failing, or if they were "null" as that is what was in your
table. By giving SQL NULL values its own type, you can test that the data being returned is actuall a NULL.
DBNull is something that represents a NULL value in your underlying database. If your database layers returned a c# "null" then you wouldn't know if params were coming back "null" as your code was failing, or if they were "null" as that is what was in your
table. By giving SQL NULL values its own type, you can test that the data being returned is actuall a NULL.
Thanks for the useful information. But unfortunately, I am still getting the same exception.
I don't know what to do. I need to complete this task, it has already prolonged.
if (paraID.Value == DBNull.Value) { // You need to decide what you want to do if the param is null, here I set ID to 0 ID = 0; } else { ID = Convert.ToInt32(paraID.Value); }
Hey I tried this code again, now I get another exception saying: 'ddlTitle' has a SelectedValue which is invalid because it doesnot exist in the list of items. Parameter name: value
Earlier the ID value was returning NULL, due to which this exception was popping up. Now since the ID value has been changed from output type to input type, I am not getting this exception.
Marked as answer by Sage Gu - MSFT on May 28, 2012 01:58 AM
coolpal9
Member
156 Points
520 Posts
Object cannot be cast from DBNull to other types
May 21, 2012 10:54 AM|LINK
Hi,
This is the error I am getting when I submit the form.
Here is the relevant piece of code:
SqlParameter paraID = new SqlParameter("@ID", SqlDbType.Int, 3); paraID.Direction = ParameterDirection.Output; oCommand.Parameters.Add(paraID); ID = Convert.ToInt32(paraID.Value); //ID is int typepl pl help me out resolve this issue... This is taking a lot of time
urenjoy
Star
12109 Points
1818 Posts
Re: Object cannot be cast from DBNull to other types
May 21, 2012 10:59 AM|LINK
Try following:
If (paraID.Value != DBNull.Value){
ID = Convert.ToInt32(paraID.Value);
}
AidyF
Star
9204 Points
1570 Posts
Re: Object cannot be cast from DBNull to other types
May 21, 2012 11:01 AM|LINK
if (paraID.Value == DBNull.Value) { // You need to decide what you want to do if the param is null, here I set ID to 0 ID = 0; } else { ID = Convert.ToInt32(paraID.Value); }coolpal9
Member
156 Points
520 Posts
Re: Object cannot be cast from DBNull to other types
May 21, 2012 11:05 AM|LINK
But what does it mean by DBNull?
This is the only relevant code, I found out by running the code in debug mode that the problem lies in
H_ID= Convert.ToInt32(paraID.Value);
coolpal9
Member
156 Points
520 Posts
Re: Object cannot be cast from DBNull to other types
May 21, 2012 11:13 AM|LINK
I tried your piece of code but still I am getting the same exception.
AidyF
Star
9204 Points
1570 Posts
Re: Object cannot be cast from DBNull to other types
May 21, 2012 11:15 AM|LINK
DBNull is something that represents a NULL value in your underlying database. If your database layers returned a c# "null" then you wouldn't know if params were coming back "null" as your code was failing, or if they were "null" as that is what was in your table. By giving SQL NULL values its own type, you can test that the data being returned is actuall a NULL.
coolpal9
Member
156 Points
520 Posts
Re: Object cannot be cast from DBNull to other types
May 21, 2012 08:04 PM|LINK
Thanks for the useful information. But unfortunately, I am still getting the same exception.
I don't know what to do. I need to complete this task, it has already prolonged.
Pl help me resolve this issue...
coolpal9
Member
156 Points
520 Posts
Re: Object cannot be cast from DBNull to other types
May 22, 2012 05:30 AM|LINK
Hey I tried this code again, now I get another exception saying: 'ddlTitle' has a SelectedValue which is invalid because it doesnot exist in the list of items. Parameter name: value
coolpal9
Member
156 Points
520 Posts
Re: Object cannot be cast from DBNull to other types
May 23, 2012 07:14 AM|LINK
Earlier the ID value was returning NULL, due to which this exception was popping up. Now since the ID value has been changed from output type to input type, I am not getting this exception.
Sage Gu - MS...
Contributor
6693 Points
578 Posts
Microsoft
Re: Object cannot be cast from DBNull to other types
May 28, 2012 03:28 AM|LINK
Hi coolpal9,
I’m glad to hear that you figured it out.
Thank you for sharing your solutions & experience here. It will be very beneficial for other community members who have similar questions.
Regards,
Sage Gu - MSFT
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework