Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a table in sql database with some columns.In that one column name is 'Featured' with char (1) and default value is 'N'.While filling form in aspx page, if i didn't checked 'Featured' checkbox, in database in saving as 'N', but if i check that 'Featured' checkbox, the database column should save as 'Y'.Can any one help me with this?

What I have tried:

 DAL:

 public void save_Click(int pid, string pname,bool fea, string sdesc)
        {
            SqlCommand cmd = new SqlCommand("SPSAVEDATA", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@P_id", pid);
            cmd.Parameters.AddWithValue("@P_name", pname);                   
            cmd.Parameters.AddWithValue("@P_isfeatured", fea);
            cmd.Parameters.AddWithValue("@P_shortdesc", sdesc);            
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

 BAL:

 public void Product(int pid, string pname,bool fea, string sdesC)
        {
            dll.save_Click(pid, pname,fea, sdesc);
        }

 CS:
 protected void Btn_save_Click(object sender, EventArgs e)
    {
        if (HiddenField1.Value == "")
        {
            
            bll.Product(0, txt_proName.Text, Convert.ToBoolean(chk_featured.Checked), txt_sdesc.Text);
            Response.Write("<script>alert('Product Added Successfully')</script>");
            
        }
        else
        {            
            bll.Product(Convert.ToInt16(HiddenField1.Value), txt_proName.Text,Convert.ToBoolean(chk_featured.Checked), txt_sdesc.Text);           
            Response.Write("<script>alert('Product Updated Successfully')</script>");            
        }        
    }

Stored Procedure:

ALTER PROC [dbo].[SPSAVEDATA]
@P_id int,
@P_name varchar(50),
@P_isfeatured char(1),
@P_shortdesc varchar(150)
AS 
BEGIN 
 if(@P_id = 0)
   BEGIN
	insert into Products1 (P_name,P_isfeatured,P_shortdesc) values (@P_name,@P_isfeatured,@P_shortdesc) 
END  

  else
BEGIN
        update Products1 set P_name=@P_name,P_isfeatured=@P_isfeatured,P_shortdesc=@P_shortdesc where P_id=@P_id
END  
END
Posted
Updated 19-Apr-19 2:52am

1 solution

Change this:
cmd.Parameters.AddWithValue("@P_isfeatured", fea);

to this:
cmd.Parameters.AddWithValue("@P_isfeatured", (fea) ? "Y" : "N" );
 
Share this answer
 
Comments
Member 14185275 19-Apr-19 9:11am    
Thank u soo much for ur response, it worked. Tanx a lot

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