INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
Go Back  Xtreme Visual Basic Talk > > > INVALID ATTEMPT TO READ-Error


Reply
 
Thread Tools Display Modes
  #1  
Old 05-12-2014, 02:55 PM
sirmilt sirmilt is offline
Contributor
 
Join Date: Jan 2003
Location: Florida
Posts: 451
Default INVALID ATTEMPT TO READ-Error


This error has me baffled. When I try to get the results for this stored procedure I get the index out of range error.

The SQL query is "SELECT MAX(LoanID) AS LastLoanID FROM tblLoans;"
When I try to select the "LastLoanID" using this code:

'This procedure will select the LoanID from the latest Loan record...
Dim SQLConnection As New System.Data.SqlClient.SqlConnection("server=milton- hp\sqlexpress;database=MyCollections;Integrated Security=True;")

Dim cmd As New System.Data.SqlClient.SqlCommand
Dim reader As SqlDataReader

cmd.CommandText = "SELECT MAX(LoanID) AS LastLoanID FROM tblLoans;"
cmd.CommandType = System.Data.CommandType.Text
cmd.Connection = SQLConnection

cmd.Connection.Open()

reader = cmd.ExecuteReader
'Get the loan ID from last loan...
intLoanID = CInt(reader.Item("lastLoanID"))
txtLoanID.Text = intLoanID


SQLConnection.Close()

I get the error "Index Out Of Range", indicating That SQL doesn't see the prior entries...but they are there.

Obviously, I messed up something but can't figure out what...can anyone help?

Milt
Reply With Quote
  #2  
Old 05-13-2014, 04:35 AM
PlausiblyDamp's Avatar
PlausiblyDampINVALID ATTEMPT TO READ-Error PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you are only after a single value you can just use cmd.ExecuteScalar instead of creating a reader, if you are using a reader you need to make sure you have called reader.Read() to have moved to the first record before trying to access anything via the reader.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 05-13-2014, 09:44 AM
sirmilt sirmilt is offline
Contributor
 
Join Date: Jan 2003
Location: Florida
Posts: 451
Default

Thank you for the response. I have never previously used the Execute Scalar before, it seemed like the best choice since it seems to be designed to extract a single value. Unfortunately the code suggested by MSDN failed to work. A copy of my code is below, I obviously got something wrong...can you help?

Using con As New SqlConnection("server=milton-hp\sqlexpress;database=MyCollections;Integrated Security=True;")
Dim cmd As New System.Data.SqlClient.SqlCommand
Dim SQL As String = "SELECT MAX(LoanID) AS LastLoanID FROM tblLoans;"
cmd.CommandType = System.Data.CommandType.Text
Dim intLastLoanID As Integer
Try
con.Open()
intLastLoanID = Convert.ToInt32(cmd.ExecuteScalar())
Catch ex As Exception
MsgBox("Data Error")
End Try
End Using

Thanks again,
Milt
Reply With Quote
  #4  
Old 05-13-2014, 09:58 AM
PlausiblyDamp's Avatar
PlausiblyDampINVALID ATTEMPT TO READ-Error PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Could you be a bit more specific than "failed to work"? Which line failed, what was the error?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 05-14-2014, 06:30 AM
sirmilt sirmilt is offline
Contributor
 
Join Date: Jan 2003
Location: Florida
Posts: 451
Default

Thanks for the continued interest.

The procedure executed every step up to "cmd.Connection.Open()"

It then stopped (shut down the program), with no error message.

Milt
Reply With Quote
  #6  
Old 05-15-2014, 02:26 PM
CharlieMay's Avatar
CharlieMay CharlieMay is offline
Regular
 
Join Date: Sep 2009
Location: Indiana
Posts: 89
Default

You are now using a Using statement, there should be no need to use the con.Open() line as the Using handles the opening and closing of the connection. Not sure why you wouldn't at the very least get your Generic error message you're displaying in the Catch but I'm pretty sure trying to open a connection that is already open would throw some error.

EDIT:
Actually after a closer look, it appears you are not setting the commandtext property of the command, nor are you setting the connection property.

cmd.CommandText = sql 'What SQL statement am I processing with the command
cmd.Connection = con 'What connection am I using to execute this command against.
__________________
I thought I knew what I was doing...
...And then I turned on Option Strict

Last edited by CharlieMay; 05-15-2014 at 02:39 PM.
Reply With Quote
  #7  
Old 05-15-2014, 05:42 PM
sirmilt sirmilt is offline
Contributor
 
Join Date: Jan 2003
Location: Florida
Posts: 451
Default

Thank you for the follow up and the time you spent to help an old amateur.

My problem s resolved and I'm getting the results I hoed for.

Milt
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
 
INVALID ATTEMPT TO READ-Error
INVALID ATTEMPT TO READ-Error
 
-->