0

Hi there,

I am using a datarow to get data from my database and using a textbox to search for a part number, which if it finds it, will redirect to another page. This works fine, however if I enter an incorrect number it will come up with the error "index was outside the bounds of the array" even though I am using an if statement to say if it is empty.....

The code is below, any help would be greatly appreciated.

Imports System.Data.SqlClient
Imports System.Data

Partial Class About
    Inherits System.Web.UI.Page

    Public GetContent As DataTable
    Public GetMB As DataRow
    Public getBatteryContent As DataRow()
    Public getM As DataTable

    Protected Sub Page_load() Handles Me.Load
        Dim sql As String() = {"SELECT * FROM Content"}
        Dim tables As String() = {"Content"}
        CheckTables(sql, tables)
        GetContent = DataModule.GetContentSet.Tables("Content")
        Page.DataBind()

    End Sub

    Public Function GetVis() As Boolean
        Return System.IO.File.Exists(Server.MapPath(String.Format("/images/photography/{0}_back.jpg", Request.QueryString("type"))))
    End Function

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        Dim sql As String() = {"SELECT * FROM Batteries"}
        Dim tables As String() = {"Batteries"}
        CheckTables(sql, tables)
        Dim val As String = txtSearch.text

        If isnumeric(txtSearch.text) = True Then

            GetMB = GetContentSet.Tables("Batteries").Select("DinRef = " & val)(0)
            If GetMB("id") = Nothing Then
                lblStatus.Visible = True
            Else
                Response.Redirect(String.Format("/MBProduct.aspx?ref={0}", GetMB("id")))
                Page.DataBind()
            End If
        Else
            If val.Contains("-") Then
                GetMB = GetContentSet.Tables("Batteries").Select("part = '" & val & "'")(0)
                If GetMB("id") = Nothing Then
                    lblStatus.Visible = True
                Else
                    Response.Redirect(String.Format("/MBProduct.aspx?ref={0}", GetMB("id")))
                    Page.DataBind()
                End If


            Else
                GetMB = GetContentSet.Tables("Batteries").Select("WithoutHypons = '" & val & "'")(0)
                If GetMB("id") = Nothing Then
                    lblStatus.Visible = True
                Else
                    Response.Redirect(String.Format("/MBProduct.aspx?ref={0}", GetMB("id")))
                    Page.DataBind()
                End If
            End If

        End If

        'Response.Redirect(String.Format("/MBProduct.aspx?ref={0}", GetMB("id")))

        'Page.DataBind()
    End Sub

End Class
2
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by ajwposh
0

Hi,

Put a break point in the method, step into each line of code and see which line is throwing the error. For example, the following line could cause the problem.

GetMB = GetContentSet.Tables("Batteries").Select("part = '" & val & "'")(0)

The DataTable.Select(string) method will return the array of DataRows that match the filter criteria. If the condition fails, it will return empty array. But you are trying to get the first row using the Select("part = '" & val & "'")(0) part without checking whether the Select condition returns an array of datarows.

0

Thanks for your reply Ramesh.

It is that line that kicks up an error, how would I check whether the select condition returns an array or not?

Thanks very much

0

Hi,

Try the following code.

Dim resultRows() As DataRow 
resultRows = GetContentSet.Tables("Batteries").Select("WithoutHypons = '" & val & "'")
If resultRows.Count() >0 Then
 Public GetMB As DataRow = resultRows(0)
 If GetMB("id") = Nothing Then
      lblStatus.Visible = True 
  Else
     Response.Redirect(String.Format("/MBProduct.aspx?ref={0}", GetMB("id")))
     Page.DataBind()
  End If
End If

Edited by Ramesh S: n/a

0

Thanks for the code Ramesh. I have tried that code and get the following error:

"'Public' is not valid on a local variable declaration."

Thanks

0

Instead of using

Public GetMB As DataRow = resultRows(0)

i have used

Dim GetMB As DataRow = resultRows(0)

and i no longer have an error so thanks for your help.

However, it doesnt set the lblStatus to visible. I also tried to do a redirect instead but that didnt work.

Do you know why this might be?

Thanks

0

I have realised why the label wasn't showing, if there were no rows then it was avoiding the if statement altogther.

The new code is

If resultRows.Count() >0 Then
			Dim GetMB As DataRow = resultRows(0)
			Response.Redirect(String.Format("/MBProduct.aspx?ref={0}", GetMB("id")))
			Page.DataBind()
		Else
			lblStatus.Visible = True 
		End If

and thats works.

Thanks ever so much for your help

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.