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. Web Development
  3. ASP.NET
  4. Stored Procedures retrieving values

Stored Procedures retrieving values

Scheduled Pinned Locked Moved ASP.NET
databasesql-serversysadminsecuritytutorial
7 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.
  • G Offline
    G Offline
    greendragons
    wrote on last edited by
    #1

    I am new to stored procedures. I want to know how to retrieve string values from stored procedures. Like i created a website in which when user clicks a particular artists image then it's file location is picked up from sql server. But im not getting way to retrieve from there through procedures. -----stored procedure------- create procedure arts (@Artist nchar(50)) AS return (SELECT ret =   Location from Artist_loc where Artist = @Artist ) in aspx.cs page----------- string Artist = Request.QueryString["art"];                SqlConnection cn = new SqlConnection(@"Data Source=BLACK_RHYMES-PC\MSSMLBIZ;Initial Catalog=Muzik;Integrated Security=True");             SqlCommand cmd = new SqlCommand();             cmd.CommandText = "dbo.arts";             cmd.Parameters.Add("@Artist",SqlDbType.NChar,50).Value = Artist;                            cmd.CommandType = CommandType.StoredProcedure;                         cmd.Connection = cn; i got struck after this what lines should i add to retrieve results..... *THNX*

    A 1 Reply Last reply
    0
    • G greendragons

      I am new to stored procedures. I want to know how to retrieve string values from stored procedures. Like i created a website in which when user clicks a particular artists image then it's file location is picked up from sql server. But im not getting way to retrieve from there through procedures. -----stored procedure------- create procedure arts (@Artist nchar(50)) AS return (SELECT ret =   Location from Artist_loc where Artist = @Artist ) in aspx.cs page----------- string Artist = Request.QueryString["art"];                SqlConnection cn = new SqlConnection(@"Data Source=BLACK_RHYMES-PC\MSSMLBIZ;Initial Catalog=Muzik;Integrated Security=True");             SqlCommand cmd = new SqlCommand();             cmd.CommandText = "dbo.arts";             cmd.Parameters.Add("@Artist",SqlDbType.NChar,50).Value = Artist;                            cmd.CommandType = CommandType.StoredProcedure;                         cmd.Connection = cn; i got struck after this what lines should i add to retrieve results..... *THNX*

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      Use Output parameter

      Create procedure arts
      @Artist nchar(50),
      @Ret NVARCHAR(40) OUTPUT
      BEGIN
      ...
      ...
      SET @Ret = yourresult...
      END

      From code write

      SqlParameter oparam = new SqlParameter("@Ret", SqlDbType.NVarchar, 50);
      oparam.Direction=ParameterDirection.Output;
      cmd.Parameters.Add(oparam);
      cmd.ExecuteNonQuery();

      Now you will find the return statement in oparam.value. . :rose:

      Abhishek Sur


      My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

      **Don't forget to click "Good Answer" if you like to.

      G 1 Reply Last reply
      0
      • A Abhishek Sur

        Use Output parameter

        Create procedure arts
        @Artist nchar(50),
        @Ret NVARCHAR(40) OUTPUT
        BEGIN
        ...
        ...
        SET @Ret = yourresult...
        END

        From code write

        SqlParameter oparam = new SqlParameter("@Ret", SqlDbType.NVarchar, 50);
        oparam.Direction=ParameterDirection.Output;
        cmd.Parameters.Add(oparam);
        cmd.ExecuteNonQuery();

        Now you will find the return statement in oparam.value. . :rose:

        Abhishek Sur


        My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

        **Don't forget to click "Good Answer" if you like to.

        G Offline
        G Offline
        greendragons
        wrote on last edited by
        #3

        Create procedure arts @Artist nchar(50), @Ret NVARCHAR(40) OUTPUT begin set @ret = select Location from Artist_loc where Artist = @Artist end it's giving error incorrect syntax near keyword 'select'

        A 1 Reply Last reply
        0
        • G greendragons

          Create procedure arts @Artist nchar(50), @Ret NVARCHAR(40) OUTPUT begin set @ret = select Location from Artist_loc where Artist = @Artist end it's giving error incorrect syntax near keyword 'select'

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          greendragons wrote:

          set @ret = select Location from Artist_loc where Artist = @Artist

          It should be

          Select TOP 1 @ret = Location from Artist_loc where Artist= @Artist

          SET will only be used when you initialize variable like

          SET @x = 10

          Hope you got the difference between the two. :thumbsup:

          Abhishek Sur


          My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

          **Don't forget to click "Good Answer" if you like to.

          G 1 Reply Last reply
          0
          • A Abhishek Sur

            greendragons wrote:

            set @ret = select Location from Artist_loc where Artist = @Artist

            It should be

            Select TOP 1 @ret = Location from Artist_loc where Artist= @Artist

            SET will only be used when you initialize variable like

            SET @x = 10

            Hope you got the difference between the two. :thumbsup:

            Abhishek Sur


            My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

            **Don't forget to click "Good Answer" if you like to.

            G Offline
            G Offline
            greendragons
            wrote on last edited by
            #5

            Sry...still giving incorrect syntax near keword select :(

            C A 2 Replies Last reply
            0
            • G greendragons

              Sry...still giving incorrect syntax near keword select :(

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

              The code he gave you is correct. I suggest you buy a book on SQL, and ask SQL questions in the SQL forums.

              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
              • G greendragons

                Sry...still giving incorrect syntax near keword select :(

                A Offline
                A Offline
                Abhishek Sur
                wrote on last edited by
                #7

                have you done like this ?

                Create procedure arts
                @Artist nchar(50),
                @Ret NVARCHAR(40) OUTPUT
                AS
                begin
                Select TOP 1 @ret = Location from Artist_loc
                where Artist = @Artist
                end

                :confused:

                Abhishek Sur


                My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

                **Don't forget to click "Good Answer" if you like to.

                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