M
MikeY
Hi Everyone,
I'm trying to upload information from my CD's to Access.mdb. My problem I
suspect is in my syntax, but what is happening is that it will upload 3 or 4
records (out of say 160) then ends the foreach loop without finishing the
rest of the records. Hopefully someone can help me out in spotting the
problem. Following is a sample of my code. Thank you all in advance:
private void btnSave_Click(object sender, System.EventArgs e)
{
try
{
OleDbConnection thisConnection = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Music.mdb");
// open connection
thisConnection.Open();
// create DataAdapter object for update and other operations
OleDbDataAdapter thisAdapter = new OleDbDataAdapter
("SELECT * FROM Table1", thisConnection);
// create CommandBuilder object to build SQL commands
OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);
// create DataSet to contain related data tables, rows, and columns
DataSet thisDataSet = new DataSet();
// fill DataSet using query defined previously for DataAdapter
thisAdapter.Fill(thisDataSet, "Table1");
foreach(ListViewItem myItems in this.lvDisplay.Items)
{
DataRow thisRow = thisDataSet.Tables["Table1"].NewRow();
thisRow["CD"] = this.txtBoxCDNumber.Text;
thisRow["FolderName"] = myItems.SubItems[1].Text;
thisRow["FileName"] = myItems.SubItems[2].Text;
thisRow["SongTitle"] = myItems.SubItems[3].Text;
thisRow["ArtistName"] = myItems.SubItems[4].Text;
thisRow["AlbumName"] = myItems.SubItems[5].Text;
thisRow["Comments"] = myItems.SubItems[6].Text;
thisRow["Genre"] = myItems.SubItems[7].Text;
thisRow["Track"] = myItems.SubItems[8].Text;
thisRow["Yr"] = myItems.SubItems[9].Text;
thisRow["FileType"] = myItems.SubItems[10].Text;
thisRow["FileSize"] = myItems.SubItems[11].Text;
thisRow["Protected"] = myItems.SubItems[12].Text;
thisDataSet.Tables["Table1"].Rows.Add(thisRow);
}
thisAdapter.Update(thisDataSet, "Table1");
thisConnection.Close();
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
MessageBox.Show("Upload Complete");
}
I'm trying to upload information from my CD's to Access.mdb. My problem I
suspect is in my syntax, but what is happening is that it will upload 3 or 4
records (out of say 160) then ends the foreach loop without finishing the
rest of the records. Hopefully someone can help me out in spotting the
problem. Following is a sample of my code. Thank you all in advance:
private void btnSave_Click(object sender, System.EventArgs e)
{
try
{
OleDbConnection thisConnection = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Music.mdb");
// open connection
thisConnection.Open();
// create DataAdapter object for update and other operations
OleDbDataAdapter thisAdapter = new OleDbDataAdapter
("SELECT * FROM Table1", thisConnection);
// create CommandBuilder object to build SQL commands
OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);
// create DataSet to contain related data tables, rows, and columns
DataSet thisDataSet = new DataSet();
// fill DataSet using query defined previously for DataAdapter
thisAdapter.Fill(thisDataSet, "Table1");
foreach(ListViewItem myItems in this.lvDisplay.Items)
{
DataRow thisRow = thisDataSet.Tables["Table1"].NewRow();
thisRow["CD"] = this.txtBoxCDNumber.Text;
thisRow["FolderName"] = myItems.SubItems[1].Text;
thisRow["FileName"] = myItems.SubItems[2].Text;
thisRow["SongTitle"] = myItems.SubItems[3].Text;
thisRow["ArtistName"] = myItems.SubItems[4].Text;
thisRow["AlbumName"] = myItems.SubItems[5].Text;
thisRow["Comments"] = myItems.SubItems[6].Text;
thisRow["Genre"] = myItems.SubItems[7].Text;
thisRow["Track"] = myItems.SubItems[8].Text;
thisRow["Yr"] = myItems.SubItems[9].Text;
thisRow["FileType"] = myItems.SubItems[10].Text;
thisRow["FileSize"] = myItems.SubItems[11].Text;
thisRow["Protected"] = myItems.SubItems[12].Text;
thisDataSet.Tables["Table1"].Rows.Add(thisRow);
}
thisAdapter.Update(thisDataSet, "Table1");
thisConnection.Close();
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
MessageBox.Show("Upload Complete");
}