Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variable

Status
Not open for further replies.

Anthony904

IS-IT--Management
Jul 15, 2004
153
US
Hi,

I have some code that uploads a file (excel) to our server (asp.net)..

Code:
      Sub Upload_Click(Sender as Object, e as EventArgs)

         ' Display properties of the uploaded file

         FileName.InnerHtml = MyFile.PostedFile.FileName
         FileContent.InnerHtml = MyFile.PostedFile.ContentType 
         FileSize.InnerHtml = MyFile.PostedFile.ContentLength
         UploadDetails.visible = True
' Let us recover only the file name from its fully qualified path at client 

        Dim strFileName as string
        strFileName = MyFile.PostedFile.FileName
        [COLOR=red]Dim c as string = System.IO.Path.GetFileName(strFileName) ' only the attched file name not its path[/color]

                 ' Let us Save uploaded file to server at C:\ServerFolder\

                 Try 

	       MyFile.PostedFile.SaveAs("C:\ServerFolder\" + c)
                        Span1.InnerHtml = "Your File Uploaded Sucessfully as:  ..C:\ServerFolder\" & c 
			Span3.InnerHtml = "Thank you! Click TRANSFER to import to access. "
			BtnTransfer.visible=true
			
                catch Exp as exception
                  span1.InnerHtml = "An Error occured. Please check the attached  file"
                        UploadDetails.visible = false
                       	span2.visible=false
			span3.visible=false
			BtnTransfer.visible=false
                End Try

      End Sub

I have another page that takes that file and imports it into access (asp)

Code:
   Dim cnnExcel, rstExcel
    Dim cnnAccess, rstAccess 
    Dim strExcelFile
    Dim strAccessFile
    
    strExcelFile = Server.MapPath(".") & "\Server\" & [COLOR=red]"ExcelExportData.xls"[/color]        
    strAccessFile = Server.MapPath(".") & "\ImportData.mdb"
The question I have is.. is there a way I can pass that C variable into my asp file?

Thanks!
 
You can use a querystring or session variable to pass values between pages.
 
I've searched google and still do not see how to do this.. Do you have an example?

Thanks!
 
ex:
Page a.aspx set the variable

Session("MyVar") = "Some Value"

Page b.aspx get the variable value
somevar = Session("MyVar")


There are tons of examples out there.. use Google and search on Session Variable and do a search on QueryString.


 
So in my ASPX page I would do...

Session("FileName")=(C)

And on my ASP page. I would retrieve like this?

Dim strRetrieve

strRetrieve = session("FileName")
strExcelFile = Server.MapPath(".") & "\Server\" & strRetrieve


Thanks for the response..!
 
I tried and failed..

It returned everything but the file name.

Attached below is the updated code.

Upload.aspx
Code:
      Sub Upload_Click(Sender as Object, e as EventArgs)

         ' Display properties of the uploaded file

         FileName.InnerHtml = MyFile.PostedFile.FileName
         FileContent.InnerHtml = MyFile.PostedFile.ContentType 
         FileSize.InnerHtml = MyFile.PostedFile.ContentLength
         UploadDetails.visible = True
' Let us recover only the file name from its fully qualified path at client 

        Dim strFileName as string
        strFileName = MyFile.PostedFile.FileName
        Dim c as string = System.IO.Path.GetFileName(strFileName) ' only the attched file name not its path
        [COLOR=red]Session("FileName")=(c)[/color]

                 ' Let us Save uploaded file to server at C:\ServerFolder\

                 Try 
	       MyFile.PostedFile.SaveAs("C:\ServerFolder\" + c)
                        Span1.InnerHtml = "Your File Uploaded Sucessfully as:  ..C:\ServerFolder\" & c 
			Span3.InnerHtml = "Thank you! Click TRANSFER to import to access. "
			BtnTransfer.visible=true
			
                catch Exp as exception
                  span1.InnerHtml = "An Error occured. Please check the attached  file"
                        UploadDetails.visible = false
                       	span2.visible=false
			span3.visible=false
			BtnTransfer.visible=false
                End Try

      End Sub

Import.asp
Code:
    Dim cnnExcel, rstExcel
    Dim cnnAccess, rstAccess 
    Dim strExcelFile
    Dim strAccessFile
    [COLOR=red]Dim strRetrieve

    strRetrieve = session("FileName")
    strExcelFile = Server.MapPath(".") & "\Server\" & strRetrieve [/color]
    response.write "File Generated" & strExcelFile

    
    strAccessFile = Server.MapPath(".") & "\ImportData.mdb"

Error
Code:
File GeneratedC:\ServerFolder\
File Attached! Sending File to Access...
C:\ServerFolder\


Microsoft JET Database Engine error '80004005' 

'C:\ServerFolder\.XLS' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides. 

C:\ServerFolder\import.asp, line 55

The error is pointed to excel connection in my import.asp file.
Code:
    ' Open Excel Connection
    [COLOR=blue]Set cnnExcel = Server.CreateObject("ADODB.Connection")[/color]
    cnnExcel.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=" & strExcelFile & ";" & _
               "Extended Properties=""Excel 8.0;HDR=NO;"""
    Response.Write "Excel connection opened<BR>"

Thanks for you help.
 
If it helps.. Here's how I am calling my import.asp page.

On upload.aspx
Code:
          <Form Method="Post"  EncType="Multipart/Form-Data" RunAt="Server">
         Choose Your File  To Upload : <BR>
         <Input ID="MyFile" Type="File" RunAt="Server" Size="40"> <BR>
         <BR>
         <Input Type="Submit" Value="Upload" OnServerclick="Upload_Click" RunAt="Server">
         <Div ID="UploadDetails" Visible="False" RunAt="Server">
            File Name: <Span ID="FileName" RunAt="Server"/> <BR>
            File Content: <Span ID="FileContent" RunAt="Server"/> 

           <BR>
            File Size: <Span ID="FileSize" RunAt="Server"/>bytes
           <BR></Div>
           <Span ID="Span1" Style="Color:Red" RunAt="Server"/>
           <Span ID="Span2" Style="Color:Red" RunAt="Server"/>
	   <br><br><Span ID="Span3" Style="Color:Blue" RunAt="Server"/>

	[COLOR=red]<Button id="BtnTransfer" OnServerclick="Signout_Click" runat="server" visible="false"  >
        Transfer</Button>[/color]

Signout_Click Function
Code:
      Sub Signout_Click(Sender as Object, e as EventArgs) 

	' Transfer from Excel to Access

	FormsAuthentication.SignOut()
	Response.Write("<script>window.opener=self; window.close();window.open('[URL unfurl="true"]http://www.mysite.com/import.asp','null','height=500,width=500,status=no,toolbar=no,menubar=no,location=no','_top');<"[/URL] & chr(47) & "script>") 
      End Sub


Thanks!
 
You'll have to trace through your code and see what it is the variable(s) are holding and what you expect them to have.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top