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. Return Value from Stored Procedure

Return Value from Stored Procedure

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasehelp
7 Posts 2 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.
  • A Offline
    A Offline
    ahayw01
    wrote on last edited by
    #1

    Hello, I have a asp.net website and a C# web page and I am calling a stored procedure to return the email address of the user that is logged in. I have debugged my code and am presented with the following error message. Cannot implicitly convert type 'System.Data.SqlClient.SqlParameter' to 'string' Any assistance you can provide would be much appreciated. My code for the web page class and my code for the stored procedure are below. Thank you, Allison EmailTo Class public class EmailTo { public string getEmailTo() { // Command - specify as StoredProcedure using (System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(ConfigurationManager. ConnectionStrings["ConnectionString2"].ConnectionString)) { System.Data.SqlClient.SqlCommand myCommand2 = new System.Data.SqlClient.SqlCommand(); myCommand2.CommandText = "usp_get_email_to"; myCommand2.CommandType = CommandType.StoredProcedure; myCommand2.Connection = myConnection; // Return value as parameter SqlParameter returnEmail = new SqlParameter("Email", SqlDbType.NVarChar); returnEmail.Direction = ParameterDirection.ReturnValue; myCommand2.Parameters.Add(returnEmail); // Execute the stored procedure myConnection.Open(); myCommand2.ExecuteNonQuery(); myConnection.Close(); return returnEmail; } } } Stored Procedure CREATE PROCEDURE dbo.usp_get_email_to @Email nvarchar(256), @UserId uniqueidentifier AS SET @Email = (SELECT Email FROM aspnet_Membership WHERE UserId=@UserId) RETURN @Email GO

    S 1 Reply Last reply
    0
    • A ahayw01

      Hello, I have a asp.net website and a C# web page and I am calling a stored procedure to return the email address of the user that is logged in. I have debugged my code and am presented with the following error message. Cannot implicitly convert type 'System.Data.SqlClient.SqlParameter' to 'string' Any assistance you can provide would be much appreciated. My code for the web page class and my code for the stored procedure are below. Thank you, Allison EmailTo Class public class EmailTo { public string getEmailTo() { // Command - specify as StoredProcedure using (System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(ConfigurationManager. ConnectionStrings["ConnectionString2"].ConnectionString)) { System.Data.SqlClient.SqlCommand myCommand2 = new System.Data.SqlClient.SqlCommand(); myCommand2.CommandText = "usp_get_email_to"; myCommand2.CommandType = CommandType.StoredProcedure; myCommand2.Connection = myConnection; // Return value as parameter SqlParameter returnEmail = new SqlParameter("Email", SqlDbType.NVarChar); returnEmail.Direction = ParameterDirection.ReturnValue; myCommand2.Parameters.Add(returnEmail); // Execute the stored procedure myConnection.Open(); myCommand2.ExecuteNonQuery(); myConnection.Close(); return returnEmail; } } } Stored Procedure CREATE PROCEDURE dbo.usp_get_email_to @Email nvarchar(256), @UserId uniqueidentifier AS SET @Email = (SELECT Email FROM aspnet_Membership WHERE UserId=@UserId) RETURN @Email GO

      S Offline
      S Offline
      Sandeep Mewara
      wrote on last edited by
      #2

      ahayw01 wrote:

      SqlParameter returnEmail = new SqlParameter("Email", SqlDbType.NVarChar); returnEmail.Direction = ParameterDirection.ReturnValue; myCommand2.Parameters.Add(returnEmail);

      1. parameter should start with '@'

      SqlParameter returnEmail = new SqlParameter("@Email", SqlDbType.NVarChar);

      2. parameter value is missing - add this below line before adding the parameter to myCommand2

      returnEmail.Value = "abc@codeproject.com"

      A 1 Reply Last reply
      0
      • S Sandeep Mewara

        ahayw01 wrote:

        SqlParameter returnEmail = new SqlParameter("Email", SqlDbType.NVarChar); returnEmail.Direction = ParameterDirection.ReturnValue; myCommand2.Parameters.Add(returnEmail);

        1. parameter should start with '@'

        SqlParameter returnEmail = new SqlParameter("@Email", SqlDbType.NVarChar);

        2. parameter value is missing - add this below line before adding the parameter to myCommand2

        returnEmail.Value = "abc@codeproject.com"

        A Offline
        A Offline
        ahayw01
        wrote on last edited by
        #3

        Sandeep, Thank you so much for your quick response. I am a little confused about step 2. I think that I may not have been clear in my initial post. The returnEmail.Value should be pulling from the value that is stored in the sql table. However, I had thought that the existing code will account for that returnEmail.Direction = ParameterDirection.ReturnValue; myCommand2.Parameters.Add(returnEmail); Any additional suggestions would be appreciated. My apologies for any confusion. Allison

        S 1 Reply Last reply
        0
        • A ahayw01

          Sandeep, Thank you so much for your quick response. I am a little confused about step 2. I think that I may not have been clear in my initial post. The returnEmail.Value should be pulling from the value that is stored in the sql table. However, I had thought that the existing code will account for that returnEmail.Direction = ParameterDirection.ReturnValue; myCommand2.Parameters.Add(returnEmail); Any additional suggestions would be appreciated. My apologies for any confusion. Allison

          S Offline
          S Offline
          Sandeep Mewara
          wrote on last edited by
          #4

          Did you tried adding '@' before the parameter Email?

          A S 2 Replies Last reply
          0
          • S Sandeep Mewara

            Did you tried adding '@' before the parameter Email?

            S Offline
            S Offline
            Sandeep Mewara
            wrote on last edited by
            #5

            Furtherm, Where from your @UserID parameter is supplied? Shouldn't that too be added in the Command Parameters?

            1 Reply Last reply
            0
            • S Sandeep Mewara

              Did you tried adding '@' before the parameter Email?

              A Offline
              A Offline
              ahayw01
              wrote on last edited by
              #6

              Hello, Thanks yes, I did apply step 1. And was still receiving the same error message. However, just tried and tested the following and now it works return returnEmail.Value as String; Thanks again for your help, Allison

              S 1 Reply Last reply
              0
              • A ahayw01

                Hello, Thanks yes, I did apply step 1. And was still receiving the same error message. However, just tried and tested the following and now it works return returnEmail.Value as String; Thanks again for your help, Allison

                S Offline
                S Offline
                Sandeep Mewara
                wrote on last edited by
                #7

                :thumbsup:

                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