TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Israel
693
1.3k
219.4k
Need to "loop" these codes until all records be deleted...
May 16 2017 8:32 PM
Hi!
I have database with one column. I wrote these codes to make four important steps:
1. Loading records from database into combobox
2. Display always the first item into combobox after loading
3. Delete this first item after to be displayed
4. Refresh the combobox after delete a record
All these steps work very well with my codes. But I would like to do it repeatly until deleting the latest record without make a bug.
But this moment its gives this message on this line:
Additional information:Its not possible to modify a item colection when a DataSource property is defined.
This happen at this line:
this.comboBox1.Items.AddRange(list.ToArray<string>());
private void button5_Click(object sender, EventArgs e)
{
Control p;
p = ((Button)sender).Parent;
p.SelectNextControl(ActiveControl, true, true, true, true);
int ia = 1;
conn.Open();
comm = new OleDbCommand("SELECT DISTINCT name FROM test1", conn);
OleDbDataReader sdr = comm.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(sdr);
comboBox1.DisplayMember = "name";
comboBox1.DroppedDown = true;
List<string> list = new List<string>();
foreach (DataRow row in dt.Rows)
{
list.Add(row.Field<string>("name"));
}
this.comboBox1.Items.AddRange(list.ToArray<string>());//
The bug its here
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
conn.Close();
///////////////////////doesnt allow repeat item in combobox////////////
for (int i = 0; i < comboBox1.Items.Count; i++)
{
for (int y = 0; y < comboBox1.Items.Count; y++)
{
if (y != i && comboBox1.Items[i].ToString() == comboBox1.Items[y].ToString())
{
comboBox1.Items.RemoveAt(i);
break;
}
}
}
///////////////////////////////// Display First item
comboBox1.SelectedIndex = 0;
///////////////////////////////// Delete first item
String strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Pario\Documents\Combobox_refresh\WindowsFormsApplication1\App_Datas\test.mdb;Persist Security Info=False";
string query = "delete from test1 where name='" + comboBox1.Text + "';";
OleDbConnection conDatabase = new OleDbConnection(strConnection);
OleDbCommand cmdDatabase = new OleDbCommand(query, conn);
OleDbDataReader myReader;
try
{
conn.Open();
myReader = cmdDatabase.ExecuteReader();
//MessageBox.Show("Dados excluidos com exito. Obrigado!");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
///////////////////////////////// Refresh Database and Combobox
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Pario\Documents\Combobox_refresh\WindowsFormsApplication1\App_Datas\test.mdb;Persist Security Info=False");
con.Open();
OleDbCommand cmd = new OleDbCommand("select name from test1", con);
OleDbDataAdapter SDA = new OleDbDataAdapter();
SDA.SelectCommand = cmd;
DataTable DT = new DataTable();
SDA.Fill(DT);
comboBox1.DataSource = DT;
con.Close();
Application.DoEvents();
ia++;
int milliseconds = 2000;
Thread.Sleep(milliseconds);
button5.PerformClick(); //Trigger click event
}
Reply
Answers (
5
)
C# code failing to pick user and password from access data
NOT IN operator in sp for date field