-3
 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 Answers2

1

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
syed mohsin
  • 2,948
  • 2
  • 23
  • 47
1

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!

  • syed answered first, but since the question was tagged with `vb.net`, I added this. My VB isn't the best, though. –  Mar 01 '13 at 20:23
  • instead of using "qryItemNum". the value is passed through objItem.ItemNum, can I replace it with that? – user2011977 Mar 01 '13 at 20:33
  • As in "Replace [Request.QueryString] with [objItem.ItemNum]?" This will not read anything from the URL in the query string, but would allow you to read and/or convert a value from `objItem`. –  Mar 01 '13 at 20:37
  • no replace qryitemnum with objItems.ItemNum bc I know I get a value in objItems.ItemNum – user2011977 Mar 01 '13 at 20:39
  • 1
    it is a good one but i think you missed that part "`ItemNum is less than or equal to 1`" – syed mohsin Mar 01 '13 at 20:39
  • 1
    Ha! You posted that just as I posted the fix for it. Thanks for noticing! :) –  Mar 01 '13 at 20:40
  • @user2011977: No, you can't use `objItems.ItemNum` like that because `Request.QueryString` is specifically for reading key/value combinations from the URL. If `objItems.ItemNum` is populated with data from the URL, then something else is loading it. –  Mar 01 '13 at 20:42
  • ok jp2 I used your example and the test was getting a error. what was i doing wrong – user2011977 Mar 01 '13 at 20:48
  • +1 for the answer. @user2011977 what was the error? – syed mohsin Mar 01 '13 at 20:50
  • I edited the code to show a Try/Catch method. You should edit your original question and Paste in the code you are using and the text of the error message. Too many downvotes on this, so I upvoted. –  Mar 01 '13 at 20:52
  • is says its not declared, do i declare it in the page load to? – user2011977 Mar 01 '13 at 20:56
  • It? `imgbtnPrev`? If so, this Button is not on the form you are running it on. Is this a project in Visual Studio or strictly a codefile aspx page? –  Mar 01 '13 at 20:58
  • 1
    no its says "test" is not declared – user2011977 Mar 01 '13 at 21:14
  • lol - damn. Missed that one. Take `test` out (what I used to test it with on my PC) and type `qryItemNum` in that spot. Sorry about that! –  Mar 01 '13 at 21:27