问题描述
大家好,
在我的asp.net应用程序中。我使用dropdownlist选择的值如下:
Hi all,
In my asp.net app. I use dropdownlist selected value like this:
var brand = document.getElementById('<%=ddlBrands.ClientID%>');
var brandid = brand.options[brand.selectedIndex].value;
var searchUrl = 'SearchResponse.aspx?brand=' + brandid;
在searchResponse页面中,我将品牌从字符串转换为Int32,因为我将其用作SQL SP的参数。
In searchResponse page I convert brand from string to Int32 because I use it as parameter to SQL SP.
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:
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是我从字符串转换为Int32的行。
我不知道为什么这个错误在服务器上没有在本地引发,如果我转换可能是什么原因?
请指教
提前致谢。
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.
推荐答案
Line30是我转换的行从字符串到Int32。
我不知道为什么这个错误在服务器上没有在本地引发,如果我转换可能是什么原因?
请指教
提前谢谢。
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.
输入字符串的格式不正确。
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,
Convert.ToInt32(Request.QueryString["brand"]);
你需要检查传递的值为 Request.QuerySring [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.
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,
int brandId = 0; // int == Int32
if(int.TryParse(Request.QueryString["brand"], out brandId)) {
// Converted
} else {
// Not Converted
}
价值是存储在应用程序的 brandId
变量中。
这篇关于Asp.net系统网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!