UpLoading To Access???

  • Thread starter Thread starter MikeY
  • Start date Start date
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");
}
 
MikeY,

I noticed that you are coding this in an event handler for a button.
Usually, if an exception is thrown in an event handler for a control, the
exception is swallowed.

I would step through the code line by line for a few iterations, or,
wrap the whole method body in a try/catch block, and place a break on the
line in the catch statement (it has to be a dummy line like "int i = 0;", if
you are using .NET 1.1 or before) and see what happens. Then, you should
see the exception that is being thrown, and change your code accordingly.

Hope this helps.
 
Thanks for your reply Nicholas, will give it a go.

MikeY

Nicholas Paldino said:
MikeY,

I noticed that you are coding this in an event handler for a button.
Usually, if an exception is thrown in an event handler for a control, the
exception is swallowed.

I would step through the code line by line for a few iterations, or,
wrap the whole method body in a try/catch block, and place a break on the
line in the catch statement (it has to be a dummy line like "int i = 0;",
if you are using .NET 1.1 or before) and see what happens. Then, you
should see the exception that is being thrown, and change your code
accordingly.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

MikeY said:
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");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top