Click here to Skip to main content
16,022,362 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi everyone
I pick a code on one site I copy it but I cant open and run this code
can help me anyone

thank you
AmA

What I have tried:

C#
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileAddress + ";Extended Properties='Excel 12.0 XML;HDR=Yes;IMEX=1'";
          using (OleDbConnection conn = new OleDbConnection(excelConnectionString))
          {
              if (conn.State != ConnectionState.Open)
                  conn.Open(); //I have error 'Invalid argument.'
              OleDbDataAdapter objDA = new System.Data.OleDb.OleDbDataAdapter
              ("select * from [ثبت$]", conn);
              DataSet excelDataSet = new DataSet();
              objDA.Fill(excelDataSet);
              dataGridView1.DataSource = excelDataSet.Tables[0];
          }


          using (OleDbConnection conn = new OleDbConnection(excelConnectionString))
          {
              try
              {
                  conn.Open();
                  OleDbCommand cmd = new OleDbCommand();
                  cmd.Connection = conn;
                  cmd.CommandText = @"Insert into [ثبت$] (month,mango,apple,orange)
          VALUES ('DEC','40','60','80');";
                  cmd.ExecuteNonQuery();
              }
              catch (Exception ex)
              {
                  //exception here
              }
              finally
              {
                  conn.Close();
                  conn.Dispose();
              }
          }
Posted
Updated 13-Jun-24 3:21am
v3
Comments
Richard Deeming 13-Jun-24 4:53am    
Nobody can possibly answer such a vague question. How would we know why you can't "open and run" this code, when you haven't given us any details of the problem?

Any answer we give can only be a wild guess. For example:

* You don't have a computer;
* You have a computer, but you don't know how to use it;
...
* Your computer doesn't have the correct version of .NET installed;
* Your computer doesn't have the correct database file that's used by the code;
...

If you want anyone to be able to help, you need to provide a lot more information that this simple "it doesn't work" statement.
Member 16284027 13-Jun-24 5:31am    
Hi Richard

I think I can't take the good address for connection

Thank you
Dave Kreskowiak 13-Jun-24 9:24am    
What address?? You're trying to connect to an Access database, which is entirely File-based. There is not address for it.

I think what you're calling an "address" is actually a file path to the Access database file you're supposed to be using. If you don't have that file, or the path to it isn't correct, your code isn't going to work.

My bad. It's not an Access database you're connecting to, but an Excel sheet. The same limitation applies. If the file path to the sheet isn't correct, the code isn't going to work.

1 solution

Use the debugger: find out exactly what FileAddress contains and compare that against the full path to the actual file. Unless they match exactly the code will fail.
When you are sure they match, check the file (and it's containing folders) access permissions for the user running the code - unless that user has at least read access (and probably write as well) the code will fail.
Then check that no process is using the file - and that includes your code: if you open a file for write in one method but do not close it this code will fail.

Sorry, but we can't do any =of that for you: we have no access to your variables or your file system!

And remember: when you copy'n'paste code from the internet the chances of it working without modifications are slim; the chances of it doing exactly what you want are even slimmer!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900