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
------------------
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.