CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2008
    Posts
    8

    Problem retrieving (and using) POST vars

    HI,

    I am very new here in the forum, and I usually try to find myself the problems before to cry for help, but I really cannot see what is wrong.

    I have a C# script to upload a picture, and it is working fine. The case is that this script should receive a couple POST vars from the previous script, and it is not working. I am receiving a compilation error, and I believe that the problem is the place in the script where I am trying to retrieve the vars. I have made them public but it is still not working. Could you help please?

    The script:

    Code:
    <%@ Import Namespace="System.Data.SqlClient" %>
    
    <% @Page Language="C#" %>
    
    <html>
    <head>
    	<title>File upload in ASP.NET</title>
    </head>
    <body bgcolor="#ffffff" style="font:8pt verdana;">
    
    <script language="C#" runat="server">
    
    public string carmake = Request["car_make"];
    public string carmodel = Request["car_model"];
    public string caryear = Request["car_year"];
    public string wrecktype = Request["wreck_type"];
    
    void btnUploadTheFile_Click(object Source, EventArgs evArgs) 
    {
    	string strFileNameOnServer = txtServername.Value;
    	string strBaseLocation = "c:\\ASPDNSFWithPL2\\Web\\games\\wrecking\\pics\\";
    
    	if ("" == strFileNameOnServer) 
    	{
    		txtOutput.InnerHtml = "Error - a file name must be specified.";
    		return;
    	}
    
    	if (null != uplTheFile.PostedFile) 
    	{
    		try 
    		{
    			uplTheFile.PostedFile.SaveAs(strBaseLocation+strFileNameOnServer);
    			
    			String SqlQuery = "INSERT INTO GameWreckingGuess (car_pic,car_year,car_make,car_model,wreck_type) values ('" + strFileNameOnServer + "','" + caryear + "','" + carmake + "','" + carmodel + "','" + wrecktype + "')";
    			SqlConnection myConn = new SqlConnection("data source=*******;initial catalog=*********;user id=****;password=********;persist security info=True;packet size=4096");
    			SqlCommand myCommand = new SqlCommand(SqlQuery, myConn);
    			myCommand.Connection.Open();
    			myCommand.ExecuteNonQuery();
    			
    			txtOutput.InnerHtml = "<meta http-equiv=\"refresh\" content=\"3;url=games_wrecking_wizard_3.aspx\" />";
    			
    		}
    		catch (Exception e) 
    		{
    			txtOutput.InnerHtml = "Error saving <b>" + 
    				strBaseLocation+strFileNameOnServer+"</b><br>"+ e.ToString();
    		}
    	}
    }
    
    </script>
    
    <table width="690" height="440" border="0">
      <tr><td valign="top"><img src="/images/games_cpanel.jpg" /></td></tr>
      <tr>
        <td>
        	WRECKING GAME - New Game Wizard<br /><br />
        	Step 2/3: Choose the car picture <br /><br />
    	<form enctype="multipart/form-data" runat="server">
    	<tr>
    	  <td>Select file:</td>
    	  <td><input id="uplTheFile" type=file runat="server"></td>
    	</tr>
    	<tr>
    	  <td>Name on server:</td>
    	  <td><input id="txtServername" type="text" runat="server"></td>
    	</tr>
    	<tr>
    	  <td colspan="2">
    	  <input type=button id="btnUploadTheFile" value="Upload and proceed to next step &gt;&gt;" 
    	   OnServerClick="btnUploadTheFile_Click" runat="server">
    	  </td>
    	</tr>
    	</form>
    </table>
        
    <span id=txtOutput style="font: 8pt verdana;" runat="server" />
    
    </body>
    </html>
    Thanks!

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Problem retrieving (and using) POST vars

    welcome to the forum!

    how does your previous script call this page?
    Busy

  3. #3
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem retrieving (and using) POST vars

    Quote Originally Posted by Thread1 View Post
    welcome to the forum!

    how does your previous script call this page?
    There is a simple HTML file with a form that send the vars thru POST method.

  4. #4
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Problem retrieving (and using) POST vars

    Quote Originally Posted by yankleber View Post
    I am receiving a compilation error, and I believe that the problem is the place in the script where I am trying to retrieve the vars.
    Posting the exact error is a good practice to make it easier to identify the issue instead of guessing.
    Anyway. Separate declaration from instantiation. initialize variables to default values (empty strings for example).
    Move reading from request (Request["car_make"]) to Page_Load event handler.
    Use code behind instead of page inline code for better structure.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  5. #5
    Join Date
    Dec 2008
    Posts
    8

    Unhappy Re: Problem retrieving (and using) POST vars

    Folks,

    I am trying to do a REALLY simple application. It is a 3-step wizard.

    1st step:
    User type in car make, model and year though a HTML form

    2st step:
    An aspx (C#) script receives the posted vars, allows user to upload the car pic and then save all the info to a SQL-Server table.

    3st step:
    Thank you!

    The ONLY thing I cannot do is to receive the posted vars! It should be something as simple as a Request done in PHP or ASP. Why it is that complicated in C#????

    Just for the records, I am (trying) doing it in C# because it was the only way I found to upload a file in ASP.NET...

    I am really, really, REALLY frustrated...


  6. #6
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Problem retrieving (and using) POST vars

    Did you look at or tried the solution I posted ?
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  7. #7
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem retrieving (and using) POST vars

    Hi, hspc, yes, I tried and it worked, but just PARTIALLY.

    See my code after changes (just for testing):

    Code:
    <%@ Import Namespace="System.Data.SqlClient" %>
    <% @Page Language="C#" %>
    
    <html>
    <head>
    	<title>File upload in ASP.NET</title>
    </head>
    <body bgcolor="#ffffff" style="font:8pt verdana;">
    
    <script language="C#" runat="server">
    
    public string carmake = "";
    public string carmodel = "";
    public string caryear = "";
    public string wrecktype = "";
    
    void Page_Load (object sender, System.EventArgs e)
    {   
    	carmake = Request.Form["car_make"];
    	carmodel = Request.Form["car_model"];
    	caryear = Request.Form["car_year"];
    	wrecktype = Request.Form["wreck_type"];
    	Response.Write("carmake="+carmake);
    	Response.Write("<br>");
    	Response.Write("carmodel="+carmodel);
    	Response.Write("<br>");
    	Response.Write("carmake="+caryear);
    	Response.Write("<br>");
    	Response.Write("wrecktypecaryear="+wrecktype);
    	Response.Write("<br>");
    }
    
    void btnUploadTheFile_Click(object Source, EventArgs evArgs) 
    {
    	Response.Write("carmake="+carmake);
    	Response.Write("<br>");
    	Response.Write("carmodel="+carmodel);
    	Response.Write("<br>");
    	Response.Write("carmake="+caryear);
    	Response.Write("<br>");
    	Response.Write("wrecktypecaryear="+wrecktype);
    	Response.Write("<br>");
    }
    
    </script>
    
    <table width="690" height="440" border="0">
      <tr><td valign="top"><img src="/images/games_cpanel.jpg" /></td></tr>
      <tr>
        <td>
        	WRECKING GAME - New Game Wizard<br /><br />
        	Step 2/3: Choose the car picture <br /><br />
    	<form enctype="multipart/form-data" runat="server">
    	<tr>
    	  <td>Select file:</td>
    	  <td><input id="uplTheFile" type=file runat="server"></td>
    	</tr>
    	<tr>
    	  <td>Name on server:</td>
    	  <td><input id="txtServername" type="text" runat="server"></td>
    	</tr>
    	<tr>
    	  <td colspan="2">
    	  <input type=button id="btnUploadTheFile" value="Upload and proceed to next step &gt;&gt;" 
    	   OnServerClick="btnUploadTheFile_Click" runat="server">
    	  </td>
    	</tr>
    	</form>
    </table>
        
    <span id=txtOutput style="font: 8pt verdana;" runat="server" />
    
    </body>
    </html>
    Qhen the page load for the first time, the Page_Load handler looks like to load OK the values and prints it out in the screen. However, if I click the submit button to invoke the button click handler, it seems to run the Page_Load handler again and this time the values are not there!

    I cant understand what is wrong, since I have declared public vars, so they should be visible to the whole script, not????

    Should I repost the vars in form? If yes, how can I transport the requested vars to the form?

    Thanks!

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Problem retrieving (and using) POST vars

    Quote Originally Posted by yankleber View Post
    Should I repost the vars in form? If yes, how can I transport the requested vars to the form?

    Thanks!
    Er.. why have it split over 2 pages at all? why not just have the user pick the picture on the html page too, then post to the apsx

    ?

    but if you insist on having the html -> aspx -> aspx again then yes, you will either have to:
    Store the values the user sent, into a session and retrieve them
    or
    Send them back to the user in the form that they will submit again


    Right now, youre not storing them anywhere, so of course they are blank the second time the page loads! Can you imagine what chaos it would be if amazon stored only the details of the most recent person to log in? Multi user systems would fall apart..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Problem retrieving (and using) POST vars

    you may use either of the following to persist your variables over submissions

    1) Session object
    2) Cache object
    3) Hidden fields
    4) Database
    5) etc

    but like what @cjard have said, it would be better if you restructure your web pages. also, if you are new to ASP.NET it is important to know first the "page lifecycle".

    hth
    Busy

  10. #10
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem retrieving (and using) POST vars

    Quote Originally Posted by cjard View Post
    Er.. why have it split over 2 pages at all? why not just have the user pick the picture on the html page too, then post to the apsx

    ?
    Just because I have no idea how to do that! I got the ready-to-go script to upload picture, and I need to finish this wizard and have no much time to keep over that...


  11. #11
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem retrieving (and using) POST vars

    It is being a nightmare. I have separated it into two scripts, and there is always a new error...

    Now I get the following error:
    Compiler Error Message: CS0501: 'ASP.games_wrecking_games_wrecking_wizard_2_aspx.postedfile.get' must declare a body because it is not marked abstract or extern

    First script:

    Code:
    <html>
    	<table width="690" height="440" border="0">
    	  <tr><td valign="top"><img src="/images/games_cpanel.jpg" /></td></tr>
    	  <tr>
    	    <td>
    	    	WRECKING GAME - New Game Wizard<br />
    	    	Important: the current game will be ended<br /><br />
    	    	Step 1/3: Set game info <br /><br />
    		<form name="form1" enctype="multipart/form-data" method="post" action="/games/wrecking/games_wrecking_wizard_2.aspx" runat="server">
    		  <p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Type in below the car make:<br />
    		    <input type="text" name="car_make" /><br /><br />
    		    Type in the car model:<br />
    		    <input type="text" name="car_model" /><br /><br />
    		    Type in the car year:<br />
    		    <input type="text" name="car_year" /><br /><br />
    		    Type in the wreck type:<br />
    		    <input type="text" name="wreck_type" /></font><br /><br />
    		    Select picture:<br />
    		    <input id="uplTheFile" type="file" runat="server"><br /><br />
    		    Name on server:<br />
    		    <input id="server_name" type="text" runat="server"><br /><br /></font>
    		    <input type="submit" name="Submit" value="Next step &gt;&gt;" />
    		  </p>
    		</form>
    	   </td>
    	</tr>
    	</table>
    </html>
    Second script:

    Code:
    <&#37;@ Import Namespace="System.Data.SqlClient" %>
    <% @Page Language="C#" %>
    
    <html>
    <head>
    	<title>File upload in ASP.NET</title>
    </head>
    <body bgcolor="#ffffff" style="font:8pt verdana;">
    
    <script language="C#" runat="server">
    
    public string carmake = "";
    public string carmodel = "";
    public string caryear = "";
    public string wrecktype = "";
    public HttpPostedFile postedfile {get;}
    
    void Page_Load (object sender, System.EventArgs e)
    {   
    	carmake = Request.Form["car_make"];
    	carmodel = Request.Form["car_model"];
    	caryear = Request.Form["car_year"];
    	wrecktype = Request.Form["wreck_type"];
    	postedfile = Request.Form["uplTheFile"];
    
    	Response.Write("carmake="+carmake);
    	Response.Write("<br>");
    	Response.Write("carmodel="+carmodel);
    	Response.Write("<br>");
    	Response.Write("carmake="+caryear);
    	Response.Write("<br>");
    	Response.Write("wrecktypecaryear="+wrecktype);
    	Response.Write("<br>");
    
    	string strFileNameOnServer = Request.Form["server_name"];
    	string strBaseLocation = "c:\\ASPDNSFWithPL2\\Web\\games\\wrecking\\pics\\";
    
    	if ("" == strFileNameOnServer) 
    	{
    		txtOutput.InnerHtml = "Error - a file name must be specified.";
    		return;
    	}
    
    	if (null != uplTheFile.PostedFile) 
    	{
    		try 
    		{
    			uplTheFile.PostedFile.SaveAs(strBaseLocation+strFileNameOnServer);
    		}
    		catch (Exception e) 
    		{
    			txtOutput.InnerHtml = "Error saving <b>" + 
    				strBaseLocation+strFileNameOnServer+"</b><br>"+ e.ToString();
    		}
    	}
    }
    
    </script>
    
    <span id=txtOutput style="font: 8pt verdana;" runat="server" />
    
    </body>
    </html>

  12. #12
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem retrieving (and using) POST vars

    I found the solution. For whose have interest:

    script 1:
    Code:
    <html>
    	<table width="690" height="440" border="0">
    	  <tr><td valign="top"><img src="/images/games_cpanel.jpg" /></td></tr>
    	  <tr>
    	    <td>
    	    	WRECKING GAME - New Game Wizard<br />
    	    	Important: the current game will be ended<br /><br />
    	    	Step 1/3: Set game info <br /><br />
    		<form name="form1" enctype="multipart/form-data" method="post" action="/games/wrecking/games_wrecking_wizard_2.aspx">
    		  <p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Type in below the car make:<br />
    		    <input type="text" name="car_make" /><br /><br />
    		    Type in the car model:<br />
    		    <input type="text" name="car_model" /><br /><br />
    		    Type in the car year:<br />
    		    <input type="text" name="car_year" /><br /><br />
    		    Type in the wreck type:<br />
    		    <input type="text" name="wreck_type" /></font><br /><br />
    		    Select picture:<br />
    		    <input name="attach1" type="file" /><br /><br />
    		    <input type="submit" name="Submit" value="Next step &gt;&gt;" />
    		  </p>
    		</form>
    	   </td>
    	</tr>
    	</table>
    </html>
    script 2:
    Code:
    <&#37;@ Import Namespace="System.Data.SqlClient" %>
    <% @Page Language="C#" %>
    
    <html>
    <head>
    	<title>File upload in ASP.NET</title>
    </head>
    <body bgcolor="#ffffff" style="font:8pt verdana;">
    
    <script language="C#" runat="server">
    
    public string carmake = "";
    public string carmodel = "";
    public string caryear = "";
    public string wrecktype = "";
    
    void Page_Load (object sender, System.EventArgs e)
    {   
    	carmake = Request.Form["car_make"];
    	carmodel = Request.Form["car_model"];
    	caryear = Request.Form["car_year"];
    	wrecktype = Request.Form["wreck_type"];
    
    	Response.Write("carmake="+carmake);
    	Response.Write("<br>");
    	Response.Write("carmodel="+carmodel);
    	Response.Write("<br>");
    	Response.Write("carmake="+caryear);
    	Response.Write("<br>");
    	Response.Write("wrecktype="+wrecktype);
    	Response.Write("<br>");
    
    	HttpFileCollection uploadFiles = Request.Files;
    	Response.Write(uploadFiles.Count);
    	HttpPostedFile postedfile = uploadFiles[0];
    	System.IO.Stream inStream = postedfile.InputStream;
            byte[] fileData = new byte[postedfile.ContentLength];
            inStream.Read(fileData, 0, postedfile.ContentLength);
    	
            // Save the posted file in "pics" folder
            postedfile.SaveAs(Server.MapPath("pics") + "\\" + postedfile.FileName);
            
            // Save info into the table
    	String SqlQuery = "EXEC GameWreckingInsertNewGame '" + carmake + "','" + carmodel + "','" + caryear + "','" + wrecktype + "','" + postedfile.FileName + "'";
    	SqlConnection myConn = new SqlConnection("data source=****;initial catalog=*******;user id=****;password=****;persist security info=True;packet size=4096");
    	SqlCommand myCommand = new SqlCommand(SqlQuery, myConn);
    	myCommand.Connection.Open();
    	myCommand.ExecuteNonQuery();
    	
    	//txtOutput.InnerHtml = "<meta http-equiv=\"refresh\" content=\"3;url=games_wrecking_wizard_3.aspx\" />";
    			
    	
    }
    
    </script>
    
    <span id=txtOutput style="font: 8pt verdana;" runat="server" />
    
    </body>
    </html>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured