Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello dears,
i will try to tell you what im trying to accomplish i think it will be more clear,
in sql i have column that contain some value separated by - liek(a-b-c)
and in my form i have 3 checkbox that text =a and b and c
so i wanna check if this value exist checkbox get checked here is my code
C#
SqlCommand cmd = new SqlCommand("select st from tblvl where ID="+textBox1.Text+"",cn);
            cn.Open();
            SqlDataReader sqld = cmd.ExecuteReader();
            while (sqld.Read())
            {
             
                string st = (string)sqld["st"];//this retrieve value
                string[] aray = st.Split('-');//here is separate it  
                foreach (string sta in aray)//in here it loop between them
                {
                    
                   
                    if (sta == a.Text)
                    {
                        a.BackColor = Color.Red;
                        a.Checked = true;
                    }
                    else { a.Checked = false; }
                     if(sta ==b.Text)
                    {
                        b.a.Checked = true;
                    }
                    else { b.a.Checked = false }
                }
                
            }
            cn.Close();

this codes work but the problem the loop second time or third is not equal then it unchecked it .and if there any way else i can do it plz guide me
any help will be so appreciated

What I have tried:

i have tried with switch cas but not worked
Posted
Updated 11-Jun-16 3:06am
Comments
jame01 27-Apr-16 16:50pm    
thanks man thats so true,but this was just for trying..
George Jonsson 11-Jun-16 4:54am    
How many rows do you expect to retrieve and if you only have 3 checkboxes in the form, how should more than one row in the result be handled?

1 solution

Quote:
this codes work
No, it doesn't even compile.
C#
b.a.Checked = true;
should be
C#
b.Checked = true;

Richard Deeming pointed out the vulnerability to SQL Injection to which your responded
Quote:
thanks man thats so true,but this was just for trying..
Try to get into the habit of using parameterised queries immediately. You won't have to go back and revisit your code and you are more likely to avoid the trivial errors that can creep in.
C#
SqlCommand cmd = new SqlCommand("select st from tblvl where ID=@id", cn);
cmd.Parameters.AddWithValue("@id", textBox1.Text);

Quote:
but the problem the loop second time or third is not equal then it unchecked it
Have a look at your code again, see
C#
if (sta == a.Text)
{
    a.BackColor = Color.Red;
    a.Checked = true;
}
else { a.Checked = false; }
The first time around sta is the same as a.Text and Checkbox a is checked. But 2nd time around sta is the same as b.Text so you immediately uncheck Checkbox a! Get rid of the else clauses and move the a.Checked = false; to outside the loop.
C#
a.Checked = false;
b.Checked = false;
c.Checked = false;

foreach (string sta in aray) //in here it loop between them
{
    if (sta == a.Text)
    {
        a.BackColor = Color.Red;
        a.Checked = true;
    }
    if(sta == b.Text)
        b.Checked = true;
    if (sta == c.Text)
        c.Checked = true;
}
You would have probably spotted this if you had debugged properly - see this article to get up to speed with debugging Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Now to address some of the other issues...Storing multiple values in a single column on the database is never a good idea - as you have discovered, it is a pain trying to get the values back out in a sensible way.
Why not just have 3 bit columns sta, stb, stc where if the value = 0 the corresponding Checkbox should be unchecked and if the value is 1 then it should be Checked. Eg.
C#
using (var cn = new SqlConnection(constr))
{
    var cmd = new SqlCommand("select sta, stb, stc from tblvl where ID=@id", cn);
    cmd.Parameters.AddWithValue("@id", textBox1.Text);
    cn.Open();
    var sqld = cmd.ExecuteReader();

    if (sqld.Read())
    {
        a.Checked = (int)sqld["sta"] == 1;
        b.Checked = (int)sqld["stb"] == 1;
        c.Checked = (int)sqld["stc"] == 1;
        if (a.Checked) a.BackColor = Color.Red;
    }
    cn.Close();
}
 
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