Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,606 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 902 people online right now. Registration is fast and FREE... Join Now!




Jet, Excel Shets, and Permissions

 
Reply to this topicStart new topic

Jet, Excel Shets, and Permissions, It says the table doesn't exist

Redian
post 15 Jul, 2008 - 03:44 AM
Post #1


New D.I.C Head

*
Joined: 28 Dec, 2007
Posts: 22

Hey, I'm trying to access an excel table using ADO.NET oleDB commands. It works fine as long as the excel file is in the same directory as the executable. Other wise I get "Microsoft Jet Database can not find the object [table name]" How do I correct this? I know the spreadsheet I'm using exists in the file because I use a do while loops right beforehand to create it.
User is offlineProfile CardPM

Go to the top of the page

zakary
post 15 Jul, 2008 - 04:07 AM
Post #2


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 401



Thanked 6 times

Dream Kudos: 175
My Contributions


if you move the file out side the runtime or where the exe is you need to tell ole where to find the file. this is done in your connections string

csharp

string pathAndFileName = @"C:\temp\myExcel.xls";

System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source='" + pathAndFileName + "'; Extended Properties=Excel 8.0;");

User is offlineProfile CardPM

Go to the top of the page

Redian
post 15 Jul, 2008 - 04:14 AM
Post #3


New D.I.C Head

*
Joined: 28 Dec, 2007
Posts: 22

QUOTE(zakary @ 15 Jul, 2008 - 05:07 AM) *

if you move the file out side the runtime or where the exe is you need to tell ole where to find the file. this is done in your connections string

csharp

string pathAndFileName = @"C:\temp\myExcel.xls";

System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source='" + pathAndFileName + "'; Extended Properties=Excel 8.0;");




That's what I did, and when the directory in the connection string doesn't match the directory or one of the subdirectories of the exe, it gives me the Object could not be found error referring to the Spreadsheet name.
User is offlineProfile CardPM

Go to the top of the page

zakary
post 15 Jul, 2008 - 06:19 AM
Post #4


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 401



Thanked 6 times

Dream Kudos: 175
My Contributions


Can you post your code
User is offlineProfile CardPM

Go to the top of the page

Redian
post 15 Jul, 2008 - 11:20 AM
Post #5


New D.I.C Head

*
Joined: 28 Dec, 2007
Posts: 22

I'm not sure what to give, Here's the sections that I would think are important:
CODE

public void SaveToExcel()
        {
            bool InterestedCustomersExists = false;
            if (Clients != null)
            {
                try
                {
                    conn.Open();
                    if (File.Exists(ConferenceName + ComputerNumber + ".xls"))
                    {
                        do
                        {
                            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                            String[] excelSheets = new String[dt.Rows.Count];
                            int i = 0;
                            foreach (DataRow row in dt.Rows)
                            {
                                string strSheetTableName = row["TABLE_NAME"].ToString();
                                excelSheets[i] = strSheetTableName.Substring(0, strSheetTableName.Length - 1);
                                i++;
                            }
                            i = 0;
                            foreach (string tablename in excelSheets)
                            {
                                if (tablename == "IntCust") InterestedCustomersExists = true;
                            }
                            if (InterestedCustomersExists == false)
                            {
                                string Command = @"CREATE TABLE [InterestedCustomers] ([First Name] text, [Last Name] text, [Address Line 1] text, [Address Line 2] text, [City] text, [State] text, [Zip Code] text, [Home Phone Number] text, [Business Phone Number] text, [Position] text, [Email Address] text, [Subjects Taught] text, [Projector Use (X times per week)] text, [School Name] text, [School Address Line 1] text, [School Address Line 2] text, [School City] text, [School State] text, [School Zip] text, [School Phone Number] text, [Software Type Requested] text, [License for Subject] text, [Wand Trial?] text, [Comments] text)";
                                cmdCreateTable = new OleDbCommand(Command, conn);
                                cmdCreateTable.ExecuteNonQuery();
                            }
                        } while (InterestedCustomersExists == false);
                    }
                    for (int i = 0; i <= Clients.Count - 1; i++)
                    {
                        this.cmdCustomers.Parameters.Clear(); //Clears the parameters from the last time around
                        foreach (OleDbParameter Par in Clients[i].ParametersforWriting)
                        {
                            this.cmdCustomers.Parameters.Add(Par);
                        }
                        {
                            this.cmdCustomers.ExecuteNonQuery(); //Execute the command
                        }
                    }
                    Clients = null;
                }
                finally
                {
                    conn.Close();//Close the connection
                }
            }
        }

