Solved

Sql select next/previous

Posted on 2006-06-12
13
253 Views
Last Modified: 2008-02-01
Hey,

I've created two functions, one to select the next item and one to select the previous item from a sql table using the item code. The problem is that when the user inputs the first item and I call the GetPrevious function, I get an exception saying "There's no row at position 0." Same exception when the users inputs the last item and I call the GetNext function.

I cannot know what the first and last item are when I call the functions. Is there any way I can catch this error? Maybe in the SQL statement itself?
0
Comment
Question by:tsay
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 39

Expert Comment

by:appari
ID: 16883720

can you post your existing code?
0
 
LVL 5

Author Comment

by:tsay
ID: 16883748
   Public Function GetNext(ByVal pstrCode As String) As String
        GetNext = ""
        If pstrCode <> "" Then
                Try
                    Dim ldsItem As New ItemDataSet
                    sqlCmd = New SqlCommand("SELECT TOP 1 Code FROM Item WHERE code>@i", SQLConn.SQL_Connection)

                    sqlCmd.Parameters.Add("@i", pstrCode)

                    daItem = New SqlDataAdapter(sqlCmd)
                    daItem.Fill(ldsItem, "Item")

                    GetNext = ldsItem.Item(0).Code
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
        End If
    End Function

    Public Function GetPrevious(ByVal pstrCode As String) As String
        GetPrevious = ""
        If pstrCode <> "" Then
                Try
                    Dim ldsItem As New ItemDataSet
                    sqlCmd = New SqlCommand("SELECT TOP 1 Code FROM Item WHERE code<@i ORDER BY Code DESC", SQLConn.SQL_Connection)

                    sqlCmd.Parameters.Add("@i", pstrCode)

                    daItem = New SqlDataAdapter(sqlCmd)
                    daItem.Fill(ldsItem, "Item")

                    GetPrevious = ldsItem.Item(0).Code
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
        End If
    End Function
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16884060
Hi tsay,

This line:
 GetNext = ldsItem.Item(0).Code

Should read:
 GetNext = ldsItem.Tables("Item").Rows(0).Item("Code").ToString

I also think that:
    "SELECT TOP 1 Code FROM Item WHERE code>@i"
Should be:
"SELECT MIN(Code) AS Code FROM Item WHERE code>@i"

GetPrevious should be modified with much the same modifications.


Cheers!
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:tsay
ID: 16884356
You just gave me a different way to get the data, it doesn't solve the problem...
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16884461
Is your SQL query good?

Can you try this:
                   sqlCmd = New SqlCommand("SELECT MIN(Code) AS Code FROM Item WHERE code> '" & pstrcode & "'", SQLConn.SQL_Connection)
                    daItem = New SqlDataAdapter(sqlCmd)
                    daItem.Fill(ldsItem, "Item")
                    GetNext = ldsItem.Tables("Item").Rows(0).Item("Code").ToString
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 16884511

In your catch statement you have

Catch ex As Exception
                    MsgBox(ex.Message)
 End Try
This message box give the error:
"There's no row at position 0."

at the exception, write
if ex.message = "There's no row at position 0." then
   if buttonWas = Previous then
      messagebox.show("already at first record")
   else
      messagebox.show("Already at last record")
   end if
else
   messagebox.show("An known error: " & ex.message)
end if  

You may modify the code above for better performance but you can use the above statement
0
 
LVL 5

Author Comment

by:tsay
ID: 16884513
Same problem :(
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 16884519
A completely different logic would be.
Write only one sql statement and select all the lines.

then, use
bindingcontext(dataset1, "tablename").position += 1
or -= 1

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16884523
>>Same problem :(

That means that your query is not returning anything. Try running your query into Query Analyzer.
0
 
LVL 5

Author Comment

by:tsay
ID: 16884541
ofcourse it's not returning anything because there is no item with a code less then the code of the first item...
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16884554
but the code I am providing is for the GetNEXT.

It says to return the MIN code where code > something
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 16884571
and if nothing is returned you cannot query row 0. that's why you receive the error. You need to validate the number of rows first

if ldsItem.Tables("Item").Rows.count > 0 then
   GetNext = ldsItem.Tables("Item").Rows(0).Item("Code").ToString
else
   getnext = ""
end if
0
 
LVL 5

Author Comment

by:tsay
ID: 16884597
Now you're talking :) The check was the answer I was looking for. Damn shoud've figured that one out myself. I thought the solution was a different SQL statement.

tnx!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error handling in asp.net site 5 34
VB.NET 1 28
VB.Net - MemoryMappedFiles - Confirm receipt 2 24
vb.net convert long time to mm:ss 23 13
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question