Skip Navigation Links
Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » Visual Basic .NET » Very strange export to csv behaviour!
       
Author Reply
Andrew
posted 7 posts
since Oct 05, 2009 
from

Very strange export to csv behaviour!

  Posted on: 27 Jul 2011       
Hi All, i have two button on two different tab pages of a tab control. Each one calls the same sub to generate a csv file by providing a filename, the datagridview, a boolean to display columnheaders, and the delimiter value.

As you can see there is no difference between the two calls other than the filename and datagridview reference. So why is it btnExportEnquiry works fine but btnExportVolumes give the error: "Access to the path 'C:\exporttest2.csv' is denied." ?????????????

This happens regardless of what order i click them and whether or not the file already exists. btnExportEnquiry always works, btnExportVolumes always fails.

ExportDGVToCSV(


Private Sub btnExportEnquiry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportEnquiry.Click"C:\exporttest.csv", grdEnquiry, True, ",")End Sub

ExportDGVToCSV(


Private Sub btnExportVolumes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportVolumes.Click"C:\exporttest2.csv", grdDocType, True, ",")End Sub

Private














Sub ExportDGVToCSV(ByVal strExportFileName As String, _ByVal DataGridView As DataGridView, _Optional ByVal blnWriteColumnHeaderNames As Boolean = True, _Optional ByVal strDelimiterType As String = ",")Dim sr As IO.StreamWriter = File.CreateText(strExportFileName)Dim strDelimiter As String = strDelimiterTypeDim intColumnCount As Integer = DataGridView.Columns.Count - 1Dim strRowData As String = ""

If blnWriteColumnHeaderNames Then

strRowData += Replace(DataGridView.Columns(intX).Name, strDelimiter,
IIf(intX < intColumnCount, strDelimiter,


sr.WriteLine(strRowData)


For intX As Integer = 0 To intColumnCount"") & _"")Next intXEnd If

strRowData =
For intX As Integer = 0 To DataGridView.Rows.Count - 1""

strRowData += Replace(DataGridView.Rows(intX).Cells(intRowData).Value, strDelimiter,
IIf(intRowData < intColumnCount, strDelimiter,


sr.WriteLine(strRowData)


sr.Close()
sr.Dispose()
MsgBox(


For intRowData As Integer = 0 To intColumnCount"") & _"")Next intRowDataNext intX"Done!")End Sub
Zoran Horvat
posted  411 posts
since  Nov 15, 2010 
from 

 Re: Very strange export to csv behaviour!
  Posted on: 27 Jul 2011        0  
Are you absolutely sure that C:\exporttest2.csv does not exist and that it is not open by another application, e.g. Excel when it fails?

As the first measure, try not to write to C:\ because Windows XP and later prohibit or at least don't agree with writing to root of C:. Try writing to C:\tmp\ or something like that.

Zoran
.NET Late Binding Class Library
Andrew
posted  7 posts
since  Oct 05, 2009 
from 

 Re: Very strange export to csv behaviour!
  Posted on: 27 Jul 2011        0  
Thanks for your reply Zoran.

The C:\ drive is only being used for initial testing, this will eventually be replaced by a network folder path. You did however give me an idea.....i replaced the hard-coded filename with one dynamically created incorporating the current date/time stamp (making it always unique). This has not yet failed initial testing :)

Many thanks.
Zoran Horvat
posted  411 posts
since  Nov 15, 2010 
from 

 Re: Very strange export to csv behaviour!
  Posted on: 27 Jul 2011        0  
But this indicates that file did exist when writing failed, and further down the stream it looks like some other application was holding a lock on it. Are you sure that you didn't have Excel open, reading the last version of file, or some other application like that?

Zoran
.NET Late Binding Class Library
Suthish Nair
posted  4867 posts
since  Jul 09, 2009 
from  India

 Re: Very strange export to csv behaviour!
  Posted on: 27 Jul 2011        0  
Might be a permission issue. Try to write the file inside root project path.
Sam Hobbs
posted  6447 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Very strange export to csv behaviour!
  Posted on: 27 Jul 2011        0  
I don't understand your code. Part of the problem might be that I am not sufficiently familiar with (or have forgotten) VB syntax.

So I will take a wild guess and it correct then it is not so wild. Perhaps you use (perhaps write to) the file in the same program then you don't Dispose of the file object that was used to use the file. However I must also say that you should not use Dispose unless you know there is a need for it. Usually C# and .Net makes it unnecessary to call Dispose and usually it is more likely that you could cause a problem using Dispose. In this situation it might be that we know there is a need for it.
Thinking is a feeling; pleasant for some and unpleasant for others.
       
Top Articles
View all »
Team Foundation Server Hosting
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
Team Foundation Server Hosting
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved