Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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 { }
        }
 
Old March 3rd, 2009, 06:52 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
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.
 
Old March 3rd, 2009, 07:14 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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/
 
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.
 
Old March 3rd, 2009, 09:46 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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/





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.