How can i solve this problem?

  • Thread starter Thread starter Miguel Dias Moura
  • Start date Start date
M

Miguel Dias Moura

Hello,

I have this code:

1) <script runat="server">

2)Sub Page_Load(sender As Object, e As System.EventArgs)
3)Dim keywords() As String =
Request.QueryString("search").Split(CChar(""))
4)End Sub

</script>

When I use, for example, page.aspx?search=asp%20book everything works
well.
When I call only page.aspx I get this error in line (3):

Object reference not set to an instance of an object.

How can I prevent this?

Thanks,
Miguel
 
Miguel Dias Moura said:
Hello,

I have this code:

1) <script runat="server">

2)Sub Page_Load(sender As Object, e As System.EventArgs)
3)Dim keywords() As String =
Request.QueryString("search").Split(CChar(""))
4)End Sub

</script>

When I use, for example, page.aspx?search=asp%20book everything works
well.
When I call only page.aspx I get this error in line (3):

Object reference not set to an instance of an object.

How can I prevent this?

If Request.QueryString.Length > 0 Then
Dim keywords() As String =
Request.QueryString("search").Split(CChar(""))
End If
 
Hi, Miguel.

Try wrapping your code in an If...Then

<script runat="server">
Sub Page_Load(sender As Object, e As System.EventArgs)
If Request.QueryString.Count > 0 Then
Dim keywords() As String =
Request.QueryString("search").Split(CChar(""))
Else
End If
End Sub
</script>




Juan T. Llibre
===========
 
Hi, Mark.

My VS's IntelliSense doesn't have a QueryString.Length
property, but it does have a QueryString.Count property.

Could you have been thinking of JavaScript/Java ?



Juan T. Llibre
===========
 
Sorry, but that could break too. If someone went to
page.aspx?anythingelse=IBrokeYourPage this would also fail. Try this (this
may contain incorrect syntax for null checking it's been a long time since
I've done any VB)

<script runat="server">
Sub Page_Load(sender As Object, e As System.EventArgs)
If Request.QueryString("search") is not null Then
Dim keywords() As String =
Request.QueryString("search").Split(CChar(""))
Else
End If
End Sub
</script>
 
To illustrate the point further, I added some code
to loop through the keys and values in the querystring,
and response.write the keys and values:

http://asp.net.do/test/request2.aspx?anythingelse=IBrokeYourPage

Test that any way you want to.

Here's some suggested ways:

http://asp.net.do/test/request2.aspx?
http://asp.net.do/test/request2.aspx?a
http://asp.net.do/test/request2.aspx?a=
http://asp.net.do/test/request2.aspx?a=ab
http://asp.net.do/test/request2.aspx?a=a&b=b&c=cd

If there's *any* value(s) in the querystring, the code runs.
If there isn't any value in the query string, the code runs.

Here's the code running at that page :

request2.aspx:
----------------
<%@ Page Language="VB" %>
<script runat="server">
Public Sub Page_Load(Sender As System.Object, E As System.EventArgs)
If Request.QueryString.Count > 0 Then
Response.Write("There's a value<br>")
Else
Response.Write("Null QueryString <br>")
End If
Dim loop1, loop2 As Integer
Dim arr1(), arr2() As String
Dim coll As NameValueCollection
coll = Request.QueryString
arr1 = coll.AllKeys
For loop1 = 0 To arr1.GetUpperBound(0)
Response.Write("Key: " & Server.HtmlEncode(arr1(loop1)) & "<br>")
arr2 = coll.GetValues(loop1)
For loop2 = 0 To arr2.GetUpperBound(0)
Response.Write("Value " & CStr(loop2) & ": " & Server.HtmlEncode(arr2(loop2)) & "<br><br>")
Next loop2
Next loop1
End Sub
</script>
<html>
<body>
<form id="Form1" runat="server">
</form>
</body>
</html>
----

Can you test and comment ?

This message was formatted as HTML,
in order to preserve line spacing.



Juan T. Llibre
===========
 
He's looking specifically for "search" in his query string. If search
doesn't exist but other values exist in the query string your logic will
trigger a null ref exception, mine won't. Your examples work just fine
because they aren't doing what the original poster wanted.
 
Oh, OK...

I got a bit carried away there.

In that case, the code you sent in
needs to me modified somewhat:


<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Request.QueryString("search") Is Nothing Then
Else
Dim keywords() As String = Request.QueryString("search").Split(CChar(""))
' etc., etc. etc., ( rest of his code... )
End If
End Sub
</script>
<html>
<body>
' something...
</body>
</html>

---

( Null is C# syntax; Nothing = null in VB/VB.NET )

Thanks...



Juan T. Llibre
===========
 
Back
Top