Hey everyone,
recently I am workign on a project where people are able to search wines and later get them from my database. I store the variables from the search form in Session which i later retrieve in the next page (I couldnt use the query string here since its too small). However i get a problem in retrieving my data.
First of all my search field put the data to a session, here is the code.
public partial class catalogus : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BTNzoeken_Click(object sender, EventArgs e)
{
Session["searchkleur"] = DDLkleur.SelectedValue;
Session["searchland"] = DDLland.SelectedValue;
Session["searchdruifsoort"] = TXTdruifsoort.Text;
Session["searchregio"] = DDLregio.SelectedValue;
Session["searchjaar"] = TXTjaar.Text;
Session["searchinhoud"] = DDLinhoud.SelectedValue;
Session["searchprijs"] = DDLprijs.SelectedValue;
Session["searchoverig"] = TXToverig.Text;
Session["searchwijnnaam"] = TXTwijnnaam.Text;
Session["searchproducent"] = TXTproducent.Text;
Response.Redirect("resultaten.aspx");
}
}
After this i will arrive at the resultaten.aspx page which holds the following code
public partial class resultaten : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["searchkleur"] != null)
{
string VARkleur = Session["searchkleur"].ToString();
string VARland = Session["searchland"].ToString();
System.Convert.ToInt32(VARland);
string VARdruifsoort = Session["searchdruifsoort"].ToString();
string VARregio = Session["searchregio"].ToString();
string VARjaar = Session["searchjaar"].ToString();
string VARinhoud = Session["searchinhoud"].ToString();
string VARprijs = Session["searchprijs"].ToString();
string VARoverig = Session["searchoverig"].ToString();
string VARwijnnaam = Session["searchwijnnaam"].ToString();
string VARproducent = Session["searchproducent"].ToString();
LBLtest.Text = "Kleur " + VARkleur + "
Land " + VARland + "
Druifsoort " + VARdruifsoort + "
Regio " + VARregio + "
Jaar " + VARjaar + "
Inhoud " + VARinhoud + "
Prijs " + VARprijs + "
Overig " + VARoverig + "
Wijnnaam " + VARwijnnaam + "
Producent " + VARproducent;
}
}
}
and here's my aspx part with the SQL command:
now i ve got 2 error messages: First one says that my landId cannot be converted to an integer so the session variable i stored as a string can't be converted to an int although i ve told C# to convert it.
2: i get an FormatException was unhandled y user code.
Does anyone know's whats wrong or how to solve it? I can share the project files if needed. Thanks in advance for your help :)
nvraman19Posted Oct 25, 2008, 12:24 PM
The command System.Convert.ToInt32(VARland); will return an integer, so if you want to use it, it should be assigned to the integer variable as in
int nAssign = System.Convert.ToInt32(VARland);
But that was not the problem. The problem is that before this statement gets executed, the conversion has failed. In order to solve the problem,
string sVarLand = Session["somevar"] as String;
int nSomeVar;
if( !String.IsNullOrEmpty( sVarLand))
{
Int32.TryParse( sVarLand, out nSomeVar);
}
If you proceed like the above, the typecasting exceptions won't be raised. If the variable nSomeVar is zero, then the value stored is not an integer value.