Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
In my asp.net app. I use dropdownlist selected value like this:
JavaScript
var brand = document.getElementById('<%=ddlBrands.ClientID%>');
var brandid = brand.options[brand.selectedIndex].value;
var searchUrl = 'SearchResponse.aspx?brand=' + brandid;

In searchResponse page I convert brand from string to Int32 because I use it as parameter to SQL SP.
C#
Int32 brandId=0;
if (Request.QueryString["brand"] != "")
  { brandId = Convert.ToInt32(Request.QueryString["brand"]); }

Everything seems to work well locally, but when I uploaded the app to the server sometimes I get this error:
C#
error message: Exception of type 'System.Web.HttpUnhandledException' was thrown.
error Source: System.Web
error stack trace: at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.searchresponse_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\c9279761\e5a0f74a\App_Web_eywnmdzj.11.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
error inner stack trace: at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at ResponsiveAutoMobSite.SearchResponse.Page_Load(Object sender, EventArgs e) in \\hv1\webdev$\WebFolder\RESPONSIVE2014-03-26\responsive2014-03-24\ResponsiveAutoMobSite\ResponsiveAutoMobSite\SearchResponse.aspx.cs:line 30 at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
error inner Message: Input string was not in a correct format.

Line30 is the line i convert from string to Int32.
I don't know why this error is thrown on the server not locally and what could be the reason if i convert?
Please advise

Thanks in advance.
Posted

1 solution

The main exception (HttpUnhandledException) would never give you a very good message of what the problem is. The problem in this context is in the inner message. Inner message states,

Quote:
Input string was not in a correct format.


So, the solution is that you are not providing a string value that can be converted to integer value. Your variable type is not a problem. I believe it is being thrown at,

C#
Convert.ToInt32(Request.QueryString["brand"]);


You need to check what value is being passed as "Request.QuerySring["brand"]". It must be a numeric value. Instead of this, I would try using the int.TryParse(string, out int)[^] method which would check whether the number can be converted to integer or not. It won't break your application.

C#
int brandId = 0;  // int == Int32
int.TryParse(Request.QueryString["brand"], out brandId);


This code would minimize the exception chances now and would allow you to use a condition. Something like this,

C#
int brandId = 0;  // int == Int32
if(int.TryParse(Request.QueryString["brand"], out brandId)) {
   // Converted
} else { 
   // Not Converted
}


Value is stored in the "brandId" variable of your application.
 
Share this answer
 
Comments
Samira Radwan 13-May-15 14:24pm    
+5 for the well explained solution!
I will try and see if it works.
Thanks
Afzaal Ahmad Zeeshan 13-May-15 14:36pm    
You can also select this post as selected answer. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900