//and the original command object is created here:
        public Form2(string ConferenceName, int ComputerNumber, object SessLogin, string FilePath)
        {
            InitializeComponent();
            //prevent form redraw from flashing
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
            //collect the information passed from the first form
            this.ConferenceName = ConferenceName;
            this.ComputerNumber = ComputerNumber;
            this.SessLogin = SessLogin;
            //Set up the OleDb connection and commands for inserting rows, using the passed information.
            ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + FilePath + "\\" + ConferenceName + ComputerNumber + ".xls; Extended Properties=\"Excel 8.0;HDR=YES;Readonly=FALSE\"";
            conn = new OleDbConnection(ConnectionString);
            string Command = @"INSERT INTO [InterestedCustomers]
               ([First Name] , [Last Name] , [Address Line 1] , [Address Line 2] , [City] , [State] , [Zip Code] , [Home Phone Number] , [Business Phone Number] , [Position] , [Email Address] , [Subjects Taught] , [Projector Use (X times per week)] , [School Name] , [School Address Line 1] , [School Address Line 2] , [School City] , [School State] , [School Zip] , [School Phone Number] , [Software Type Requested] , [License for Subject] , [Wand Trial?] , [Comments] )
                VALUES (@FirstName, @LastName, @Address1, @Address2, @City, @State, @Zip, @HomePhone, @BusinessPhone, @Position, @Email, @Subjects, @Projector, @School, @SchoolAddress1, @SchoolAddress2, @SchoolCity, @SchoolState, @SchoolZip, @SchoolPhone, @Software, @SubjectLicense, @WandTrial, @Comments)";
            cmdCustomers = new OleDbCommand(Command, conn);
        }


I don't think there's anything wrong with my connection or commands, I really feel like its some preference I need to change or something...
User is offlineProfile CardPM

Go to the top of the page

Redian
post 16 Jul, 2008 - 02:30 PM
Post #6


New D.I.C Head

*
Joined: 28 Dec, 2007
Posts: 22

Okay, now I'm totally lost, I started tinkering with some of the app.manifest settings trying to configure permissions, but I really have no idea what I'm doing here:
CODE

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <applicationRequestMinimum>
        <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
        <defaultAssemblyRequest permissionSetReference="Custom" />
      </applicationRequestMinimum>
      <RequestedPriveleges>
        <RequestedExecutionLevel level="requireAdministrator" />
      </RequestedPriveleges>
    </security>
  </trustInfo>
</asmv1:assembly>

Could someone let me know if I'm moving in the right direction?
User is offlineProfile CardPM

Go to the top of the page

Redian
post 17 Jul, 2008 - 05:19 PM
Post #7


New D.I.C Head

*
Joined: 28 Dec, 2007
Posts: 22

Took me long enough to get past my ego, but I figured it out, I just took the check off that made sure the file exists right before the loop to make sure the table exists, because the file is created by the connection. What I fool I am! Wow. Thanks for all the interest.
User is offlineProfile CardPM

Go to the top of the page

zakary
post 21 Jul, 2008 - 06:20 AM
Post #8


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 401



Thanked 6 times

Dream Kudos: 175
My Contributions


Thats ok because I looked at your code and could not see anything either. I was hoping someone else could figure it out for you but looks like you got it. Good Job.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:29AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month