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. problem with stored procedure

problem with stored procedure

Scheduled Pinned Locked Moved C#
helpcsharpdatabase
14 Posts 5 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.
  • R RongNK

    I have 1 table student(ID,Name,Address). and i make a stored procedure CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] @MaxID as int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) end else Begin SET @MaxID=1 END return @MaxID END so i can't get the MaxID in C#. Please help me ! sorry for my english.

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #3

    Why would you want to?

    R 1 Reply Last reply
    0
    • C Christian Graus

      Why can't you ? What have you tried ? ExecuteScalar may work, if not, just build a data reader and read your field.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      R Offline
      R Offline
      RongNK
      wrote on last edited by
      #4

      How to get return value in C# code ? :-O Thank !

      C 1 Reply Last reply
      0
      • P PIEBALDconsult

        Why would you want to?

        R Offline
        R Offline
        RongNK
        wrote on last edited by
        #5

        Get the value that returned :-O Thank !

        P 1 Reply Last reply
        0
        • R RongNK

          How to get return value in C# code ? :-O Thank !

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #6

          So, if I get this right, what you're saying is that you don't know anything about C# at all and have done nothing to try to learn it ?

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          R 1 Reply Last reply
          0
          • C Christian Graus

            So, if I get this right, what you're saying is that you don't know anything about C# at all and have done nothing to try to learn it ?

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            R Offline
            R Offline
            RongNK
            wrote on last edited by
            #7

            Sorry, i have learned C# for 2 weeks :) SqlConnection connect = new SqlConnection(ConnectionFactory.strconn); connect.Open(); cmd = new SqlCommand("Proc_student_Add", connect); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("MaxID",varScenario.ID)); With that stored procedured i can't get return value by "int i = cmd.ExecuteNonQuery();", please help me to resolve this problem. Thank !

            L C 2 Replies Last reply
            0
            • R RongNK

              I have 1 table student(ID,Name,Address). and i make a stored procedure CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] @MaxID as int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) end else Begin SET @MaxID=1 END return @MaxID END so i can't get the MaxID in C#. Please help me ! sorry for my english.

              L Offline
              L Offline
              lrsoft2009
              wrote on last edited by
              #8

              CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] ( @MaxID int OUTPUT ) as if exists(SELECT [ID] FROM student) SELECT @MaxID = MAX(ID) FROM student else SET @MaxID=1 ---------------------------- Because '@MaxID' is OUTPUT So Your stored procedure... :) ---------------------------- Chinese programmers: 狼人软件

              1 Reply Last reply
              0
              • R RongNK

                Sorry, i have learned C# for 2 weeks :) SqlConnection connect = new SqlConnection(ConnectionFactory.strconn); connect.Open(); cmd = new SqlCommand("Proc_student_Add", connect); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("MaxID",varScenario.ID)); With that stored procedured i can't get return value by "int i = cmd.ExecuteNonQuery();", please help me to resolve this problem. Thank !

                L Offline
                L Offline
                lrsoft2009
                wrote on last edited by
                #9

                SqlCommand sampleCMD = new SqlCommand("SampleProc", nwindConn); sampleCMD.CommandType = CommandType.StoredProcedure; SqlParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int); sampParm.Direction = ParameterDirection.ReturnValue; sampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12); sampParm.Value = "Sample Value"; sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28); sampParm.Direction = ParameterDirection.Output; nwindConn.Open(); SqlDataReader sampReader = sampleCMD.ExecuteReader(); Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1)); while (sampReader.Read()) { Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1)); } sampReader.Close(); nwindConn.Close(); Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value); Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);

                C R 2 Replies Last reply
                0
                • R RongNK

                  Sorry, i have learned C# for 2 weeks :) SqlConnection connect = new SqlConnection(ConnectionFactory.strconn); connect.Open(); cmd = new SqlCommand("Proc_student_Add", connect); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("MaxID",varScenario.ID)); With that stored procedured i can't get return value by "int i = cmd.ExecuteNonQuery();", please help me to resolve this problem. Thank !

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #10

                  If you've been learning C# for two weeks, why are you working on calling stored procedures ? There's no way that's a sensible next step for you at this point. The MSDN site is a good place to look for documentation. You should really also own at least one C# book. Either way, ExecuteNonQuery returns a number that tells you how many rows were changed. The documentation will tell you this. I told you in the first instance that you should call ExecuteScalar, if you had any idea what you were doing, or willingness to do a little research, you should have been able to work that out. Even the intellisense would show you the answer. If you're doing a course, you should talk to your teacher about getting some resources to help you do some basic research. If, as I expect, you're doing paid work, then you're yet another example of the disgraceful excuse for an industry that has popped up in the third world, where people with no experience work on paid systems and then expect the people in the West who lose their jobs to you, to do the work for you. There is no level on which someone who started to learn C# two weeks ago, should be working on this sort of code, unless they have sufficient experience in another language to be able to do some basic research for themselves and to understand a post like my initial reply.

                  Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                  1 Reply Last reply
                  0
                  • L lrsoft2009

                    SqlCommand sampleCMD = new SqlCommand("SampleProc", nwindConn); sampleCMD.CommandType = CommandType.StoredProcedure; SqlParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int); sampParm.Direction = ParameterDirection.ReturnValue; sampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12); sampParm.Value = "Sample Value"; sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28); sampParm.Direction = ParameterDirection.Output; nwindConn.Open(); SqlDataReader sampReader = sampleCMD.ExecuteReader(); Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1)); while (sampReader.Read()) { Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1)); } sampReader.Close(); nwindConn.Close(); Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value); Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #11

                    That's a whole lot more work than is necessary, but I accept it would work.

                    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                    1 Reply Last reply
                    0
                    • L lrsoft2009

                      SqlCommand sampleCMD = new SqlCommand("SampleProc", nwindConn); sampleCMD.CommandType = CommandType.StoredProcedure; SqlParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int); sampParm.Direction = ParameterDirection.ReturnValue; sampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12); sampParm.Value = "Sample Value"; sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28); sampParm.Direction = ParameterDirection.Output; nwindConn.Open(); SqlDataReader sampReader = sampleCMD.ExecuteReader(); Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1)); while (sampReader.Read()) { Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1)); } sampReader.Close(); nwindConn.Close(); Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value); Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);

                      R Offline
                      R Offline
                      RongNK
                      wrote on last edited by
                      #12

                      Thank you very much :-O

                      1 Reply Last reply
                      0
                      • R RongNK

                        I have 1 table student(ID,Name,Address). and i make a stored procedure CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] @MaxID as int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) end else Begin SET @MaxID=1 END return @MaxID END so i can't get the MaxID in C#. Please help me ! sorry for my english.

                        D Offline
                        D Offline
                        deepikadurge
                        wrote on last edited by
                        #13

                        REATE PROCEDURE Proc_Scenario_GetMaxID @MaxID int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) return 0 end else Begin SET @MaxID=1 return 1 END end code for execution :-- declare @i int exec Proc_Scenario_GetMaxID @i output print convert(char(4),@i))

                        1 Reply Last reply
                        0
                        • R RongNK

                          Get the value that returned :-O Thank !

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #14

                          For what purpose?

                          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