Click here to Skip to main content
15,891,734 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Frnds,

I have designed with check boxes.. i need check box true when the columns Values are "yes" in data base.

I have written this code but always shows checked.

C#
while (dtr.Read())
           {
                   if (dtr["EmpMaster"].ToString() == "N")
                   {
                       ChkEmpMaster.Checked = false;
                   }
                   else
                   {
                       ChkEmpMaster.Checked = true;
                   }

}



With Regards,
Vivek
Posted
Comments
Prasad_Kulkarni 24-May-14 2:34am    
Not clear.
I have written this code but always shows checked.
Vivek.anand34 24-May-14 2:40am    
for that code... output is checked in checkbox. didn't change please c that code..
Peter Leow 24-May-14 2:46am    
The problem lies somewhere else. Show more code if you can.
Vivek.anand34 24-May-14 2:56am    
GridViewRow row = (sender as ImageButton).NamingContainer as GridViewRow;
con.Open();
string strSQL = "SELECT * from Vw_UserCreation where UserId='" + row.Cells[0].Text + "'";
SqlCommand cmd = new SqlCommand(strSQL, con);
SqlDataReader dtr = cmd.ExecuteReader();
while (dtr.Read())
{
txtUserId.Text = dtr.GetString(dtr.GetOrdinal("UserId"));
txtUserName.Text = dtr.GetString(dtr.GetOrdinal("UserName"));
txtPassword.Text=dtr.GetString(dtr.GetOrdinal("Password"));
txtDeptId.Text = dtr.GetString(dtr.GetOrdinal("DepartmentId"));
txtDeptName.Text = dtr["DepartmentName"].ToString();
rbUserType.Text = dtr["UserType"].ToString();


if (dtr["EmpMaster"].ToString() == "N")
{
ChkEmpMaster.Checked = false;
}
else
{
ChkEmpMaster.Checked = true;
}
}
Peter Leow 24-May-14 3:14am    
Add you code to your original question using the "Improve question" widget. More eyes is better than 2.

As you are using DataReader, so it would loop for all the rows returned by the query.
You are trying to check one single CheckBox inside the loop, so the loop would end up in checking the CheckBox according to the last returned value of the DataReader.

So, debug and see for each loop, what the value returned.
 
Share this answer
 
Comments
Vivek.anand34 24-May-14 2:48am    
how use loop send some lines...
Vivek.anand34 24-May-14 2:52am    
GridViewRow row = (sender as ImageButton).NamingContainer as GridViewRow;
con.Open();
string strSQL = "SELECT * from Vw_UserCreation where UserId='" + row.Cells[0].Text + "'";
SqlCommand cmd = new SqlCommand(strSQL, con);
SqlDataReader dtr = cmd.ExecuteReader();
while (dtr.Read())
{
txtUserId.Text = dtr.GetString(dtr.GetOrdinal("UserId"));
txtUserName.Text = dtr.GetString(dtr.GetOrdinal("UserName"));
txtPassword.Text=dtr.GetString(dtr.GetOrdinal("Password"));
txtDeptId.Text = dtr.GetString(dtr.GetOrdinal("DepartmentId"));
txtDeptName.Text = dtr["DepartmentName"].ToString();
rbUserType.Text = dtr["UserType"].ToString();


if (dtr["EmpMaster"].ToString() == "N")
{
ChkEmpMaster.Checked = false;
}
else
{
ChkEmpMaster.Checked = true;
}
}
try below
C#
ChkEmpMaster.Checked =dtr["EmpMaster"].ToString() == "yes";


also note that if you have multiple records then you will get the status of last record since you have while loop and set again and again the checked status of same control.
 
Share this answer
 
v2
Comments
Vivek.anand34 24-May-14 2:52am    
no use of this line.. didn't action anyting
DamithSL 24-May-14 3:12am    
what is the value you get for dtr["EmpMaster"].ToString()? debug and let me know
Vivek.anand34 24-May-14 3:25am    
in this dtr["EmpMaster"] columns two values: Y and N.. if Y means check box is checked and N means Checkbox not checked..

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