I need to validate a query string in asp.net

2.1k Views Asked by At
 Dim objItems As clsItems

'Loads the pages with the Gridview and Infomation pretaining to the Item selected
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

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

    'Populates Gridview
    If Not Page.IsPostBack Then
        gvwCategorySelect.DataSource = clsCategory.GetList
        gvwCategorySelect.DataBind()

        'imgbtnPrev.Visible = False

        'If Not String.IsNullOrEmpty(qryItemNum) Then
        '    Dim ItemNum As Int32
        '    If Int32.TryParse(test, ItemNum) Then
        '        imgbtnPrev.Visible = (-1 < ItemNum)
        '    End If
        'End If
    End If


    objItems = New clsItems(qryItemNum)

    'Set up the from labels
    lblTitle.Text = objItems.Title
    lblPrice.Text = objItems.Price.ToString("C")
    lblDescription.Text = objItems.Description
    ImgItem.ImageUrl = "~/images/ItemImages/Item" & objItems.ItemNum & ".jpg"



End Sub

'Returns to pervious item
Protected Sub imgbtnPrev_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgbtnPrev.Click

    Response.Redirect("~/ItemInfo.aspx?qryItemNum=" & objItems.ItemNum - 1)

End Sub

'Next item
Protected Sub imgbtnNext_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgbtnNext.Click


    Response.Redirect("~/ItemInfo.aspx?qryItemNum=" & objItems.ItemNum + 1)

End Sub

In the code above there are 2 image buttons, that move between different ItemNum. Im trying to stop the user from clicking the previous button, by removing the imgbtnPrev button when the query string at 1.

The error message says There is no row at position 0

dataRowObject = DBMethods.CreateTable(sqlString, sqlArg).Rows(0)

^ here is where is its highlighted.

2

There are 2 best solutions below

4
On

Try this

   int number;
   if(Request.QueryString["qryItemNum"]!= null)
    {
     bool isNumeric = int.TryParse(Request.QueryString["qryItemNum"].ToString(), out number);
      if(isNumeric == true)
      {
        if(Convert.ToInt(Request.QueryString["qryItemNum"])<=1)
        {
          imgbtnPrev = false;
        }
      }
   }

Edit: I converted that to VB.NET

   Dim number As Integer
   If Request.QueryString("qryItemNum") IsNot Nothing Then
Dim isNumeric As Boolean = Integer.TryParse(Request.QueryString("qryItemNum").ToString(), number)
   If isNumeric = True Then
    If Convert.ToInt(Request.QueryString("qryItemNum")) <= 1 Then
        imgbtnPrev = False
    End If
   End If
   End If
14
On

I think this is how VB does it:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        imgbtnPrev = False
        Dim qryItemNum As String = Request.QueryString("qryItemNum")
        If Not String.IsNullOrEmpty(qryItemNum) Then
          Dim ItemNum As Int32
          If Int32.TryParse(qryItemNum, ItemNum) Then
            imgbtnPrev = (-1 < ItemNum)
          End If
        End If
    End If
End Sub

This assumes, of course, that imgbtnPrev is defined somewhere in your code or your ASPX page.

EDIT: From one of your comment imgbtnPrev.Visible = False to your original question, it appears that imgbtnPrev is a button on your form. If that is the case, you would write something like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        imgbtnPrev.Visible = False
        Dim qryItemNum As String = Request.QueryString("qryItemNum")
        If Not String.IsNullOrEmpty(qryItemNum) Then
          Dim ItemNum As Int32
          If Int32.TryParse(qryItemNum, ItemNum) Then
            imgbtnPrev.Visible = (-1 < ItemNum)
          End If
        End If
    End If
End Sub

EDIT2: This version employs the Try...Catch to see what the error was:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        imgbtnPrev.Visible = False
        Try
          Dim qryItemNum As String = Request.QueryString("qryItemNum")
          If Not String.IsNullOrEmpty(qryItemNum) Then
            Dim ItemNum As Int32
            If Int32.TryParse(qryItemNum, ItemNum) Then
              imgbtnPrev.Visible = (-1 < ItemNum)
            End If
          End If
        Catch err As Exception
          Response.Write(err.Message)
        End Try
    End If
End Sub

Hope it helps!