error when trying to update dataset with oledb

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 146
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

error when trying to update dataset with oledb

 
0
  #1
Dec 5th, 2008
hi
i'm using oledb to read from an excel file into dataset.
my program is showing the dataset using datagridview,

when i'm trying to change the cell content and update the dataset i get an error :
"Update unalbe to find tablemapping['Table'] or DataTable ['Table']

my code is:

  1. public static string conString = "provider=Microsoft.Jet.OLEDB.4.0;data source=..\\..\\Data.xls;" +
  2. "Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"";
  3.  
  4. string[,] AllData = new string[27,27];
  5. static OleDbConnection con = new OleDbConnection(conString);
  6. DataSet dataset1;
  7. OleDbDataAdapter dadapter;
  8. DataGrid myDatagrid;
  9.  
  10. private void button1_Click(object sender, EventArgs e)
  11. {
  12. try
  13. {
  14. con.Open();
  15. dadapter = new OleDbDataAdapter("select * from [Vertexes$]", con);
  16. dataset1 = new DataSet();
  17. dadapter.Fill(dataset1, "Vertexes");
  18. DataTable Table = dataset1.Tables["Vertexes"];
  19.  
  20. int a =0 ,b = 0;
  21.  
  22. foreach (DataRow row in Table.Rows)
  23. {
  24. foreach (DataColumn col in Table.Columns)
  25. {
  26. AllData[a, b] = row[col].ToString();
  27. b++;
  28.  
  29. if (b == 27)
  30. {
  31. b = 0; a++;
  32.  
  33. if (a == 27)
  34. break;
  35.  
  36. }
  37.  
  38. }
  39.  
  40. if (a == 27)
  41. break;
  42.  
  43. }
  44.  
  45. con.Close();
  46.  
  47. myDatagrid = new DataGrid();
  48. myDatagrid.DataSource = dataset1.Tables["Vertexes"];
  49. myDatagrid.CaptionText = "All Data";
  50. myDatagrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  51. myDatagrid.Location = new System.Drawing.Point(256, 56);
  52. myDatagrid.Name = "dataGrid1";
  53. myDatagrid.Size = new System.Drawing.Size(488, 200);
  54. myDatagrid.TabIndex = 6;
  55. myDatagrid.Visible = false;
  56. myDatagrid.BorderStyle = BorderStyle.Fixed3D;
  57. myDatagrid.CaptionBackColor = Color.Blue;
  58. this.Controls.Add(myDatagrid);
  59.  
  60. }
  61. catch (Exception ex)
  62. {
  63. MessageBox.Show("Error in retrieving data: " + ex.Message);
  64. }
  65. }
  66.  
  67. private void button2_Click(object sender, EventArgs e)
  68. {
  69. myDatagrid.Visible = true;
  70.  
  71. }
  72.  
  73. private void button3_Click(object sender, EventArgs e)
  74. {
  75. try
  76. {
  77. DataSet changes = dataset1.GetChanges();
  78.  
  79. if (changes != null)
  80. {
  81. con.Open();
  82. dadapter.Update(changes);
  83. con.Close();
  84. dataset1.AcceptChanges();
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. MessageBox.Show("ErrorR: " + ex.Message);
  90. dataset1.RejectChanges();
  91. con.Close();
  92. }
  93. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 146
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

Re: error when trying to update dataset with oledb

 
0
  #2
Dec 5th, 2008
i found the answer. i needed to write:

  1. dadapter.TableMappings.Add("Table","Table");

now does anyone knows how can i apply the changes from the dataset to the excel file ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 146
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

Re: error when trying to update dataset with oledb

 
0
  #3
Dec 20th, 2008
ok thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC