Hi,
I am trying to retrieve a value from database, based on that value I want to insert records into DB.Let’s say I am retrieving tsmid which serves as the output parameter in the stored procedure.
.net/C# code is -
public void getTSmid()
-
{
-
int tsid=0;
-
tsid = System.Convert.ToInt32(SqlHelper.ExecuteReader(SqlHelper.tConnectionString,
-
CommandType.StoredProcedure, "GetTSMID_SP",
-
new SqlParameter("@PID", this.PId),
-
new SqlParameter("@EN", this.En),
-
new SqlParameter("@ESG",this.Esg),
-
new SqlParameter("@TSMID", SqlDbType.Int, 4, ParameterDirection.Output, true, 5, 0, "TSMId", DataRowVersion.Current, tsid)));
-
TSM = tsid;
-
}
-
I have the DB connection classes in DLL and above code in BLL.
I am holding the output value into the integer tsid variable which is in the end assigned to TSM variable. I am getting the following error “Unable to cast object of type 'System.Data.SqlClient.SqlDataReader' to type 'System.IConvertible'”
MY Q’: how to handle a output parameter in ASP.net?
Stored procedure looks like this: -
CREATE PROCEDURE [dbo].[GetTSMID_SP]
-
-
@EN Integer,
-
@PID Integer,
-
@ESG varchar(25),
-
@TSMID integer OUTPUT
-
AS
-
DECLARE @ TSMID int
-
DECLARE @total int
-
-
SELECT @total=Count(*) from tbl_Timer where EN =@EN and PID =@ PID and ESG =@ESG
-
if( @total = 0)
-
Begin
-
INSERT INTO tbl_Timer(EN, PID, ESG)
-
VALUES(@EN, @ PID, @ESG)
-
SELECT @ TSMID = @@Identity
-
end
-
else
-
begin
-
select @ TSMID=TSMID from tbl_Timer where EN =@ EN and PID =@PID and ESG =@ESG
-
end
-
1 3666
Well I am not sure why you are trying to convert your DataReader to an int, but yes that is the wrong thing to do.
Assuming your SQL procedure is correct (not sure what it's doing, its very nested)
If you added the SqlParameter for your output value correctly. (What you have it a bit over zealous but will probably work)
All you need to do is call the Execute function and then get the value from the parameter:
tsid= (int)sc.Parameters["@TSMID"].Value;
Sign in to post your reply or Sign up for a free account.
Similar topics
by: MS |
last post by:
Here's my simple stored procedure:
ALTER PROCEDURE GetMemberIDByEmail
@Email EmailAddress,
@ID int OUTPUT
AS
SELECT @ID = ID FROM tbl_Member WHERE Email=@Email
RETURN
|
by: James Brett |
last post by:
Hi
I've got this function that executes a stored procedure. One of the
parameters is an output parameter but for some reason it always returns 0.
Any clues?
Cheers
James
|
by: James Radke |
last post by:
Hello,
I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module...
|
by: Islamegy® |
last post by:
When i call stored procedure which have an output paramter, t'm unable to
get the value
How could i fix it??
here is the method....
----------------------------
userid = 0;
SqlParameter...
|
by: rockdale |
last post by:
Hi, All
How to get the output parameter's value when you use the SQLHelper
(Microsoft Data Access Block)?
When I try to access my ourput parm I got the following error.
...
|
by: c676228 |
last post by:
Hi everyone,
I wrote a store procedure that fetch one row data in the database based on
the parameter value I entered. After I created the store procedure,
the store procedure code looks like...
|
by: ashtek |
last post by:
Hi,
I have a generic function that executes a stored procedure & returns a
data table.
Code:
===
public static DataTable ExecuteStoredProcedure(string
strProc,SqlParameter paramArray)
{...
|
by: Igor |
last post by:
Hi
I have a drop down list on my page that lists some items returned from a
database. Each item has its GUID (or uniqueidentifier). I have to (in code
behind of the page) read this GUID, to hand...
|
by: klharding |
last post by:
I have an SQL Stored Procedure that is outputting (using scope_indentity) the PersonID after a record is inserted into the table. I need this outputted value to be returned to my application. I...
|
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
|
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: 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: 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,...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |