Click here to Skip to main content
14,641,269 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have this code that will check the listview checkbox if the condition is met.


con = new SqlConnect();
      con.SqlQuery("Select * from EmployeeDeduct where EmployeeeID = @empID");
      con.cmd.Parameters.AddWithValue("@empID", lblEmpID.Text);
      sda = new SqlDataAdapter(con.cmd);
      ds = new DataSet();
      sda.Fill(ds);
      con.cmd.Connection.Close();
      if (ds.Tables[0].Rows.Count > 0)
      {
          for (int g = 0; g < ds.Tables[0].Rows.Count; g++)
          {
              con = new SqlConnect();
              con.SqlQuery("Select * from DeductionInfo");
              sda = new SqlDataAdapter(con.cmd);
              DataSet ds2 = new DataSet();
              sda.Fill(ds2);
              con.cmd.Connection.Close();
              if (ds2.Tables[0].Rows.Count > 0)
              {
                  for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                  {
                      if (ds.Tables[0].Rows[g].ItemArray[0].ToString().Contains(ds2.Tables[0].Rows[j].ItemArray[6].ToString()))
                      {
                          LvDeductions.Items[j].Checked = true;

                      }
                  }
              }


I have it inside textbox textchanged. When I click the listview value, the checkbox will be checked according to db values, but when I click another item, the previous checkboxes still have checked and it will just add a check on checkboxes inside the listview. <pre>

What I have tried:

I put ELSE statement with LvDeductions.Items[j].Checked = false;

but when I do, it uncheck the boxes that does not meet the condition but only check one box instead of two or more.
Posted
Updated 8-Jun-20 5:15am
Comments
Maciej Los 8-Jun-20 4:17am
   
Sorry, but your the description of issue is unclear.
Richard Deeming 8-Jun-20 8:23am
   
That code doesn't make any sense, and looks horrendously inefficient, and prone to crash if the number of items in LvDeductions doesn't match the number of rows in the DeductionInfo table.

Why are you loading the whole DeductionInfo table multiple times? You can just load it once, outside of the outer loop.

I suspect your nested loops can be replaced with a single SQL query. But without knowing the structure of your tables, or exactly what you're trying to do, it's impossible to tell you how.
James Dean 8-Jun-20 11:23am
   
My Bad I removed the DeductionInfo out of for loop
James Dean 8-Jun-20 19:02pm
   
I managed to fix this by refreshing the ListView that has checkbox. The code is here,

con = new SqlConnect();
con.SqlQuery("Select * from DeductionInfo");
ListViewDed();
            //LvDeductions.Items.Clear();
con = new SqlConnect();
con.SqlQuery("Select Firstname, Lastname, Department, Position from EmpRecord inner join EmpRecord2 on EmpRecord.empID = EmpRecord2.empID where EmpRecord.empID = @id");

con.cmd.Parameters.AddWithValue("@id", lblEmpID.Text);
sdr = con.cmd.ExecuteReader();
if (sdr.Read())
            {
                lblEmployeeName.Text = sdr[1].ToString() + "," + " " + sdr[0].ToString();
                lblEmpDept.Text = sdr[2].ToString() + "," + " " + sdr[3].ToString();
            }



con = new SqlConnect();
con.SqlQuery("Select * from EmployeeDeduct where EmployeeeID = @empID");
            con.cmd.Parameters.AddWithValue("@empID", lblEmpID.Text);
            sda = new SqlDataAdapter(con.cmd);
            ds = new DataSet();
            sda.Fill(ds);
            con.cmd.Connection.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int g = 0; g < ds.Tables[0].Rows.Count; g++)
                {
                    con = new SqlConnect();
                    con.SqlQuery("Select * from DeductionInfo");
                    sda = new SqlDataAdapter(con.cmd);
                    DataSet ds2 = new DataSet();
                    sda.Fill(ds2);
                    con.cmd.Connection.Close();
                    if (ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {

                            if (ds.Tables[0].Rows[g].ItemArray[0].ToString() == ds2.Tables[0].Rows[j].ItemArray[6].ToString())
                            {

                                LvDeductions.Items[j].Checked = true;

                            }
                        }
                    }
                }
            }



I want to change this code,

LvDeductions.Items[j].Checked = true;



Into this,


  con = new SqlConnect();
con.SqlQuery("Select * from DeductionInfo where DeductID like @id");
con.cmd.Parameters.AddWithValue("@id", ds2.Tables[0].Rows[j].ItemArray[6].ToString()+"");


I tried using Wildcard like this

con.cmd.Parameters.AddWithValue("@id", ds2.Tables[0].Rows[j].ItemArray[6].ToString()+"%");


and still shows me one row instead of three. I change the % to _ and I got error There is no row at position 0.
Maciej Los 16-Jun-20 8:17am
   
Use "Improve question widget" and provide appropriate information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100