QPR
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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.Poste dFile, "")
Is that how you handle it?
In my calling sub I have squiggles under this line still saying SavePath not declared
.Parameters.AddWithValue(" @photo_pat h", savePath)
In the calling sub I had squiggles saying not enough arguments supplied so I changed it to this...
SaveFile(FileUpload1.Poste
Is that how you handle it?
In my calling sub I have squiggles under this line still saying SavePath not declared
.Parameters.AddWithValue("
let's say you have a sub
sub1()
{
dim strPath as string = ""
'call savefile sub
SaveFile(FileUpload1.Poste dFile, strPath )
' then output strPath see what is the value for strPath
}
sub1()
{
dim strPath as string = ""
'call savefile sub
SaveFile(FileUpload1.Poste
' then output strPath see what is the value for strPath
}
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.
I tried that and it told me that it had been declared twice.
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.SelectedValu e = "You" Then
Label1.Visible = True
Exit Sub
End If
If DropDownList1.SelectedValu e = "Street" And DropDownList2.SelectedValu e = "Structure" Then
Label2.Visible = True
Exit Sub
End If
If DropDownList1.SelectedValu e <> "Street" Then
thePlace = DropDownList1.SelectedValu e
Else
thePlace = DropDownList2.SelectedValu e
End If
Dim Conn As Data.SqlClient.SqlConnecti on = New Data.SqlClient.SqlConnecti on(System. Configurat ion.Config urationMan ager.Conne ctionStrin gs("Graffi tiConnecti onString") .Connectio nString)
Dim Cmd As New Data.SqlClient.SqlCommand( "AddIncide nt", Conn)
Cmd.CommandType = System.Data.CommandType.St oredProced ure
If (FileUpload1.HasFile) Then
' Call a helper method routine to save the file.
SaveFile(FileUpload1.Poste dFile, "")
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.SelectedValu e)
.Parameters.AddWithValue(" @place", thePlace)
.Parameters.AddWithValue(" @place_det ail", TextBox1.Text)
.Parameters.AddWithValue(" @incident_ detail", TextBox2.Text)
.Parameters.AddWithValue(" @photo_pat h", 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.ClearSelecti on()
DropDownList2.ClearSelecti on()
DropDownList3.ClearSelecti on()
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/pho tos/")
' 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(pat hToCheck)) Then
Dim counter As Integer = 2
While (System.IO.File.Exists(pat hToCheck))
' 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(savePat h)
End Sub
End Class
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.SelectedValu
Label1.Visible = True
Exit Sub
End If
If DropDownList1.SelectedValu
Label2.Visible = True
Exit Sub
End If
If DropDownList1.SelectedValu
thePlace = DropDownList1.SelectedValu
Else
thePlace = DropDownList2.SelectedValu
End If
Dim Conn As Data.SqlClient.SqlConnecti
Dim Cmd As New Data.SqlClient.SqlCommand(
Cmd.CommandType = System.Data.CommandType.St
If (FileUpload1.HasFile) Then
' Call a helper method routine to save the file.
SaveFile(FileUpload1.Poste
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("
.Parameters.AddWithValue("
.Parameters.AddWithValue("
.Parameters.AddWithValue("
.Parameters.AddWithValue("
.Parameters.AddWithValue("
End With
Conn.Open()
Cmd.ExecuteNonQuery()
Conn.Close()
Cmd.Dispose()
Label1.Visible = False
Label2.Visible = False
DropDownList1.ClearSelecti
DropDownList2.ClearSelecti
DropDownList3.ClearSelecti
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/
' 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(pat
Dim counter As Integer = 2
While (System.IO.File.Exists(pat
' 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(savePat
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
ASKER
Thanks, I'm getting the same error when saving the file as before on this line (run time)
FileUpload1.SaveAs(savePat h)
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/pho tos/")
I tried adding the ~ to the beginning but it sisn't like that
FileUpload1.SaveAs(savePat
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/
I tried adding the ~ to the beginning but it sisn't like that
then try anoter way.
Dim strThumbNailPath As String = AppDomain.CurrentDomain.Ba seDirector y & "graffiti\images\photos\" & strUploadFileName
Dim strThumbNailPath As String = AppDomain.CurrentDomain.Ba
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.Ba seDirector y & "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(savePat h)
thanks for sticking with me on this :)
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.Ba
' 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(savePat
thanks for sticking with me on this :)
can you output the value of
AppDomain.CurrentDomain.Ba seDirector y, and post here for me please
AppDomain.CurrentDomain.Ba
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(savePat h)
FileUpload1.SaveAs(savePat
ASKER
AppDomain.CurrentDomain.Ba seDirector y = Z:\VS .Net Projects\Graffiti\
if there is a directory like Z:\VS .Net Projects\Graffiti\images\p hotos, then use the following.
Dim savePath As String = AppDomain.CurrentDomain.Ba seDirector y & "images\photos\"
Dim savePath As String = AppDomain.CurrentDomain.Ba
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.Ba seDirector y & "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
I output Frontline.gif to a label and all i get is myimage.gif when it should be:
Dim savePath As String = AppDomain.CurrentDomain.Ba
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
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.Ba seDirector y & "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.
In the 1st sub the var is declared as AppDomain.CurrentDomain.Ba
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.
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
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
ASKER
SOLVED!!!!
Instead of calling the sub with these 2 arguments
SaveFile(FileUpload1.Poste dFile, savePath)
I was passing this
SaveFile(FileUpload1.Poste dFile, "")
Thanks for all the help.
Instead of calling the sub with these 2 arguments
SaveFile(FileUpload1.Poste
I was passing this
SaveFile(FileUpload1.Poste
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.
ASKER
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.Poste
I want to be able to refer to savepath in sub1 after sub2 has done it's thing