Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql select next/previous

Posted on 2006-06-12
13
Medium Priority
?
259 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

876 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