Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to read value from database using sqlhelper.ExecuteReader & Stored procedure, Output parameter

How to read value from database using sqlhelper.ExecuteReader & Stored procedure, Output parameter

Scheduled Pinned Locked Moved C#
databasecsharpasp-netsql-server
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B BalasahebK

    Hello Friends Can someone help me. I know how to read value using SqlHelper.ExecuteReader using storedProcedure. But if stored Procedure has a output parameter, I'm not able to read output parameter value. How to read output parameter value. I could read output parameter value using SqlHelper.ExecuteNonQuery, but then I'm not able to read database query column values. how to read database query column values. pls help soon Regards Balasaheb :((

    Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

    G Offline
    G Offline
    Guffa
    wrote on last edited by
    #2

    Create a paramter object, set the direction to output and add it to the command object (but keep the reference to the parameter object). After you have closed the data reader, you can access the output value in the parameter object.

    --- b { font-weight: normal; }

    B 1 Reply Last reply
    0
    • B BalasahebK

      Hello Friends Can someone help me. I know how to read value using SqlHelper.ExecuteReader using storedProcedure. But if stored Procedure has a output parameter, I'm not able to read output parameter value. How to read output parameter value. I could read output parameter value using SqlHelper.ExecuteNonQuery, but then I'm not able to read database query column values. how to read database query column values. pls help soon Regards Balasaheb :((

      Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #3

      BalasahebK wrote:

      I could read output parameter value using SqlHelper.ExecuteNonQuery, but then I'm not able to read database query column values.

      There is no resultset returned, hence the name, ExecuteNonQuery. You retrieve the output param value the same way from ExecuteQuery as you do with ExecuteNonQuery.


      only two letters away from being an asset

      B 1 Reply Last reply
      0
      • N Not Active

        BalasahebK wrote:

        I could read output parameter value using SqlHelper.ExecuteNonQuery, but then I'm not able to read database query column values.

        There is no resultset returned, hence the name, ExecuteNonQuery. You retrieve the output param value the same way from ExecuteQuery as you do with ExecuteNonQuery.


        only two letters away from being an asset

        B Offline
        B Offline
        BalasahebK
        wrote on last edited by
        #4

        Thanks for ur reply In case of ExecuteReader:- My stored procedure contains two result set The actual format of stored procedure is ***************************Stored Procedure start************************************* declare @colid1 int declare @col2 uniqueidentifire declare @Status out set @col1 = "001" set @col2 = "{123456789}" declare @id int select @id = (select id from tableid where colid = @colid1) Select col1,col2,col3,col4 from tabel where col1 = @colid1 and col2= @col2 if (exists(select col1,col2 from table2 where col1 = @col1 and col2 = @colid)) { @Status = 1 } else { @Status =0 } select @Status as status *****************************Stored Procedure End**************************************** The above stored procedure is given for reference. Now my problem is I'm able to retreive column values using ExecuteReader by passing stored procedure properly ReaderObj = SqlHelper.ExecuteReader(Pass parameters) ReaderObj retreives only table1 columns values.......now how to read @Status value(look at above stored procedure):(( ***************************************************** arParams[0] = new SqlParameter("@prref",SqlDbType.VarChar,20); arParams[0].Value = strPrjRef; arParams[1] = new SqlParameter("@userid",SqlDbType.UniqueIdentifier); arParams[1].Value = new Guid("{7126886d-2538-429c-ab3c-22b3341dcecf}"); arParams[2] = new SqlParameter("@STATUS", SqlDbType.Int); arParams[2].Direction = ParameterDirection.Output; RdPrjDetails = SqlHelper.ExecuteReaderconnstr,CommandType.StoredProcedure, "GET_PROJECT_DETAILS_SP", arParams); ******************************************************* Pls help me in this regard

        Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

        1 Reply Last reply
        0
        • G Guffa

          Create a paramter object, set the direction to output and add it to the command object (but keep the reference to the parameter object). After you have closed the data reader, you can access the output value in the parameter object.

          --- b { font-weight: normal; }

          B Offline
          B Offline
          BalasahebK
          wrote on last edited by
          #5

          Thanks for ur reply In case of ExecuteReader:- My stored procedure contains two result set The actual format of stored procedure is ***************************Stored Procedure start************************************* declare @colid1 int declare @col2 uniqueidentifire declare @Status out set @col1 = "001" set @col2 = "{123456789}" declare @id int select @id = (select id from tableid where colid = @colid1) Select col1,col2,col3,col4 from tabel where col1 = @colid1 and col2= @col2 if (exists(select col1,col2 from table2 where col1 = @col1 and col2 = @colid)) { @Status = 1 } else { @Status =0 } select @Status as status *****************************Stored Procedure End**************************************** The above stored procedure is given for reference. Now my problem is I'm able to retreive column values using ExecuteReader by passing stored procedure properly ReaderObj = SqlHelper.ExecuteReader(Pass parameters) ReaderObj retreives only table1 columns values.......now how to read @Status value(look at above stored procedure) ***************************************************** arParams[0] = new SqlParameter("@prref",SqlDbType.VarChar,20); arParams[0].Value = strPrjRef; arParams[1] = new SqlParameter("@userid",SqlDbType.UniqueIdentifier); arParams[1].Value = new Guid("{7126886d-2538-429c-ab3c-22b3341dcecf}"); arParams[2] = new SqlParameter("@STATUS", SqlDbType.Int); arParams[2].Direction = ParameterDirection.Output; RdPrjDetails = SqlHelper.ExecuteReaderconnstr,CommandType.StoredProcedure, "GET_PROJECT_DETAILS_SP", arParams); ******************************************************* Pls help me in this regard:((

          Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

          G 1 Reply Last reply
          0
          • B BalasahebK

            Thanks for ur reply In case of ExecuteReader:- My stored procedure contains two result set The actual format of stored procedure is ***************************Stored Procedure start************************************* declare @colid1 int declare @col2 uniqueidentifire declare @Status out set @col1 = "001" set @col2 = "{123456789}" declare @id int select @id = (select id from tableid where colid = @colid1) Select col1,col2,col3,col4 from tabel where col1 = @colid1 and col2= @col2 if (exists(select col1,col2 from table2 where col1 = @col1 and col2 = @colid)) { @Status = 1 } else { @Status =0 } select @Status as status *****************************Stored Procedure End**************************************** The above stored procedure is given for reference. Now my problem is I'm able to retreive column values using ExecuteReader by passing stored procedure properly ReaderObj = SqlHelper.ExecuteReader(Pass parameters) ReaderObj retreives only table1 columns values.......now how to read @Status value(look at above stored procedure) ***************************************************** arParams[0] = new SqlParameter("@prref",SqlDbType.VarChar,20); arParams[0].Value = strPrjRef; arParams[1] = new SqlParameter("@userid",SqlDbType.UniqueIdentifier); arParams[1].Value = new Guid("{7126886d-2538-429c-ab3c-22b3341dcecf}"); arParams[2] = new SqlParameter("@STATUS", SqlDbType.Int); arParams[2].Direction = ParameterDirection.Output; RdPrjDetails = SqlHelper.ExecuteReaderconnstr,CommandType.StoredProcedure, "GET_PROJECT_DETAILS_SP", arParams); ******************************************************* Pls help me in this regard:((

            Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #6

            Ok, so you are not using an output parameter at all? It's a bit hard to tell, as the stored procedure that you show is lacking the entire declaration part. It's hard to say what parameters you should use in the calling code if you don't show what parameters the stored procedure uses. Also the stored procedure contains some errors that tells me that this is not the code that you are actually using. If you only show me some made up code, I can only give advice on that code, not on the code that you are actually using. It's like if you went to your car mechanic and said: "I've brough my bike. Can you take a look at it and tell me what's wrong with my car?" ;) If you have a query that returns more than one result, use the NextResult method of the data reader to move from the first result to the next.

            --- b { font-weight: normal; }

            B 2 Replies Last reply
            0
            • G Guffa

              Ok, so you are not using an output parameter at all? It's a bit hard to tell, as the stored procedure that you show is lacking the entire declaration part. It's hard to say what parameters you should use in the calling code if you don't show what parameters the stored procedure uses. Also the stored procedure contains some errors that tells me that this is not the code that you are actually using. If you only show me some made up code, I can only give advice on that code, not on the code that you are actually using. It's like if you went to your car mechanic and said: "I've brough my bike. Can you take a look at it and tell me what's wrong with my car?" ;) If you have a query that returns more than one result, use the NextResult method of the data reader to move from the first result to the next.

              --- b { font-weight: normal; }

              B Offline
              B Offline
              BalasahebK
              wrote on last edited by
              #7

              ok I'm giving u actual stored procedure, earlier stored procedure was given only for reference The original stored procedure (working perfectly without error) is SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO ALTER PROCEDURE GET_PROJECT_DETAILS_SP ( @prref varchar(20), @userid uniqueidentifier, @STATUS int output ) As begin declare @prjid int select @prjid = (select PRID from EET_PROJECTS where PRREF = @prref) IF ((SELECT PRISPATT FROM EET_PROJECTS WHERE PRID = @prjid) = 0) BEGIN Select P.PRID as PRID, P.PRREF as PRREF, --P.PRNAME as PRNAME, EUP.PROJECTNAME as PRNAME, PRACTIVE = case P.PRACTIVE when 1 then 1 else 0 end, P.PRDESC as PRDESC, EUP.PURCHASERNAME as PRPURCHASERNAME, EUP.ENDUSERNAME as PRENDUSERNAME, EUP.DESTINATION, P.PROWNER as PROWNER, P.PROWNLOC as PROWNLOC, P.PRMKTLOC as PRMKTLOC, '' as PRPATT, EUP.INDUSTRY as PRINDUSTRY, P.PRPROPOSAL as PRPROPOSAL, P.PRSALES as PRSALES, P.PRQUOTCURRENCY as PRQUOTCURRENCY, P.CREATED_DATE as PRDATE, CREATED_BY = (SELECT cast(FIRST_NAME + ' ' + LAST_NAME as varchar(161)) FROM EET_SEC_USERS WHERE USERID = P.CREATED_BY), P.LAST_UPDATED_DATE as PRUP, LAST_UPDATED_BY = (SELECT cast(FIRST_NAME + ' ' + LAST_NAME as varchar(161)) FROM EET_SEC_USERS WHERE USERID = P.LAST_UPDATED_BY), ((SELECT LOCCOUNTRY FROM EET_LOCATIONS WHERE LOCID = P.PRMKTLOC) + ' (' + P.PRMKTLOC + ')') as PRMKTLOCWITHCOUNTRY, EUP.GP3_FLAG, EUP.PROJECTORIGINATOR_NAME, EUP.PROJECTLEAD_NAME, EUP.SYS_FUNNEL_POSITION, EUP.SYS_KOB, EUP.SYS_PROBABILITY, EUP.SIS_CONTENT, EUP.SERV_FUNNEL_POSITION, EUP.SERV_KOB, EUP.SERV_PROBABILITY, P.PRISPATT, P.PRNOPATTREASON, NULL as PRPURCHASERID, case when P.PRUSECUSTPRICE = 1 then 'Yes' else 'No' end as USECUSTPRICE, P.PRCUSTPRICEID, (SELECT CUPRCUSTOMER FROM EET_CUSTOMER_PRICING WHERE CUPRID = P.PRCUSTPRICEID) as CUSTPRICENAME, dbo.FUNC_GET_LOCATION_LIST(P.PRCUSTPRICEID) as CUSTPRICELOCN, isnull(EUP.BOOK_DATE, getutcdate()) as PRBOOKDATE, EUP.transitional_order as PRTRANSPRJSTATUS from EET_PROJECTS P join EET_USER_PATT_DATA EUP on P.PRID = EUP.EUPDPROJECT where PRID = @prjid END if (exists(select opuserid, opprjid from EET_MY_PROJECTS_LISTS where opuserid = @userid and opprjid = @prjid)) begin SET @STATUS = 1 end else SET @STATUS = 0 select @STATUS as status end GO SET QUOTED_IDENTIFIER OFF GO SET A

              G 1 Reply Last reply
              0
              • G Guffa

                Ok, so you are not using an output parameter at all? It's a bit hard to tell, as the stored procedure that you show is lacking the entire declaration part. It's hard to say what parameters you should use in the calling code if you don't show what parameters the stored procedure uses. Also the stored procedure contains some errors that tells me that this is not the code that you are actually using. If you only show me some made up code, I can only give advice on that code, not on the code that you are actually using. It's like if you went to your car mechanic and said: "I've brough my bike. Can you take a look at it and tell me what's wrong with my car?" ;) If you have a query that returns more than one result, use the NextResult method of the data reader to move from the first result to the next.

                --- b { font-weight: normal; }

                B Offline
                B Offline
                BalasahebK
                wrote on last edited by
                #8

                ok problem has been solved using ExecuteReader. I'm using below code to read next Resultset. Reader = SqlHelper.ExecuteReader(pass parameters) if !(Reader == null) { while(Reader.Read()) { string str = Reader["param1"].ToString(); } Reader.NextResult While(Reader.read()) { string str1 = Reader["Param2"].ToString(); } } Many many thanks for ur help too. Thanks Keep in touch My email id:- kbalu_2001@rediffmail.com

                Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

                G 1 Reply Last reply
                0
                • B BalasahebK

                  ok I'm giving u actual stored procedure, earlier stored procedure was given only for reference The original stored procedure (working perfectly without error) is SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO ALTER PROCEDURE GET_PROJECT_DETAILS_SP ( @prref varchar(20), @userid uniqueidentifier, @STATUS int output ) As begin declare @prjid int select @prjid = (select PRID from EET_PROJECTS where PRREF = @prref) IF ((SELECT PRISPATT FROM EET_PROJECTS WHERE PRID = @prjid) = 0) BEGIN Select P.PRID as PRID, P.PRREF as PRREF, --P.PRNAME as PRNAME, EUP.PROJECTNAME as PRNAME, PRACTIVE = case P.PRACTIVE when 1 then 1 else 0 end, P.PRDESC as PRDESC, EUP.PURCHASERNAME as PRPURCHASERNAME, EUP.ENDUSERNAME as PRENDUSERNAME, EUP.DESTINATION, P.PROWNER as PROWNER, P.PROWNLOC as PROWNLOC, P.PRMKTLOC as PRMKTLOC, '' as PRPATT, EUP.INDUSTRY as PRINDUSTRY, P.PRPROPOSAL as PRPROPOSAL, P.PRSALES as PRSALES, P.PRQUOTCURRENCY as PRQUOTCURRENCY, P.CREATED_DATE as PRDATE, CREATED_BY = (SELECT cast(FIRST_NAME + ' ' + LAST_NAME as varchar(161)) FROM EET_SEC_USERS WHERE USERID = P.CREATED_BY), P.LAST_UPDATED_DATE as PRUP, LAST_UPDATED_BY = (SELECT cast(FIRST_NAME + ' ' + LAST_NAME as varchar(161)) FROM EET_SEC_USERS WHERE USERID = P.LAST_UPDATED_BY), ((SELECT LOCCOUNTRY FROM EET_LOCATIONS WHERE LOCID = P.PRMKTLOC) + ' (' + P.PRMKTLOC + ')') as PRMKTLOCWITHCOUNTRY, EUP.GP3_FLAG, EUP.PROJECTORIGINATOR_NAME, EUP.PROJECTLEAD_NAME, EUP.SYS_FUNNEL_POSITION, EUP.SYS_KOB, EUP.SYS_PROBABILITY, EUP.SIS_CONTENT, EUP.SERV_FUNNEL_POSITION, EUP.SERV_KOB, EUP.SERV_PROBABILITY, P.PRISPATT, P.PRNOPATTREASON, NULL as PRPURCHASERID, case when P.PRUSECUSTPRICE = 1 then 'Yes' else 'No' end as USECUSTPRICE, P.PRCUSTPRICEID, (SELECT CUPRCUSTOMER FROM EET_CUSTOMER_PRICING WHERE CUPRID = P.PRCUSTPRICEID) as CUSTPRICENAME, dbo.FUNC_GET_LOCATION_LIST(P.PRCUSTPRICEID) as CUSTPRICELOCN, isnull(EUP.BOOK_DATE, getutcdate()) as PRBOOKDATE, EUP.transitional_order as PRTRANSPRJSTATUS from EET_PROJECTS P join EET_USER_PATT_DATA EUP on P.PRID = EUP.EUPDPROJECT where PRID = @prjid END if (exists(select opuserid, opprjid from EET_MY_PROJECTS_LISTS where opuserid = @userid and opprjid = @prjid)) begin SET @STATUS = 1 end else SET @STATUS = 0 select @STATUS as status end GO SET QUOTED_IDENTIFIER OFF GO SET A

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #9

                  That's better, that stored procedure actually has an output parameter. Why are you returning the value of @STATUS both as an output parameter and a result? Anyway, the code that you showed earlier that created the parameter object should work just fine. After you have closed the reader, you can retrieve the value from the Value property of the parameter object. The value is returned boxed inside an object, so you have to unbox it by casting it to the type int.

                  --- b { font-weight: normal; }

                  1 Reply Last reply
                  0
                  • B BalasahebK

                    ok problem has been solved using ExecuteReader. I'm using below code to read next Resultset. Reader = SqlHelper.ExecuteReader(pass parameters) if !(Reader == null) { while(Reader.Read()) { string str = Reader["param1"].ToString(); } Reader.NextResult While(Reader.read()) { string str1 = Reader["Param2"].ToString(); } } Many many thanks for ur help too. Thanks Keep in touch My email id:- kbalu_2001@rediffmail.com

                    Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #10

                    Ok, but then you are not reading the value from the output parameter.

                    --- b { font-weight: normal; }

                    B 2 Replies Last reply
                    0
                    • G Guffa

                      Ok, but then you are not reading the value from the output parameter.

                      --- b { font-weight: normal; }

                      B Offline
                      B Offline
                      BalasahebK
                      wrote on last edited by
                      #11

                      yes, no need to use output parameter. if u know how to take value from output parameter then pls let me know. that solution will also helpful to me Thanks for ur help

                      Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

                      1 Reply Last reply
                      0
                      • G Guffa

                        Ok, but then you are not reading the value from the output parameter.

                        --- b { font-weight: normal; }

                        B Offline
                        B Offline
                        BalasahebK
                        wrote on last edited by
                        #12

                        Hello Guffa, I'm not reading the value from output parameter, that is the problem because when I write code like string str = arParams[2].value.ToString(); it return null and fires exceprtion. I'm not able to understand what exactly happing behind the screen. ok thanks for ur help

                        Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

                        G 1 Reply Last reply
                        0
                        • B BalasahebK

                          Hello Guffa, I'm not reading the value from output parameter, that is the problem because when I write code like string str = arParams[2].value.ToString(); it return null and fires exceprtion. I'm not able to understand what exactly happing behind the screen. ok thanks for ur help

                          Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #13

                          The property is named Value, not value. Other than that, the code is correct.

                          BalasahebK wrote:

                          it return null and fires exceprtion

                          What exception does it fire? What is the error message?

                          --- b { font-weight: normal; }

                          B 1 Reply Last reply
                          0
                          • G Guffa

                            The property is named Value, not value. Other than that, the code is correct.

                            BalasahebK wrote:

                            it return null and fires exceprtion

                            What exception does it fire? What is the error message?

                            --- b { font-weight: normal; }

                            B Offline
                            B Offline
                            BalasahebK
                            wrote on last edited by
                            #14

                            now my prob. has been solved using output parameter too. it can be possible to retreive value after reder.nextresult. ok thanks for ur help have nice day bye now

                            Balasaheb Software Developer Platform: Asp.net,vb.net Database: SQL Server 2000

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups