Hi,

Not sure if my question belongs in an ASP.NET or MSSQL forum..

I have an ASP.NET webpage built in VS2010 with an Input field with the functionality to browse to a network directory, select a text file, read content and upload the data to an MSSQL database table. Nothing fancy.

I am programming on a client, debugging, and running the website locally and the process works like a charm. When I publish the site to a webserver, browse to it and do exactly the same thing the following server error it thrown:
_________________________________________________________________________________________

Server Error in '/' Application.
Could not find a part of the path 'Z:\Data\AQVD_ReadReport20101108020000.txt'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'Z:\Data\AQVD_ReadReport20101108020000.txt'.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[DirectoryNotFoundException: Could not find a part of the path 'Z:\Data\AQVD_ReadReport20101108020000.txt'.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +224
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) +1142
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) +82
System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) +87
System.IO.StreamReader..ctor(String path) +30
MeteringInnovation._Default1.UploadFileButton_Click(Object sender, EventArgs e) in Z:\Visual Studio Projects\MeteringInnovation\MeteringInnovation\Arqiva\Default.aspx.vb:8
System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e) +118
System.Web.UI.HtmlControls.HtmlInputButton.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
_________________________________________________________________________________________

VB code:

Imports System.IO
Imports System.Data.SqlClient

Public Class _Default1
Inherits System.Web.UI.Page

Private Sub UploadFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadFile.ServerClick
Dim sr As New StreamReader(File1.Value)
Dim strLine As String = ""

Do Until sr.EndOfStream
strLine = sr.ReadLine
saveData(strLine)
Loop

UploadResultLabel.Text = "Data imported"

End Sub

Private Sub saveData(ByVal data As String)
Dim sb As System.Text.StringBuilder
Dim dc As New SqlConnection("Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=True")
Dim cm As New SqlCommand()
Dim dataValues() As String = data.Split(",")
Dim radioUnit As Long = dataValues(4)
Dim Meter As String = dataValues(5)
Dim dateTimeStamp As String = ""
Dim reading As Integer = 0
cm.Connection = dc
dc.Open()
For i As Integer = 14 To dataValues.Length - 1 Step 3
sb = New System.Text.StringBuilder
dateTimeStamp = dataValues(i)
reading = dataValues(i + 2)
sb.Append("Insert into dbo.TABLE Table (COL1, COL2, COL3, COL4) Values(")
sb.Append(COL1 & ",")
sb.Append("'" & COL2 & "',")
sb.Append("'" & COL3 & "',")
sb.Append(COL4 & ")")

cm.CommandText = sb.ToString()
cm.ExecuteNonQuery()
Next

dc.Close()

End Sub


This is in a domain Intranet environment, no firewalls etc. As I said, I am not sure if the problem is programming, network permissions or SQLdb permissions. Because of the nature of the error I'm swayed to think (as some forum posts suggest) ASPNET permissions are required on the sources directory. However, as these are networked I cannot check this option. IIS application pools are not a strong point for me, the site is in the default pool running v4.0 Integrated with NetworkService identity.

All pointers welcome.

Thanks

You need to use

System.IO.Path.GetFileName(FileName_With_Path_And_Extension)

Instead of StreamReader

You need to use

System.IO.Path.GetFileName(FileName_With_Path_And_Extension)

Instead of StreamReader

Thanks for your advice Knvn. I'm struggling to get that working still despite following MSDN help on the GetFileName method. Can you please offer an example based on my code given as a pointer, or refer to a good tutorial source?

Thanks

Try the following code(Actually, I have wrote it is c# and converted using http://www.developerfusion.com/tools/convert/csharp-to-vb/)

If (fileUploader.PostedFile IsNot Nothing) AndAlso (fileUploader.PostedFile.ContentLength > 0) Then
	Dim fn As String = System.IO.Path.GetFileName(fileUploader.PostedFile.FileName)
	Dim SaveLocation As String = Server.MapPath(fileSavePath) & fn
	Try
		fileUploader.PostedFile.SaveAs(SaveLocation)
	Catch ex As Exception
		Response.Write("Error: " + ex.Message)
	End Try
Else
	Response.Write("Please select a file to upload.")
End If

Try this too

This was a real help! The only thing is that as I want to read the text file into the database instead of import it, this wasn't the exact solution, but it did steer me towards one that ALMOST is.. Here is my revised code.

If (TextFile.HasFile) Then

            ' get file path and name            
            Dim sr As New StreamReader(TextFile.PostedFile.InputStream)
            
            ' declares new empty strLine
            Dim strLine As String = ""

            Do Until sr.EndOfStream
                strLine = sr.ReadLine
                saveData(strLine)
            Loop

            UploadStatusLabel.Text = "Your file uploaded successfully"
        Else
            UploadStatusLabel.Text = "You did not specify a file to upload"
        End If

    End Sub

The only issue now is hat only the last row of the text file dataset is output, not all rows. I'm sure this has something to do with the StreamReader and must be simple but what am I missing? Can't see the forest for the trees, maybe :)

All sorted. My last post query was down to incorrect data in the original files, nothing to do with the code. School boy error ;)
For reference, this now works.
Thanks for the advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.