Solved

share a var and set the path

Posted on 2007-03-20
21
236 Views
Last Modified: 2012-06-22
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
0
Comment
Question by:QPR
  • 11
  • 10
21 Comments
 
LVL 11

Accepted Solution

by:
lijunguo earned 250 total points
ID: 18760851
share a var: using parameter byref instead of byval
set the path: Server.MapPath(vritual path on the web server)
0
 
LVL 29

Author Comment

by:QPR
ID: 18761112
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
 
LVL 11

Expert Comment

by:lijunguo
ID: 18761281
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
 
LVL 29

Author Comment

by:QPR
ID: 18761307
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
 
LVL 11

Expert Comment

by:lijunguo
ID: 18761575
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
 
LVL 29

Author Comment

by:QPR
ID: 18761595
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
 
LVL 29

Author Comment

by:QPR
ID: 18761599
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
 
LVL 11

Expert Comment

by:lijunguo
ID: 18761734
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
 
LVL 11

Expert Comment

by:lijunguo
ID: 18761738
you should declare savePath at Sub Button1_Click
0
 
LVL 29

Author Comment

by:QPR
ID: 18761779
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:lijunguo
ID: 18762046
then try anoter way.
               
 Dim strThumbNailPath As String = AppDomain.CurrentDomain.BaseDirectory & "graffiti\images\photos\" & strUploadFileName
0
 
LVL 29

Author Comment

by:QPR
ID: 18767111
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
 
LVL 11

Expert Comment

by:lijunguo
ID: 18767775
can you output the value of
AppDomain.CurrentDomain.BaseDirectory, and post here for me please
0
 
LVL 11

Expert Comment

by:lijunguo
ID: 18767785
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
 
LVL 29

Author Comment

by:QPR
ID: 18768157
AppDomain.CurrentDomain.BaseDirectory = Z:\VS .Net Projects\Graffiti\
0
 
LVL 11

Expert Comment

by:lijunguo
ID: 18768331
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
 
LVL 29

Author Comment

by:QPR
ID: 18768340
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
 
LVL 29

Author Comment

by:QPR
ID: 18768683
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
 
LVL 29

Author Comment

by:QPR
ID: 18768719
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
 
LVL 29

Author Comment

by:QPR
ID: 18768737
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
 
LVL 11

Expert Comment

by:lijunguo
ID: 18775435
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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now