Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a web form in which a check box with code as below
ASP.NET
<asp:CheckBox ID="txtIsPaid" runat="server" autopostback="true" OnCheckedChanged="txtIsPaid_CheckedChanged" />

is used.and in sql database datatype is bit.
but when i checked or unchecked this box the value is always false in database. i googled but in vain.
C#
protected void txtIsPaid_CheckedChanged(object sender, EventArgs e)
        {
            if (txtIsPaid.Checked == true)
            {
                EmployeeLeave empleave = new EmployeeLeave();
                empleave.IsPaid = Convert.ToBoolean(txtIsPaid.Text);
            }
        }
Posted
Comments
MuhammadUSman1 1-Oct-14 23:47pm    
Where is your save code logic which save always false and check Solution.
ChintanShukla 7-Oct-14 1:56am    
Dude, Stop down voting Solutions. You did not Posted the whole code, so we are not Magaicians to know your problem without code. And the solution that you were satisifed with shows you need to brush up with OOPs. Read This next time before you post a question

http://www.codeproject.com/Articles/64628/Code-Project-Quick-Answers-FAQ#rules
Sajid227 8-Oct-14 11:07am    
nice thanx for guideline i will address it

C#
protected void txtIsPaid_CheckedChanged(object sender, EventArgs e)
        {
             EmployeeLeave empleave = new EmployeeLeave();

             empleave.IsPaid=txtIsPaid.Checked;
          
        }
 
Share this answer
 
Comments
VC.J 2-Oct-14 11:03am    
Why I am downvoted , is the code is not understandable or something else ??
Hi, I hope you don't have insert/update code in txtIsPaid_CheckedChanged event

so please declare the object empleave at the top of page_load event (EmployeeLeave empleave = new EmployeeLeave();), and use this object in all events.

and change txtIsPaid_CheckedChanged envent like this

C#
EmployeeLeave empleave = new EmployeeLeave();

protected void Page_Load(object sender, EventArgs e)
{
      /// code
}

protected void txtIsPaid_CheckedChanged(object sender, EventArgs e)
{
      empleave.IsPaid = txtIsPaid.Checked;
}


please update, good luck,
 
Share this answer
 
Comments
Sajid227 1-Oct-14 13:12pm    
namespace CTP.HRMS.WebApp.Forms
{
public partial class EmployeeLeaveForm : PageBase
{
protected override string ModuleName
{
get
{
return "Employee Leave";
}
}

private Employee _employee;
private Employee Employee
{
get
{
if (_employee == null)
{
if (Request.QueryString["id"] != null)
{
_employee = _service.GetEmployee(Request.QueryString["id"].ToString());
}

if (_employee == null)
_employee = new Employee();
}
return _employee;
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!MyUser.CanAccess.LeaveRecord)
{
Redirect("/Forms/Redirected.aspx", "You do not have access to this page");
}


if (!Page.IsPostBack)
{
Page.Header.Title = "Please Enter New Leave Record:";
BindData();
// PopulateData();
}
}

protected void BindData()
{
// Fill all Dropdowns
ddlRollNo.Bind(_service.GetAllEmployee(), "IdName", "Id");
ddlLeaveType.Bind(_service.GetAllLeaveType());
ddlRank.Bind(_service.GetAllRank());
ddlAllotedBy.Bind(_service.GetAllAllotedBy());
}

//protected void PopulateData()
//{
// if ( Employee.Id != null)
// {
// EmployeeLeave empLeave = new EmployeeLeave();
// txtRollNo.Text = Employee.Id;
// txtRank.Text = Employee.Rank;
// ddlLeaveType.SelectedValue = empLeave.LeaveType_Id.ToString();
// txtAllotedBy.Text = empLeave.AllotedBy;
// txtStartDate.Text = empLeave.StartDate.ToString();
// txtEndDate.Text = empLeave.EndDate.ToString();
// txtCreationDate.Text = empLeave.CreationDate.ToString();
// }
//}


protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
EmployeeLeave empLeave = new EmployeeLeave();
empLeave.UserName = MyUser.UserName;
empLeave.EntryDate = DateTime.Now;
empLeave.Employee_Id = ddlRollNo.Text;
empLeave.Rank_Id = SafeConvert.ToByte(ddlRank.SelectedValue);
empLeave.LeaveType_Id = SafeConvert.ToByte(ddlLeaveType.SelectedValue);
empLeave.AllotedBy_Id = SafeConvert.ToByte(ddlAllotedBy.SelectedValue);
empLeave.StartDate = SafeConvert.ToDateTime(txtStartDate.Text);
empLeave.EndDate = SafeConvert.ToDateTime(txtEndDate.Text);
empLeave.CreationDate = SafeConvert.ToDateTime(txtCreationDate.Text);
_service.InsertEmployeeLeave(empLeave);
_queryStatus = empLeave.Id > 0;
}
catch (Exception ex) { HandException(ex); }

Redirect("EmployeeLeaveForm.aspx");
}

protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("EmployeeLeaveForm.aspx");
}

protected void txtIsPaid_CheckedChanged(object sender, EventArgs e)
{
if (txtIsPaid.Checked == true)
{
EmployeeLeave empleave = new EmployeeLeave();

empleave.IsPaid = txtIsPaid.Checked;


}
}
}
}
Sajid227 1-Oct-14 13:52pm    
thanx to all i got my goal
C#
protected void txtIsPaid_CheckedChanged(object sender, EventArgs e)
        {
           // if (txtIsPaid.Checked == true)
            //{
                EmployeeLeave empleave = new EmployeeLeave();
                empleave.IsPaid = txtIsPaid.Checked;//don't assign text just assign checked property.
            //}
        }
//please also post code where you are save value and get saved false always.


if any question then let me know.

-> M.U
 
Share this answer
 
Comments
Sajid227 2-Oct-14 4:13am    
bro i have solved it thanx
Dont Convert txtIsPaid to bool.
Directly set
C#
empleave.IsPaid =  true;

it will take the value bit in DB
 
Share this answer
 
Comments
Sajid227 1-Oct-14 6:06am    
i tried your solution but still same result
HI,

why are you converting string (txtIsPaid.Text) in boolean. It will not work.

you can do this, if check box is checked then just pass true to database like,

C#
protected void txtIsPaid_CheckedChanged(object sender, EventArgs e)
        {
            if (txtIsPaid.Checked == true)
            {
                EmployeeLeave empleave = new EmployeeLeave();
                empleave.IsPaid = true;
            }
            else
            {
                EmployeeLeave empleave = new EmployeeLeave();
                empleave.IsPaid = false;
            }
        }
If this will also not work then pass 1 instead of true and 0 instead of false.
 
Share this answer
 
Comments
Sajid227 1-Oct-14 6:07am    
i tried your solution but still same result
Your first Mistake is Naming, Dont use txt for Check Boxes. Search about naming user controls.
Click Here[^]

Then you already had a condition for check the status of check box
C#
if (txtIsPaid.Checked == true)
            {
}

, and you are passing the value of check box text. You should pass the current status of the check box.

Correct the code as follows. But be sure to have good names, not txtIsPaid.And also empleave.IsPaid should be a bool variable.
C#
protected void txtIsPaid_CheckedChanged(object sender, EventArgs e)
        {
            if (txtIsPaid.Checked == true)
            {
                EmployeeLeave empleave = new EmployeeLeave();
                empleave.IsPaid = true;
            }
            else
            {
                EmployeeLeave empleave = new EmployeeLeave();
                empleave.IsPaid = false;
            }
        }
 
Share this answer
 
v3
Comments
Sajid227 1-Oct-14 6:07am    
i tried your solution but still same result

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