Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8169769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:57:05+00:00 2026-06-06T20:57:05+00:00

I have a stored procedure that adds a user and at each permission I

  • 0

I have a stored procedure that adds a user and at each permission I add, I want to start building a success message.

My stored procedure runs fine but how do I get that success message back into a message dialog in my app?

I want to display the below @text in a messagebox in my C# app.

DECLARE @text NVARCHAR(1000)
SET @text = 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'
SELECT @text

This is my call in my C# app:

    public DataTable CreateOrDropUser(string dataBase, string procedure, SqlParameter[] parameters)
    {
       try
       {
          if (dataBase.Length > 0) { procedure = dataBase + ".." + procedure; } //Set procedure to DBNAME..ProcedureName

          SqlCommand cmd1 = new SqlCommand(procedure, con);
          cmd1.CommandType = CommandType.StoredProcedure;

          foreach (SqlParameter p in parameters)
          {
              if (p != null)
              {
                  cmd1.Parameters.Add(p);
              }
          }

          con.Open();
          DataTable dt = new DataTable();
          SqlDataAdapter da = new SqlDataAdapter(cmd1);
          da.Fill(dt);
          con.Close();

          MessageBox.Show("Success"); //This should display the @text variable in my proc

          return dt;      
     }
     catch (Exception ex)
     {
        try
        {
           if (con.State == ConnectionState.Open)
           {
              con.Close();
           }
        }
        catch
        {
           MessageBox.Show("Could not connect to database. Check settings. " + ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        MessageBox.Show(ex.Message);
        return null;
      }      
    }

My Stored proc, Just focus on sections by all the prints, that’s the text i’m adding:

ALTER PROCEDURE [dbo].[AdminDevUserCreate]
    @SQLLoginName varchar(50),
    @SQLLoginPass varchar(50) 
AS


DECLARE @text NVARCHAR(1000)OUTPUT  

--PRINT 'Create SQL Login'
SET @text = 'Create SQL Login ' + @SQLLoginName 
-- USE [Master]

EXEC(' USE [master] CREATE LOGIN [' + @SQLLoginName + '] WITH PASSWORD=''' + @SQLLoginPass + ''', DEFAULT_DATABASE=[TestAudit], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF')

--PRINT 'Add Server Roles'
SET @text += + CHAR(13)+CHAR(10) + 'Add Server Roles'
--Add Server roles
    EXEC master..sp_addsrvrolemember @loginame = @SQLLoginName, @rolename = N'bulkadmin'
    EXEC master..sp_addsrvrolemember @loginame = @SQLLoginName, @rolename = N'processadmin'
    EXEC master..sp_addsrvrolemember @loginame = @SQLLoginName, @rolename = N'securityadmin'   
--PRINT 'Allow SQL Agent Job Manage'
SET @text += + CHAR(13)+CHAR(10) + 'Allow SQL Agent Job Manage'
--USE [MSDB]
EXEC ('msdb..sp_addrolemember ''SQLAgentOperatorRole'', ''' + @SQLLoginName + '''')

--PRINT 'Allow Trace'
SET @text += + CHAR(13)+CHAR(10) + 'Allow Trace'
--Allow trace (SQL Profiler)
--USE [MASTER]
EXEC (' USE [MASTER] GRANT ALTER TRACE TO ' + @SQLLoginName )

--PRINT 'Prevent admin proc changes '
SET @text += + CHAR(13)+CHAR(10) + 'Prevent admin proc changes '
    EXEC ('USE [TestAudit] DENY ALTER ON [TestAudit].[dbo].[Admin] TO ' + @SQLLoginName) --Prevents changes to Admin function
--PRINT 'Prevent database trigger changes'
SET @text += + CHAR(13)+CHAR(10) + 'Prevent database trigger changes'
    EXEC ('USE [TestAudit] DENY ALTER ANY DATABASE DDL TRIGGER TO ' + @SQLLoginName) --Prevents modify of [SchemaAuditTrigger]

PRINT @text
Select @text
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:57:08+00:00Added an answer on June 6, 2026 at 8:57 pm

    Your best bet is to use a output parameter.

    In your stored procedure add the parameter @text nvarchar(1000) OUTPUT then in your code add an extra parameter with the name @text and set the parameter direction to output.

    then just add the line SET @text = 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.' in your stored procedure

    Edit: My answer is if you don’t want this to affect your current query, if i misinterpreted your question please let me know. Also to get the value, after you execute the query you can get the value from the @name parameter using .Value

    Edit 2: Example Code Should look something like

    //Add these lines
    SqlParameter text = new SqlParameter("@name", SqlDbType.NVarChar);
    text.Direction = ParameterDirection.Output;
    cmd1.Parameters.Add(text);
    
    con.Open();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd1);
    da.Fill(dt);
    con.Close();
    
    //Change this line
    MessageBox.Show(text.Value); //This should display the @text variable in my proc
    

    if you need help with the stored procedure please post it and i’ll give a example with that too

    Edit 3: Quick example Tested with a quick example. The C# code:

            using (SqlConnection connection = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=TestDB;Integrated Security=True"))
            {
                connection.Open();
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "Test";
    
                    SqlParameter text = new SqlParameter("@Text", SqlDbType.NVarChar, 1000);
                    text.Direction = ParameterDirection.Output;
                    command.Parameters.Add(text);
    
                    using (DataTable dt = new DataTable())
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter(command))
                        {
                            da.Fill(dt);
                        }
                    }
    
                    Trace.WriteLine(text.Value);
    
                    connection.Close();
                }
            }
    

    The Stored Procedure:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE Test
        @Text Nvarchar(1000) OUTPUT
    AS
    BEGIN
        SET NOCOUNT ON;
    
        SET @Text = 'test'
    END
    GO
    

    Worked fine for me if you want to check it against yours for differences

    Edit 4: In your stored procedure the @text needs to be a parameter so instead of

    ALTER PROCEDURE [dbo].[AdminDevUserCreate]
        @SQLLoginName varchar(50),
        @SQLLoginPass varchar(50) 
    AS
    
    DECLARE @text NVARCHAR(1000)OUTPUT  
    

    make it

    ALTER PROCEDURE [dbo].[AdminDevUserCreate]
        @SQLLoginName varchar(50),
        @SQLLoginPass varchar(50),
        @text NVARCHAR(1000) OUTPUT 
    AS
    

    also when creating the SqlParameter use

    SqlParameter text = new SqlParameter("@Text", SqlDbType.NVarChar, 1000);
    

    which should get rid of the size issue as you are telling it that the parameter is NVARCHAR(1000)

    the line

    PRINT @text
    Select @text
    

    shouldn’t be needed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that has the parameter: @desk VARCHAR(50) I want to
I have this stored procedure that I want to use for my SAP crystal
I have a stored procedure that takes a user ID and calculates their balance
I have a stored procedure that I want to call from within another, and
I have a stored procedure that runs the following: 'ALTER LOGIN ' + @Login
I have a stored procedure that will return a list of ids. I want
I have a stored procedure that alters user data in a certain way. I
I have a stored procedure that is returning me some records. Now I want
I'm working with sql server 2008r2 developer. I have a stored procedure that adds
I have a stored procedure that updates my database. It runs great in query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.