473,781 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling output Sqlparameter

5 New Member
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.Sq lClient.SqlData Reader' to type 'System.IConver tible'”

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 3692
Plater
7,872 Recognized Expert Expert
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.Paramet ers["@TSMID"].Value;
Feb 14 '08 #2

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

Similar topics

5
25401
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
1361
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
7618
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 (note that this class module represents the business layer of code NOT the gui layer): Public Function Test(ByVal Parm1 As Integer, ByVal Parm2 As Integer) As SqlDataReader ' Declare the SQL data layer class Dim oSQL As New...
1
1173
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 collection = new SqlParameter { new SqlParameter("@ID",DBNull.Value),
0
2328
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. System.NullReferenceException: Object reference not set to an instance of an object. I thought it is because there is a statement cmd.Parameters.Clear() in
6
2732
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 this: ALTER proc getProductCommScale @product As varchar(30), @TISCommRate As Decimal(5,2) OUTPUT, @BrokerCommRate As Decimal(5,2) OUTPUT, @Fee As Decimal(5,2) OUTPUT As if RTRIM(@product)='Imed' Select @TISCommRate=TISComm,...
7
8564
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) { SqlCommand command = new SqlCommand(strProc, connection); command.CommandType = CommandType.StoredProcedure;
1
1381
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 it over to a query (stored procedure) that takes the GUID and looks for some data in a specific table. I don't know have to hand it to the query as GUID. SqlCommand myCommand = new SqlCommand(); myCommand.Connection = myConnection;
4
26004
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 have tried the following but I am getting errors..."Non-invocable member 'System.Data.SqlClient.SqlCommand.Parameters' cannot be used like a method." on the line "cmd.Parameters("@PersonID").Direction = ParameterDirection.Output;" public long...
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8964
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.