I have a combo box to select different types of data in the database, the listview will be filled with data according to its category based on the combo box. The problem is, when i click on that first index of the combo box, it fills the listview with the right data but when i press another different index or even the same index, the listview is filled with all the data that i have selected. It supposed to be clear each and everytime i select a new index in the combo box. The previous data was being loaded again even though i've cleared it.
Is there something wrong with my logic or do you have better codings in viewing data triggered by combo box? i really need it badly..Please Help me.. Thank You Very much.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace THESIS
{ 
public partial class RestoMaintenance : Form 
{ 
     OleDbConnection con; 
     OleDbDataAdapter da; 
     OleDbCommandBuilder cb; 
     DataTable dt = new DataTable(); 
     string newquery; 

public RestoMaintenance() 
{ 
     InitializeComponent(); 
} 

//Connection Using MS Access 
public void connection() 
{ 
     con = new OleDbConnection(); 
     con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data     Source=C:\\VillaOcampoResort.mdb"; 
     con.Open(); 
} 

//Fills The ListView with Data 
public void ShowList() 
{ 
      listView1.Items.Clear(); 
         foreach (DataRow dRow in dt.Rows) 
         { 
             if (dRow.RowState != DataRowState.Deleted) 
              { 
                 ListViewItem LV = new ListViewItem(dRow[2].ToString()); 
                 LV.SubItems.Add(dRow[3].ToString()); 
                 listView1.Items.Add(LV); 
              } 
         } 
} 

//This is the Button that i use to Compare the Index of the Combo //Box to the Query of the Database, It will Fill the ListView with //Data. 
private void btnSelect_Click(object sender, EventArgs e) 
{ 
      int checker; 
      checker = cbo1.SelectedIndex; 
      switch (checker) 
      { 
         case -1: 
         MessageBox.Show("Please Select a Catergory"); 
         break; 
         case 0: 
         connection(); 
         newquery = "Select * From tblMealName where MenuID = 1"; 
         da = new OleDbDataAdapter(newquery, con); 
         cb = new OleDbCommandBuilder(da); 
         listView1.Items.Clear(); 
         listView1.Refresh(); 
         da.Fill(dt); 
         ShowList(); 
         con.Close(); 
         break; 

         case 1: 
         connection(); 
         newquery = "Select * From tblMealName where MenuID = 2"; 
         da = new OleDbDataAdapter(newquery, con); 
         cb = new OleDbCommandBuilder(da); 
         listView1.Items.Clear(); 
         da.Fill(dt); 
         ShowList(); 
         con.Close(); 
         break; 
         case 2: 
         connection(); 
         newquery = "Select * From tblMealName where MenuID = 3"; 
         da = new OleDbDataAdapter(newquery, con); 
         cb = new OleDbCommandBuilder(da); 
         listView1.Items.Clear(); 
         da.Fill(dt); 
         ShowList(); 
         con.Close(); 
         break; 
         case 3: 
         connection(); 
         newquery = "Select * From tblMealName where MenuID = 4"; 
         da = new OleDbDataAdapter(newquery, con); 
         cb = new OleDbCommandBuilder(da); 
         listView1.Items.Clear(); 
         da.Fill(dt); 
         ShowList(); 
         con.Close(); 
         break; 
         case 4: 
         connection(); 
         newquery = "Select * From tblMealName where MenuID = 5"; 
         da = new OleDbDataAdapter(newquery, con); 
         cb = new OleDbCommandBuilder(da); 
         listView1.Items.Clear(); 
         da.Fill(dt); 
         ShowList(); 
         con.Close(); 
         break; 
         }

Recommended Answers

All 2 Replies

Try clearing the DataTable as well before populating it.

THANK YOU.. I GOT IT.

private void btnSelect_Click(object sender, EventArgs e) 
{ 
      int checker; 
      checker = cbo1.SelectedIndex; 
      switch (checker) 
      { 
         case -1: 
         MessageBox.Show("Please Select a Catergory"); 
         break; 
         case 0: 
         connection(); 
         dt.Rows.Clear();
         newquery = "Select * From tblMealName where MenuID = 1"; 
         da = new OleDbDataAdapter(newquery, con); 
         cb = new OleDbCommandBuilder(da); 
         listView1.Items.Clear(); 
         listView1.Refresh(); 
         da.Fill(dt); 
         ShowList(); 
         con.Close(); 
         break; 

i have inputted the datatable clear after the connection. it doesnt repeat anything.. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.