Solved

Sql select next/previous

Posted on 2006-06-12
13
246 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 69

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

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 69

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 69

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 69

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now