如果查询字符串为空怎么办? [英] What if the Query String is empty?

查看:79
本文介绍了如果查询字符串为空怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我有一些代码用查询字符串中的参数

设置一个下拉列表控件。但是,当查询字符串为空时,我得到

错误。


这是我的代码:

Protected Sub Page_Load( ByVal sender As Object,ByVal e As

System.EventArgs)处理Me.Load

如果不是Page.IsPostBack那么

Dim intProductID As Integer =修剪(请求(" productid)))

如果是IsNumeric(intProductID)那么

drpProducts.SelectedValue = intProductID

结束如果

结束如果

结束子


这是错误:

输入字符串不正确格式。

描述:在当前Web请求执行

期间发生了未处理的异常。请查看堆栈跟踪以获取更多

有关错误及其源自代码的信息。


异常详细信息:System.FormatException:输入字符串不是

格式正确。


源错误:

第18行:受保护的子Page_Load(ByVal sender As Object,ByVal e

As System.EventArgs)处理Me.Load

第19行:如果不是Page.IsPostBack那么

第20行:Dim intProductID As Integer =

修剪(请求(productid))


如何在不给我错误的情况下继续使用它?

谢谢

Hello

I have some code that sets a dropdownlist control with a parameter
from the querystring. However, when the querystring is empty, I get
an error.

Here is my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim intProductID As Integer = Trim(Request("productid"))
If IsNumeric(intProductID) Then
drpProducts.SelectedValue = intProductID
End If
End If
End Sub

Here is the error:
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a
correct format.

Source Error:
Line 18: Protected Sub Page_Load(ByVal sender As Object, ByVal e
As System.EventArgs) Handles Me.Load
Line 19: If Not Page.IsPostBack Then
Line 20: Dim intProductID As Integer =
Trim(Request("productid"))

How do I just have it continue without giving me the error?

Thanks

推荐答案

我猜这一行

Dim intProductID As Integer = Trim(Request(") productid))

假设productid是整数....并且当它不是(空)时吹动


从如何做你继续没有得到错误我会说停止

假设它是整数并开始假设它是'字符串

Dim intProductID As String = Trim(Request(" productid)))

乔治。


" MU" < mi ***** @ lonelyprogrammer.comwrote in message

news:4f ************************* ********* @ d10g2000 pra.googlegroups.com ...
My guess this line
Dim intProductID As Integer = Trim(Request("productid"))
assumes that productid is Integer....and blows when it''s not (empty)

As of "how do you continue without getting an error" I would say stop
assuming it''s Integer and start assuming it''s String
Dim intProductID As String = Trim(Request("productid"))
George.

"MU" <mi*****@lonelyprogrammer.comwrote in message
news:4f**********************************@d10g2000 pra.googlegroups.com...

你好


我有一些代码使用查询字符串中的参数

设置下拉列表控件。但是,当查询字符串为空时,我得到

错误。


这是我的代码:

Protected Sub Page_Load( ByVal sender As Object,ByVal e As

System.EventArgs)处理Me.Load

如果不是Page.IsPostBack那么

Dim intProductID As Integer =修剪(请求(" productid)))

如果是IsNumeric(intProductID)那么

drpProducts.SelectedValue = intProductID

结束如果

结束如果

结束子


这是错误:

输入字符串不正确格式。

描述:在当前Web请求执行

期间发生了未处理的异常。请查看堆栈跟踪以获取更多

有关错误及其源自代码的信息。


异常详细信息:System.FormatException:输入字符串不是

格式正确。


源错误:

第18行:受保护的子Page_Load(ByVal sender As Object,ByVal e

As System.EventArgs)处理Me.Load

第19行:如果不是Page.IsPostBack那么

第20行:Dim intProductID As Integer =

修剪(请求(productid))


如何在不给我错误的情况下继续使用它?

谢谢
Hello

I have some code that sets a dropdownlist control with a parameter
from the querystring. However, when the querystring is empty, I get
an error.

Here is my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim intProductID As Integer = Trim(Request("productid"))
If IsNumeric(intProductID) Then
drpProducts.SelectedValue = intProductID
End If
End If
End Sub

Here is the error:
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a
correct format.

Source Error:
Line 18: Protected Sub Page_Load(ByVal sender As Object, ByVal e
As System.EventArgs) Handles Me.Load
Line 19: If Not Page.IsPostBack Then
Line 20: Dim intProductID As Integer =
Trim(Request("productid"))

How do I just have it continue without giving me the error?

Thanks


你只需重新安排你的逻辑......

如果不是Page.IsPostBack然后

如果是IsNumeric(修剪(请求(" productid)))那么

drpProducts.SelectedValue =

修剪(Request.QueryString( productid))

结束如果

结束如果


或者如果您还想确保该值实际存在,请尝试

这个...


如果不是Page.IsPostBack那么

Dim productID As String = Request.QueryString(" productID")

如果

drpProducts.Items.IndexOf(drpProducts.Items.FindBy)价值(productID))>

-1然后

drpProducts.SelectedValue = productID

结束如果

结束如果
You just need to rearrange your logic a bit...
If Not Page.IsPostBack Then
If IsNumeric(Trim(Request("productid"))) Then
drpProducts.SelectedValue =
Trim(Request.QueryString("productid"))
End If
End If

Or if you also want to make sure the value actually exists, try
this...

If Not Page.IsPostBack Then
Dim productID As String = Request.QueryString("productID")
If
drpProducts.Items.IndexOf(drpProducts.Items.FindBy Value(productID)) >
-1 Then
drpProducts.SelectedValue = productID
End If
End If


" MU" < mi ***** @ lonelyprogrammer.comwrote in message

news:4f ************************* ********* @ d10g2000 pra.googlegroups.com ...
"MU" <mi*****@lonelyprogrammer.comwrote in message
news:4f**********************************@d10g2000 pra.googlegroups.com...

如何让它继续而不给我错误?
How do I just have it continue without giving me the error?



如果不是(Request.QueryString(" productid")则没有)那么

Dim strProductID As String = Trim(Request.QueryString (productid))

如果是IsNumeric(strProductID)那么

drpProducts.SelectedValue = strProductID

结束如果

结束如果

-

Mark Rae

ASP.NET MVP
http://www.markrae.net

If Not (Request.QueryString("productid") Is Nothing) Then
Dim strProductID As String = Trim(Request.QueryString("productid"))
If IsNumeric(strProductID) Then
drpProducts.SelectedValue = strProductID
End If
End If
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


这篇关于如果查询字符串为空怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