473,378 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

ExecuteScalar error

SCG
Am trying to read the primary key from an Access database table using
ExecuteScaler:

objSQLCommand = New OleDb.OleDbCommand
objSQLCommand.Connection = objConnection
objSQLCommand.CommandText = "SELECT PK_ID FROM tblBaz WHERE
FK_Item_ID=" & lintItemID & " AND Time_Sold=" &
cstrDateDelimiter & ldatDate & cstrDateDelimiter
lintID = objSQLCommand.ExecuteScalar()

(where cstrDateDelimiter is a constant "#")

I get the following error message on the ExecuteScalar command:
"A first chance exception of type 'System.Data.OleDb.OleDbException'
occurred in system.data.dll"

Can anyone a) tell me what I'm doing wrong or b) tell me how to get a
better idea of what the error is all about!?

Many Thanks

Sarah
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 20 '05 #1
3 3298
* Sa***@another-place.freeserve.co-dot-uk.no-spam.invalid (SCG) scripsit:
Am trying to read the primary key from an Access database table using
ExecuteScaler:

objSQLCommand = New OleDb.OleDbCommand
objSQLCommand.Connection = objConnection
objSQLCommand.CommandText = "SELECT PK_ID FROM tblBaz WHERE
FK_Item_ID=" & lintItemID & " AND Time_Sold=" &
cstrDateDelimiter & ldatDate & cstrDateDelimiter
lintID = objSQLCommand.ExecuteScalar()

(where cstrDateDelimiter is a constant "#")

I get the following error message on the ExecuteScalar command:
"A first chance exception of type 'System.Data.OleDb.OleDbException'
occurred in system.data.dll"


You will more likely get an answer here:

<news:microsoft.public.dotnet.framework.adonet>

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Nov 20 '05 #2
"SCG" <Sa***@another-place.freeserve.co-dot-uk.no-spam.invalid> wrote...
objSQLCommand = New OleDb.OleDbCommand
objSQLCommand.Connection = objConnection
objSQLCommand.CommandText = "SELECT PK_ID FROM tblBaz WHERE
FK_Item_ID=" & lintItemID & " AND Time_Sold=" &
cstrDateDelimiter & ldatDate & cstrDateDelimiter
lintID = objSQLCommand.ExecuteScalar()

(where cstrDateDelimiter is a constant "#") Can anyone a) tell me what I'm doing wrong or b) tell me how to get a
better idea of what the error is all about!?


I think I can help you narrow it down. Don't know what you have tried so
far but _any time_ you have a complex statement that generates an error it
is a good idea to simplify that statement.

You would be much better off using parameters (so look into that after you
find the problem) but the first thing you should try is to hardcode your
command text string. Don't concatenate anything and just type constants
where your variables are. You want to make certain the basic form is
correct, that the columns are spelled correctly and such. When you get that
working put back just one of the variables, for instance the lintItemID...
and again the point is to make incremental changes until you can find the
point where it breaks.

BTW, you might consider a helper function in place of concatenating
cstrDateDelimiter. Little helper functions are handy for formatting SQL
dates and strings and such. But as I mentioned earlier you should consider
using the parameters feature which eliminates concatenation altogether.

I assume lintID is of the same datatype as PK_ID right? And I assume you
have successfully accessed the file in some way previously? Your connection
string is properly formed correct?

Tom

Nov 20 '05 #3
Sarah,
My first thought would be to make this a command with parameters to
avoid having to work with the concatenation each time.
ex
==============
objSQLCommand.CommandText = "SELECT PK_ID FROM tblBaz WHERE (
FK_ItemID = ?) AND (Time_Sold = ?)"
objSQLCommand.Parameters.Add("item", OleDbType.Int).Value = lintItemID
objSQLCommand.Parameters.Add("dt", OleDbType.DateTime).Value = ldatDate
==============
If you want to just do this once you probably need lintItemID.ToString() and
ldatDate.ToString() assuming that the variables are an integer and a
DateTime respectively.

Depending on what is stored in your database you may need to use
ToShortDateString() for the DateTime variable or you won't get equality due
to the time part.

I'd reccomend that you get a copy of David Sceppa's book "ADO.NET Core
Reference" if you are going to be doing a lot of ADO.NET programming. It
has many good examples in both C# and VB.NET along with reasons for doing
the coding that way.

ADO.NET questions will receive better responses in
microsoft.public.dotnet.framework.adonet.

Ron Allen
"SCG" <Sa***@another-place.freeserve.co-dot-uk.no-spam.invalid> wrote in
message news:40**********@Usenet.com...
Am trying to read the primary key from an Access database table using
ExecuteScaler:

objSQLCommand = New OleDb.OleDbCommand
objSQLCommand.Connection = objConnection
objSQLCommand.CommandText = "SELECT PK_ID FROM tblBaz WHERE
FK_Item_ID=" & lintItemID & " AND Time_Sold=" &
cstrDateDelimiter & ldatDate & cstrDateDelimiter
lintID = objSQLCommand.ExecuteScalar()

(where cstrDateDelimiter is a constant "#")

I get the following error message on the ExecuteScalar command:
"A first chance exception of type 'System.Data.OleDb.OleDbException'
occurred in system.data.dll"

Can anyone a) tell me what I'm doing wrong or b) tell me how to get a
better idea of what the error is all about!?

Many Thanks

Sarah
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cmo63126 | last post by:
Why am I not able to retrieve the value of ID (which does exist) using ExecuteScalar()? Do i simply have to use ExecuteReader() instead? My problem snippet: string mySQL = "SELECT ID FROM...
2
by: WeiminZhang | last post by:
When I use the OleDb to connect a Oracle db, and use the ExecutScalar() method to get the count of a table, the return value can't be cast to a data type, say int, while this works fine for a SQL...
2
by: trialproduct2004 | last post by:
hi all i am having application which is connecting to databset and executing one query as below:- string str; str="select count(*) from table1; and then i am using sqlcommand to get result...
7
by: Neven Klofutar | last post by:
Hi, I have a problem with SqlHelper.ExecuteScalar ... When I try to execute SqlHelper.ExecuteScalar I get this message: "System.InvalidCastException: Object must implement IConvertible.". ...
5
by: bienwell | last post by:
Hi all, I have a problem with using myCommand.ExecuteScalar(). My question is : If the Web setup is incorrect, does it make command ExecuteScalar() work improperly ?? In my program, I was...
1
by: js | last post by:
I am using the following C# code and T-SQL to get result object from a SQL Server database. When my application runs, the ExecuteScalar returns "10/24/2006 2:00:00 PM" if inserting a duplicated...
2
by: Randy Smith | last post by:
Hi, I've got some weird behavior happening within one of the datamappers. It all has to do with inserting a new row, and returning the Id of the row being entered. Here is what the code...
2
by: Manikandan | last post by:
Hi, I have a table with following data Tablename:details No(varchar) Name(varchar) Updated(Datetime) 1 mm 10/10/2006 2 nn 02/12/2005 3 kk NULL I'm using executescalar to get the...
1
by: Manikandan | last post by:
Hi, I have a table with following data Tablename:details No(varchar) Name(varchar) Updated(Datetime) 1 mm 10/10/2006 2 nn 02/12/2005 3 kk NULL I'm using executescalar to get the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.