Hi C#ers,
I have a stored proc. which is as follows. Supposedly returning the ID
(LocationID) of the row I've just inserted (scope identity):
=========================================
CREATE PROCEDURE [dbo].[LocationInsert]
@LocationName varchar(32),
@LocationDescription varchar(256),
@LocationID int OUTPUT,
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [dbo].[Location] (
[LocationName],
[LocationDescription],
) VALUES (
@LocationName,
@LocationDescription,
)
SET @LocationID = Scope_Identity()
END
========================================
I then have the following code to insert the data from my code:
========================================
public void Insert(LocationEntity locationEntity)
{
DBCommandWrapper dbCommandWrapper =
this.Db.GetStoredProcCommandWrapper("LocationInser t");
dbCommandWrapper.AddInParameter("@LocationName", DbType.AnsiString,
locationEntity.Name);
dbCommandWrapper.AddInParameter("@LocationDescript ion",
DbType.AnsiString, locationEntity.Description.Value);
this.ExecuteSPNonQuery(dbCommandWrapper);
locationEntity.LocationID =
(int)dbCommandWrapper.GetParameterValue("@Location ID");
}
========================================
My question:
The Insert function is labeled "void". Will it return the ID
(LocationID) of the row I've just inserted? Infact, will the stored
procedure itself return the LocationID that I need?
SSP 4 3324
I think you will need to add "@LocationID" as a parameter to
dbCommandWrapper, and set it's direction to "Output". Then you will be able
to read the value back after executing the SP.
"ssp" <sa****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com... Hi C#ers,
I have a stored proc. which is as follows. Supposedly returning the ID (LocationID) of the row I've just inserted (scope identity): ========================================= CREATE PROCEDURE [dbo].[LocationInsert] @LocationName varchar(32), @LocationDescription varchar(256), @LocationID int OUTPUT, AS BEGIN SET NOCOUNT ON
INSERT INTO [dbo].[Location] ( [LocationName], [LocationDescription], ) VALUES ( @LocationName, @LocationDescription, )
SET @LocationID = Scope_Identity() END ========================================
I then have the following code to insert the data from my code:
======================================== public void Insert(LocationEntity locationEntity) { DBCommandWrapper dbCommandWrapper = this.Db.GetStoredProcCommandWrapper("LocationInser t");
dbCommandWrapper.AddInParameter("@LocationName", DbType.AnsiString, locationEntity.Name); dbCommandWrapper.AddInParameter("@LocationDescript ion", DbType.AnsiString, locationEntity.Description.Value);
this.ExecuteSPNonQuery(dbCommandWrapper); locationEntity.LocationID = (int)dbCommandWrapper.GetParameterValue("@Location ID"); }
========================================
My question:
The Insert function is labeled "void". Will it return the ID (LocationID) of the row I've just inserted? Infact, will the stored procedure itself return the LocationID that I need?
SSP
ssp,
From the fact that Insert() is a void, you know that it will not return
anything ... not even if you wish it would ;-) Make it an int instead
of a void and return the value of the @LocationID parameter, if that's
what you want it to do.
In addition, the @LocationID parameter would need to be added as an
output parameter to the command object calling dbo.LocationInsert. I
recognize the MSFT Enterprise Library helper methods you're using but am
not familiar with them ... offhand I suppose there is an AddOutParameter
method for the DBCommandWrapper object that you need to call to add that
parameter.
--Bob
ssp wrote: Hi C#ers,
I have a stored proc. which is as follows. Supposedly returning the ID (LocationID) of the row I've just inserted (scope identity): ========================================= CREATE PROCEDURE [dbo].[LocationInsert] @LocationName varchar(32), @LocationDescription varchar(256), @LocationID int OUTPUT, AS BEGIN SET NOCOUNT ON
INSERT INTO [dbo].[Location] ( [LocationName], [LocationDescription], ) VALUES ( @LocationName, @LocationDescription, )
SET @LocationID = Scope_Identity() END ========================================
I then have the following code to insert the data from my code:
======================================== public void Insert(LocationEntity locationEntity) { DBCommandWrapper dbCommandWrapper = this.Db.GetStoredProcCommandWrapper("LocationInser t");
dbCommandWrapper.AddInParameter("@LocationName", DbType.AnsiString, locationEntity.Name); dbCommandWrapper.AddInParameter("@LocationDescript ion", DbType.AnsiString, locationEntity.Description.Value);
this.ExecuteSPNonQuery(dbCommandWrapper); locationEntity.LocationID = (int)dbCommandWrapper.GetParameterValue("@Location ID"); }
========================================
My question:
The Insert function is labeled "void". Will it return the ID (LocationID) of the row I've just inserted? Infact, will the stored procedure itself return the LocationID that I need?
SSP
Some thing like this?
dbCommandWrapper.AddOutParameter("@LocationID", DbType.Int32, 4);
Thanks Brendan... it's working now.
Hi Bob,
Indeed I am using the MEL.
If you notice the Insert method, it takes an entity object as
parameter. So in this case I'll have to make sure it returns the
inserted entity with changed LocationID.
I've relabeled the Insert method as:
public LocationEntity Insert(LocationEntity locationEntity)
{
DBCommandWrapper dbCommandWrapper =
this.Db.GetStoredProcCommandWrapper("LocationInser t");
dbCommandWrapper.AddInParameter("@LocationName", DbType.AnsiString,
locationEntity.Name);
dbCommandWrapper.AddInParameter("@LocationDescript ion",
DbType.AnsiString, locationEntity.Description.Value);
dbCommandWrapper.AddOutParameter("@LocationID", DbType.Int32, 4);
this.ExecuteSPNonQuery(dbCommandWrapper);
locationEntity.LocationID =
(int)dbCommandWrapper.GetParameterValue("@Location ID");
}
This should do the trick... I hope
SSP This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Vladimir Grul |
last post by:
Hello,
I have a class member function declared as
class some_class {
....
virtual int call(void);
};
Can I use this-> inside the function body?
|
by: bob_jenkins |
last post by:
{
const void *p;
(void)memset((void *)p, ' ', (size_t)10);
}
Should this call to memset() be legal? Memset is of type
void *memset(void *, unsigned char, size_t)
Also, (void *) is the...
|
by: kernel.lover |
last post by:
hello,
I want to know if a fuction say malloc is declared as void *malloc()
then whats the significance of void here.
Does void * is used when function has the flexibility to return any
type of...
|
by: whatluo |
last post by:
Hi, c.l.cs
I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout\n"); while the others does't. So
can someone tell me whether there any gains in adding...
|
by: Alawna |
last post by:
Hi,
i was reading a chapter about c pointers in "c++builder complete
reference" and it is said there that malloc returns a pointer to the
start of the memory allocated but i see the prototype of...
|
by: Stijn van Dongen |
last post by:
A question about void*. I have a hash library where the hash create
function accepts functions
unsigned (*hash)(const void *a)
int (*cmp) (const void *a, const void *b)
The insert function...
|
by: maadhuu |
last post by:
hello, this is a piece of code ,which is giving an error.
#include<stdio.h>
int main()
{
int a =10;
void *p = &a;
printf("%d ", *p ); //error....why should it //be an error ?can't the...
|
by: Giannis Papadopoulos |
last post by:
I have the following code
#include <stdio.h>
void a(void) {
printf("a called.\n");
}
int b(void) {
printf("b called.\n");
|
by: Andreas Klimas |
last post by:
hello,
no many words, I will start with an example instead
int foo(void *r) {...}
void test(void) {
int *x=0;
foo(x); /* seems to me as valid */
}
|
by: PeterAPIIT |
last post by:
Hello all C++ expert programmer,
i have wrote partial general allocator for my container.
After reading standard C++ library and code guru article, i have
several questions.
1. Why...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |