472,377 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,377 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 5044
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

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.