Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sql select next/previous

Posted on 2006-06-12
13
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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