473,399 Members | 2,858 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,399 software developers and data experts.

Handling output Sqlparameter

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
Expand|Select|Wrap|Line Numbers
  1. public void getTSmid()
  2.         {
  3.             int tsid=0;
  4.            tsid = System.Convert.ToInt32(SqlHelper.ExecuteReader(SqlHelper.tConnectionString,
  5.             CommandType.StoredProcedure, "GetTSMID_SP",
  6.             new SqlParameter("@PID", this.PId), 
  7.             new SqlParameter("@EN", this.En),
  8.             new SqlParameter("@ESG",this.Esg),
  9.             new SqlParameter("@TSMID", SqlDbType.Int, 4, ParameterDirection.Output, true, 5, 0, "TSMId", DataRowVersion.Current, tsid)));
  10.             TSM = tsid;
  11.         }
  12.  
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:
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE [dbo].[GetTSMID_SP] 
  2.  
  3. @EN Integer,
  4. @PID Integer,
  5. @ESG varchar(25),
  6. @TSMID integer OUTPUT
  7. AS
  8. DECLARE @ TSMID int
  9. DECLARE @total  int
  10.  
  11. SELECT @total=Count(*) from tbl_Timer where EN =@EN and  PID =@ PID and  ESG =@ESG
  12.     if( @total = 0)
  13.         Begin
  14.             INSERT INTO tbl_Timer(EN, PID,  ESG)
  15.             VALUES(@EN, @ PID,  @ESG)
  16.             SELECT @ TSMID = @@Identity
  17.         end
  18.     else
  19.         begin
  20.             select @ TSMID=TSMID from tbl_Timer where EN =@ EN and  PID =@PID and  ESG =@ESG
  21.         end
  22.  
Feb 13 '08 #1
1 3666
Plater
7,872 Expert 4TB
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;
Feb 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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
3
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
4
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...
1
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...
0
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. ...
6
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...
7
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) {...
1
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...
4
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...
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: 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
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
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

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.