Click here to Skip to main content
15,909,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
object cannot be cast from dbnull to other types. how can i solve this problem
Posted

You cannot convert a dbnull into any type.

A null means the reference is empty, so there is no question of converting it into any type.
 
Share this answer
 
Comments
#realJSOP 28-Jul-11 8:20am    
5 - proposed as answer
Abhinav S 28-Jul-11 8:21am    
Thank you JSOP.
jitendra kumar mahpatro 28-Jul-11 8:20am    
ok but how to find which object is null. but i give value to all the field.
Abhinav S 28-Jul-11 8:23am    
if (x is System.DBNull)
Tech Code Freak 29-Jul-11 8:40am    
My 5!
 
Share this answer
 
Comments
Tech Code Freak 29-Jul-11 8:41am    
My 5!
Mohammad A Rahman 29-Jul-11 8:47am    
Thanks Tech :)
I assume that your data reader is returning null values and you need to handle them
have a look at the ternary operator[^]

e.g

C#
string myString ;
....

myString = reader.ISDBNull(0) ? "A default value" : reader.GetString(0);


[Edit] fixed typo
 
Share this answer
 
v2
Comments
Tech Code Freak 29-Jul-11 8:40am    
My 5!
this is my storeproc -- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <author,,name>
-- Create date: <create>
-- Description: <description,,>
-- =============================================
CREATE PROCEDURE sp_InsertSubCategory
-- Add the parameters for the stored procedure here
(
@sub_category_name varchar(50),
@product_cat_id int,
@SubCate_Image varchar(200),
@sub_category_id int output
)

AS
declare @obj int
if not exists(select sub_category_name from tbl_SubCategoryMaster where sub_category_name=@sub_category_name)
begin
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
insert into tbl_SubCategoryMaster values(@sub_category_name,@product_cat_id,@SubCate_Image)
select @obj=max(@sub_category_id) from tbl_SubCategoryMaster
set @sub_category_id=@obj
end
else
begin
set @sub_category_id=-1
end
return

------------------
C#
public Int32 SaveData()
{
    Int32 a = 0;
    SqlCommand cmd = new SqlCommand("sp_InsertSubCategory", con);
    cmd.CommandType = CommandType.StoredProcedure;
    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }
    cmd.Parameters.AddWithValue("@sub_category_name", txtsubCategoryName.Text);
    cmd.Parameters.AddWithValue("@product_cat_id", DropProductCategory.SelectedValue);
    if (FileSubCategoryImage.FileName != "")
    {
        string fn = "~/Admin/Images/" + FileSubCategoryImage.FileName;
        string fn1 = Server.MapPath(fn);
        FileSubCategoryImage.PostedFile.SaveAs(fn1);
        cmd.Parameters.AddWithValue("@SubCate_Image", FileSubCategoryImage.FileName);
    }
    else
    {
        string path = "no-img.jpg";
        cmd.Parameters.AddWithValue("@SubCate_Image", path);
    }
    cmd.Parameters.AddWithValue("@sub_category_id", SqlDbType.Int).Direction = ParameterDirection.Output;
    cmd.ExecuteNonQuery();
    a = Convert.ToInt32(cmd.Parameters["@sub_category_id"].Value);
    return a;
}
protected void btnSave_Click(object sender, EventArgs e)
{
    Int32 a;
    a = SaveData();
    if (a == -1)
    {
        ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert", "<script>alert('Already Exists.');</script>", false);
        ListBindProductCategory();
    }
    else
    {
        ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert", "<script>alert('Saved Record.');</script>", false);
        ListBindProductCategory();
    }
}
------------------the error is 
Object cannot be cast from DBNull to other types.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900