Solved

Sql select next/previous

Posted on 2006-06-12
13
248 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
Gigs: Get Your Project Delivered by an Expert

Select from 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.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 45
Receiving a string from a WebService Push 21 47
How to retrieve a mysql date column that has 0000-00-00 in vb.net 8 32
VB.NET 2008 Publish Error 2 25
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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