I think I'm know here as a person who doesn't include a lot of details which I'm sorry about so this time I'll try to be more informative with my problem.
Note: If you're asking why I'm not using exception handle it's because I wanted to work with the codes before handling the exceptions.
As of recent we were taught using MS Access to create a basic databases. So my problem is this. Using this code to display my Database to my listView:
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source =AssetManagement.accdb");//accessItems is the database file
System.Data.OleDb.OleDbDataAdapter adpt = new System.Data.OleDb.OleDbDataAdapter("select * from tbl_Assets", con);
adpt.Fill(ds);
DataTable table = ds.Tables[0];
foreach (DataRow row in table.Rows)
{
lstViewListOfRooms.Items.Add(row[1].ToString()).SubItems.Add(row[2].ToString());
for (int i = 0; i < lstViewListOfRooms.Items.Count; i++)
{
lstViewListOfRooms.Items[i].SubItems.Add(row[3].ToString());
lstViewListOfRooms.Items[i].SubItems.Add(row[4].ToString());
}
}
and this for adding new items for my database from a different form:
private void btnSaveAddAsset_Click(object sender, EventArgs e)
{
if (txtAddFloor.Text == "" || txtAddRoom.Text == "" || string.IsNullOrWhiteSpace(txtAddFloor.Text) == true || string.IsNullOrWhiteSpace(txtAddRoom.Text) == true)
{
MessageBox.Show("Please enter valid information", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
else
{
ths.lstViewListOfRooms.Items.Add(txtAddFloor.Text).SubItems.Add(txtAddRoom.Text);
for (int i = 0; i < ths.lstViewListOfRooms.Items.Count; i++)
{
String date = "dd/MM/yyyy - HH:mm:ss";
ths.lstViewListOfRooms.Items[i].SubItems.Add(txtAddDescriptionDetail.Text);
ths.lstViewListOfRooms.Items[i].SubItems.Add(DateTime.Now.ToString(date));
con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source =AssetManagement.accdb");
Ds = new DataSet();
string query = "INSERT INTO tbl_Assets(asset_floor, asset_room, asset_description, asset_createdOn)" + " VALUES (" + txtAddFloor.Text + "," + txtAddRoom.Text + ", '" + txtAddDescriptionDetail.Text + "' , '" + DateTime.Now.ToString(date) + "'" + ") ";
con.Open();
Da = new OleDbDataAdapter(query, con);
Da.Fill(Ds, "tbl_Assets");
con.Close();
this.Close();
}
}
}
The problem: Each time I add an item it doesn't just add one but more, every time I add it multiples even more and more. As you can see in this screenshot:
Each time you save, you are adding again every existing room because of this code: for (int i = 0; i < ths.lstViewListOfRooms.Items.Count; i++)
You should insert only new ones and update (if needed) the existing ones. This code will do this:
private void btnSaveAddAsset_Click(object sender, EventArgs e)
{
if (txtAddFloor.Text == "" || txtAddRoom.Text == "" || string.IsNullOrWhiteSpace(txtAddFloor.Text) == true || string.IsNullOrWhiteSpace(txtAddRoom.Text) == true)
{
MessageBox.Show("Please enter valid information", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
else
{
ths.lstViewListOfRooms.Items.Add(txtAddFloor.Text).SubItems.Add(txtAddRoom.Text);
String date = "dd/MM/yyyy - HH:mm:ss";
ths.lstViewListOfRooms.Items[lstViewListOfRooms.Items.Count - 1].SubItems.Add(txtAddDescriptionDetail.Text);
ths.lstViewListOfRooms.Items[lstViewListOfRooms.Items.Count - 1].SubItems.Add(DateTime.Now.ToString(date));
con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source =AssetManagement.accdb");
Ds = new DataSet();
string query = "INSERT INTO tbl_Assets(asset_floor, asset_room, asset_description, asset_createdOn)" + " VALUES (" + txtAddFloor.Text + "," + txtAddRoom.Text + ", '" + txtAddDescriptionDetail.Text + "' , '" + DateTime.Now.ToString(date) + "'" + ") ";
con.Open();
Da = new OleDbDataAdapter(query, con);
Da.Fill(Ds, "tbl_Assets");
con.Close();
this.Close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.