Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a stored procedure which will insert some data into a table, and retrieve back some data in sql. In front end using c#, i need to fetch back the result set from my procedure and need to use it.

Which statement i need to use? I found that executereader() is the solution, but this is not working. I should not use the out parameter in sql as well as c# as per company coding standards. Can anybody explain this with an example?

stored procedure
create procedure name
@patientId
as
begin
insert into table values(a,b,c);
select * from table where value = a;
end

in C#
var reader = cp.executereader();
var a=reader["value"].tostring();

This is the way i have implemented my functionality(representational only).

What I have tried:

create procedure name
@patientId
as
begin
insert into table values(a,b,c);
select * from table where value = a;
end

in C#
var reader = cp.executereader();
var a=reader["value"].tostring();

This is the way i have implemented my functionality(representational only).
Posted
Updated 13-Aug-17 23:52pm
v2
Comments
F-ES Sitecore 14-Aug-17 4:43am    
Your SP is generating two result sets. The INSERT is creating one (even though it doesn't retrieve data it will still tell you how many rows were inserted and that generates a result set), and the the SELECT is creating one (that contains the data you want). Your ExecuteReader is then reading the first set, the INSERT, so sees no data. The easiest way to handle this is to stop the INSERT generating a result set by enabling "NOCOUNT"

create procedure name
@patientId
as
begin

SET NOCOUNT ON
insert into table values(a,b,c);
select * from table where value = a;
end
Member 10945065 16-Aug-17 2:43am    
Thanks for your reply..Hope this works..Let me check by implementing this.

1 solution

Presuming you have made a valid connection to the database and have successfully created the record in the table.

After you have inserted the record you can select that record and it will be returned in the data reader.
SQL
Insert TableName
(Field1,Field2)
Value (@Value1, Value2)

SET @ID = Scope_Identity() -- gets the last ID from and identity field

Select * From TableName where ID = @ID

This is really basic stuff you can get from any article on CRUD - do some research.

BTW you stored procedure as written above makes absolutely no sense and does nothing you described.
 
Share this answer
 
Comments
Member 10945065 16-Aug-17 2:40am    
Thanks for your reply bro. But you didn't understand my question,

I need a c# solution, which should insert and retrieve data from DB, which may ExecuteReader, was not working. I need a working sample, may be a code sample..I have already mentioned, what i have given is representational, and it is not actual as i am working in healthcare sector, i am unable to give the same details. Thanks for your try.
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900