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
LVL 29
QPRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lijunguoCommented:
share a var: using parameter byref instead of byval
set the path: Server.MapPath(vritual path on the web server)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QPRAuthor Commented:
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
0
lijunguoCommented:
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
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

QPRAuthor Commented:
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)
0
lijunguoCommented:
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
}
0
QPRAuthor Commented:
you mean declare (dim) the same named var in both subs?
I tried that and it told me that it had been declared twice.
0
QPRAuthor Commented:
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
0
lijunguoCommented:
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
0
lijunguoCommented:
you should declare savePath at Sub Button1_Click
0
QPRAuthor Commented:
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
0
lijunguoCommented:
then try anoter way.
               
 Dim strThumbNailPath As String = AppDomain.CurrentDomain.BaseDirectory & "graffiti\images\photos\" & strUploadFileName
0
QPRAuthor Commented:
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 :)
0
lijunguoCommented:
can you output the value of
AppDomain.CurrentDomain.BaseDirectory, and post here for me please
0
lijunguoCommented:
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)
0
QPRAuthor Commented:
AppDomain.CurrentDomain.BaseDirectory = Z:\VS .Net Projects\Graffiti\
0
lijunguoCommented:
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\"
0
QPRAuthor Commented:
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
0
QPRAuthor Commented:
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.
0
QPRAuthor Commented:
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
0
QPRAuthor Commented:
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.
0
lijunguoCommented:
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.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.