Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 3rd, 2009, 06:46 AM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default error in reading data from excel

Exception: Failure creating File

Code:
private void button1_Click(object sender, EventArgs e)
        {
            DataTable test = getDataFromXLS("c:\temp.xls");
            if (test != null)
                dataGridView1.DataSource = test;
        }
        private DataTable getDataFromXLS(string strFilePath)
        {
            try
            {
                string strConnectionString = "";  //Data Source=HARIS\SQLEXPRESS;Initial Catalog=MFSAS;User ID=akram
                strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConnectionString);
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "Select * FROM [Sheet1$]";    //first sheet of c:/temp                OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                da = null;
                return dt;
            }

            catch (Exception ex)
            {
                return null;
            }
            finally { }
        }
  #2 (permalink)  
Old March 3rd, 2009, 06:52 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Your post's title says you have an error creating file, but is it one reading the file?
I suggest two things. Firstly let the error message show instead of trapping and discarding it. Secondly change the part where you specifiy the path so that \ is not an escape character:
Code:
DataTable test = getDataFromXLS(@"c:\temp.xls");
If that fails then post the exact error message, might be that your asp.net account does not have permissions on that file for example.
__________________
Joe
http://joe.fawcett.name/
The Following User Says Thank You to joefawcett For This Useful Post:
harisbscs2003 (March 3rd, 2009)
  #3 (permalink)  
Old March 3rd, 2009, 07:11 AM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default data reading from excel file

Framework: 2.0
Lang: C#

Which control shd i use if i want end user Locate the stated file i.e "C:\temp" from its exact location.
  #4 (permalink)  
Old March 3rd, 2009, 07:14 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I don't think there's a built-in one to browse the web server's file system from a remote client. You could write your own but it's a strange request. More normal would be offer a choice of files from a drop down list or similar.
__________________
Joe
http://joe.fawcett.name/
  #5 (permalink)  
Old March 3rd, 2009, 07:33 AM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default reading data from excel

can I use <OpenFileDialog> control to give the path of the file from which end user fetch data into grid.
  #6 (permalink)  
Old March 3rd, 2009, 09:46 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Seems to be some misunderstanding, probably on my part. If this is a Win Forms app then yes you can, for some reason I thought you were talking about a web app in which case you can't.
__________________
Joe
http://joe.fawcett.name/
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error reading data from excel to sql using ASP m_soni21 Classic ASP Professional 1 June 17th, 2008 12:40 AM
data reading from excel using VB.net fly2irfan VB Databases Basics 1 January 27th, 2008 02:35 AM
problem in reading excel data premnaath ASP.NET 2.0 Professional 5 October 22nd, 2007 10:14 AM
data reading from excel using VB belzicool VB Databases Basics 3 December 22nd, 2005 06:28 AM
reading data from excel sheet rajiv_software Classic ASP Basics 2 April 30th, 2005 01:03 AM



All times are GMT -4. The time now is 07:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.