Link to home
Start Free TrialLog in
Avatar of QPR
QPRFlag for New Zealand

asked on

share a var and set the path

I have a sub that calls another sub.
During the second sub a variable value is set. How can I then access that var from the calling sub.
The second sub sets a var to be the value of a fileupload path. The 1st sub does an insert into a table and needs that value.

Also, how can I make the saveas path local to the app?
I tried "~/appname/images/photos" + fileName
But I get an error saying that the path is not rooted?
I can use "c:\temp\uploads\" + filename but I'd prefer to keep the path relative to the app
ASKER CERTIFIED SOLUTION
Avatar of lijunguo
lijunguo
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of QPR

ASKER

Where do I set the byval thing?
Here is the sub where the var is declared
Sub SaveFile(ByVal file As HttpPostedFile)
the var declaration
Dim savePath As String = "c:\temp\uploads\"
the other sub that calls this one
SaveFile(FileUpload1.PostedFile)

I want to be able to refer to savepath in sub1 after sub2 has done it's thing
let's say you have a sub called SaveFile sub,
change your sub SaveFile like this Sub SaveFile(ByVal file As HttpPostedFile, byref savePath as string)
then from any sub which called SaveFile could get the value savePath
Avatar of QPR

ASKER

Thanks, I changed that.
In the calling sub I had squiggles saying not enough arguments supplied so I changed it to this...
SaveFile(FileUpload1.PostedFile, "")
Is that how you handle it?

In my calling sub I have squiggles under this line still saying SavePath not declared
.Parameters.AddWithValue("@photo_path", savePath)
let's say you have a sub
sub1()
{
dim strPath as string = ""
'call savefile sub
SaveFile(FileUpload1.PostedFile, strPath )
' then output strPath see what is the value for strPath
}
Avatar of QPR

ASKER

you mean declare (dim) the same named var in both subs?
I tried that and it told me that it had been declared twice.
Avatar of QPR

ASKER

Below is the entire code behind so you can see what I have.


Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim thePlace As String
        If DropDownList3.SelectedValue = "You" Then
            Label1.Visible = True
            Exit Sub
        End If
        If DropDownList1.SelectedValue = "Street" And DropDownList2.SelectedValue = "Structure" Then
            Label2.Visible = True
            Exit Sub
        End If
        If DropDownList1.SelectedValue <> "Street" Then
            thePlace = DropDownList1.SelectedValue
        Else
            thePlace = DropDownList2.SelectedValue
        End If
        Dim Conn As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("GraffitiConnectionString").ConnectionString)
        Dim Cmd As New Data.SqlClient.SqlCommand("AddIncident", Conn)
        Cmd.CommandType = System.Data.CommandType.StoredProcedure
        If (FileUpload1.HasFile) Then
            ' Call a helper method routine to save the file.
            SaveFile(FileUpload1.PostedFile, "")
        Else
            ' Notify the user that a file was not uploaded.
            Label3.Text = "You did not specify a file to upload."
        End If
        With Cmd
            .Parameters.AddWithValue("@author", DropDownList3.SelectedValue)
            .Parameters.AddWithValue("@place", thePlace)
            .Parameters.AddWithValue("@place_detail", TextBox1.Text)
            .Parameters.AddWithValue("@incident_detail", TextBox2.Text)
            .Parameters.AddWithValue("@photo_path", savePath)
            .Parameters.AddWithValue("@Clean_up_cost", TextBox3.Text)

        End With

        Conn.Open()
        Cmd.ExecuteNonQuery()
        Conn.Close()
        Cmd.Dispose()
        Label1.Visible = False
        Label2.Visible = False
        DropDownList1.ClearSelection()
        DropDownList2.ClearSelection()
        DropDownList3.ClearSelection()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
    End Sub


    Sub SaveFile(ByVal file As HttpPostedFile, ByRef savePath As String)

        ' Specify the path to save the uploaded file to.
        Dim savePath As String = Server.MapPath("/graffiti/images/photos/")

        ' Get the name of the file to upload.
        Dim fileName As String = FileUpload1.FileName

        ' Create the path and file name to check for duplicates.
        Dim pathToCheck As String = savePath + fileName

        ' Create a temporary file name to use for checking duplicates.
        Dim tempfileName As String

        ' Check to see if a file already exists with the
        ' same name as the file to upload.        
        If (System.IO.File.Exists(pathToCheck)) Then
            Dim counter As Integer = 2
            While (System.IO.File.Exists(pathToCheck))
                ' If a file with this name already exists,
                ' prefix the filename with a number.
                tempfileName = counter.ToString() + fileName
                pathToCheck = savePath + tempfileName
                counter = counter + 1
            End While

            fileName = tempfileName

            ' Notify the user that the file name was changed.
            Label3.Text = "A file with the same name already exists." + "<br>" + _
                                     "Your file was saved as " + fileName

        Else

            ' Notify the user that the file was saved successfully.
            Label3.Text = "Your file was uploaded successfully."

        End If

        ' Append the name of the file to upload to the path.
        savePath += fileName

        ' Call the SaveAs method to save the uploaded
        ' file to the specified directory.
        FileUpload1.SaveAs(savePath)

    End Sub

End Class
in Sub SaveFile, you don't have to declare savePath at all, it's passed as a reference. any other sub which called sub SaveFile will need to decalre savePath
you should declare savePath at Sub Button1_Click
Avatar of QPR

ASKER

Thanks, I'm getting the same error when saving the file as before on this line (run time)
        FileUpload1.SaveAs(savePath)
The SaveAs method is configured to require a rooted path, and the path 'Footpath.gif' is not rooted.

I'm using your server.mappath suggestion
        Dim savePath As String = Server.MapPath("/graffiti/images/photos/")
I tried adding the ~ to the beginning but it sisn't like that
then try anoter way.
               
 Dim strThumbNailPath As String = AppDomain.CurrentDomain.BaseDirectory & "graffiti\images\photos\" & strUploadFileName
Avatar of QPR

ASKER

same error...
The SaveAs method is configured to require a rooted path, and the path 'Footpath.gif' is not rooted.

Now using:
Dim savePath As String = AppDomain.CurrentDomain.BaseDirectory & "graffiti\images\photos\"
' Append the name of the file to upload to the path.
        savePath += fileName

        ' Call the SaveAs method to save the uploaded
        ' file to the specified directory.
        FileUpload1.SaveAs(savePath)

thanks for sticking with me on this :)
can you output the value of
AppDomain.CurrentDomain.BaseDirectory, and post here for me please
By the way to make sure the following is working, try to assign a hardcoded value to savePath. then you know if it's working or not. Maybe it's not a path problem.

FileUpload1.SaveAs(savePath)
Avatar of QPR

ASKER

AppDomain.CurrentDomain.BaseDirectory = Z:\VS .Net Projects\Graffiti\
if there is a directory like Z:\VS .Net Projects\Graffiti\images\photos, then use the following.

Dim savePath As String = AppDomain.CurrentDomain.BaseDirectory & "images\photos\"
Avatar of QPR

ASKER

I see a problem...
I output Frontline.gif to a label and all i get is myimage.gif when it should be:
Dim savePath As String = AppDomain.CurrentDomain.BaseDirectory & "images\photos\"  and then myimage.gif

The full path is being written to the db in the calling sub but somehow savePath is empty within the sub BEING called and so savePath += fileName = myimage.gif
Avatar of QPR

ASKER

tried that, then I decided to output the entire path (var passed to the savefile method) that's when I saw that the var contained only the image name and not the path + image name.
In the 1st sub the var is declared as AppDomain.CurrentDomain.BaseDirectory & "images\photos\"
In the 2nd sub (that savePath is passed to as an argument) we do this....
savePath += fileName
so unless I'm missing something then savePath is empty when passed to sub2. It seems to be losing it's value that is set when declared.
Avatar of QPR

ASKER

Here is the problem......
savePath += " hello"

appending to the string is blowing away it's original value and just replacing it with whatever I append to it. Is this something to do with the fact we are using ByRef as opposed to ByVal
Avatar of QPR

ASKER

SOLVED!!!!
Instead of calling the sub with these 2 arguments
SaveFile(FileUpload1.PostedFile, savePath)
I was passing this
SaveFile(FileUpload1.PostedFile, "")

Thanks for all the help.
glad to hear you fixed the problem. but you passed a dummy argument to a sub instead of passing byref, the problem is from some logic in your sub. once you pass by ref, as long as you change the argument from any sub, the value will be changed.