-
December 8th, 2008, 02:40 PM
#1
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 >>"
OnServerClick="btnUploadTheFile_Click" runat="server">
</td>
</tr>
</form>
</table>
<span id=txtOutput style="font: 8pt verdana;" runat="server" />
</body>
</html>
Thanks!
-
December 8th, 2008, 09:32 PM
#2
Re: Problem retrieving (and using) POST vars
welcome to the forum!
how does your previous script call this page?
Busy
-
December 9th, 2008, 12:13 PM
#3
Re: Problem retrieving (and using) POST vars
Originally Posted by Thread1
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.
-
December 9th, 2008, 01:03 PM
#4
Re: Problem retrieving (and using) POST vars
Originally Posted by yankleber
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.
-
December 9th, 2008, 01:37 PM
#5
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...
-
December 9th, 2008, 01:46 PM
#6
Re: Problem retrieving (and using) POST vars
Did you look at or tried the solution I posted ?
-
December 9th, 2008, 02:29 PM
#7
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 >>"
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!
-
December 9th, 2008, 08:33 PM
#8
Re: Problem retrieving (and using) POST vars
Originally Posted by yankleber
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..
-
December 9th, 2008, 09:25 PM
#9
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
-
December 10th, 2008, 10:59 AM
#10
Re: Problem retrieving (and using) POST vars
Originally Posted by cjard
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...
-
December 10th, 2008, 11:24 AM
#11
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 >>" />
</p>
</form>
</td>
</tr>
</table>
</html>
Second 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 = "";
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>
-
December 10th, 2008, 12:19 PM
#12
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 >>" />
</p>
</form>
</td>
</tr>
</table>
</html>
script 2:
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("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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|