473,385 Members | 1,942 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,385 software developers and data experts.

ExecuteScalar method sometimes work, sometimes doesn't

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 looks like that is getting "Object reference not set
to an instance ..." run-time error:

string ateId = cm.ExecuteScalar().ToString(); ß------------
this line crashes

None of the parameters are null, nor do they appear to have any illegal data
inside of them. The above code inserts a row into the AuditTrailEntry
table.

On the other hand, this code inserts a new jobStep row, and doesn't cause
any errors:

if (storedProcedure == "updateJobStep") //use this if update

cm.ExecuteNonQuery(); // no return value needed

else

{

string jobStepId = cm.ExecuteScalar().ToString(); //insert new
jobStep, get id of new row

js.Id = Convert.ToInt16(jobStepId);

}

Does anyone see a problem with this code, or know why it doesn't work?

TIA, Randy
Apr 23 '07 #1
2 5093
ExcuteScalar returns the first column value of the first row of the
first result set. if the first result set has no rows, this routine will
throw an error. would need the sp code to determine why the first result
set has no rows.

-- bruce (sqlwork.com)
Randy Smith wrote:
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 looks like that is getting "Object reference not set
to an instance ..." run-time error:

string ateId = cm.ExecuteScalar().ToString(); ß------------
this line crashes

None of the parameters are null, nor do they appear to have any illegal data
inside of them. The above code inserts a row into the AuditTrailEntry
table.

On the other hand, this code inserts a new jobStep row, and doesn't cause
any errors:

if (storedProcedure == "updateJobStep") //use this if update

cm.ExecuteNonQuery(); // no return value needed

else

{

string jobStepId = cm.ExecuteScalar().ToString(); //insert new
jobStep, get id of new row

js.Id = Convert.ToInt16(jobStepId);

}

Does anyone see a problem with this code, or know why it doesn't work?

TIA, Randy

Apr 23 '07 #2
Hi,
One of my coworker programmers solved the problem. The actual stored
procedure MUST return the value needed with code such as this:
SELECT SCOPE_IDENTITY() AS [AuditTrailEntriesId] //REPLACE
"AuditTrailEntriesId" with the name of the field you need to have returned

HTH, Randy


"bruce barker" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
ExcuteScalar returns the first column value of the first row of the first
result set. if the first result set has no rows, this routine will throw
an error. would need the sp code to determine why the first result set has
no rows.

-- bruce (sqlwork.com)
Randy Smith wrote:
>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 looks like that is getting "Object reference not
set to an instance ..." run-time error:

string ateId = cm.ExecuteScalar().ToString();
ß------------ this line crashes

None of the parameters are null, nor do they appear to have any illegal
data inside of them. The above code inserts a row into the
AuditTrailEntry table.

On the other hand, this code inserts a new jobStep row, and doesn't cause
any errors:

if (storedProcedure == "updateJobStep") //use this if update

cm.ExecuteNonQuery(); // no return value needed

else

{

string jobStepId = cm.ExecuteScalar().ToString(); //insert
new jobStep, get id of new row

js.Id = Convert.ToInt16(jobStepId);

}

Does anyone see a problem with this code, or know why it doesn't work?

TIA, Randy
Apr 24 '07 #3

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

Similar topics

0
by: Shane | last post by:
I've got a C# ASP.NET web form that fails to validate under certain conditions. I have a radio button that causes an AutoPostback that enables certain other elements on the form. If I click that...
24
by: Marcin Vorbrodt | last post by:
Hi there. How come when in one file i unclude <cmath> i need to use std::tan(...), and in another file i include <cassert> and std::assert fails to compile. Nowhere in my code i do using namespace...
1
by: Thomas | last post by:
It looks like the String.replace doesn't work in IE6.1. Anyone else has the same problem. I am using newest service package of IE and Win2K. Thanks
1
by: anon | last post by:
How does the DataView.Sort method really work under the covers? I know that it sorts a dataview, but what kind of sorting algorhythm is actually used? Thanks.
2
by: DavidGeorge | last post by:
I'm having trouble with what seems like a very simple function, although it's the first I've used in Access so I could have it all mixed up. It still works fine in forms I designed until...
2
by: Dinesh | last post by:
Hi, I have one stored procedure in SQL server in which i have written one insert statement. Now in my cs file i pass the parameters require to execute that stored procedure and finaly by mistaken...
2
by: buran | last post by:
Dear .NET Programmers, I use the following code snippet but can't figure out what is going wrong since the result object equals to nothing although it shouldn't be :) Function...
4
by: ewolfman | last post by:
Hi, I'm trying to retrieve a private method using reflection, which exists in my ASP.NET Page. It works only if I change the method's accessor to 'protected'. BTW: this occurs only for Page's...
1
by: mathewda | last post by:
Hey, I'm having a problem that I consider kinda weird that is alluding me at the moment. I've wrote some code that will set up an XMLHttpRequest, it then makes a call to open and send and sets the...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.