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 6081183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:08:48+00:00 2026-05-23T11:08:48+00:00

I was put in a situation where I was forced to create a procedure

  • 0

I was put in a situation where I was forced to create a procedure that uses two data tables such as this:

ALTER PROCEDURE [sesuser].[Login_GetUserList]
(
    @beginsWith NVARCHAR(20) = NULL,
    @SortBy nvarchar(20) = NULL,
    @sortByDirection nvarchar(5) = N'ASC'
)
AS
BEGIN

    DECLARE @searchStr NVARCHAR(21)

    IF @beginsWith IS NULL
        SET @beginsWith = N''
    ELSE
        SET @beginsWith = dbo.SuperTrim(@beginsWith);

    IF LEN(@beginsWith) > 0
        SET @searchStr = @beginsWith + N'%'
    ELSE
        SET @searchStr = N'%'

DECLARE @sqlSTR NVARCHAR(4000)
DECLARE @sqlOffice NVARCHAR(100)
DECLARE @sqlOfficeID NVARCHAR(10)
DECLARE @sqlEndSTR NVARCHAR(4000)

SET @sqlSTR = N' SELECT 
        [SESLoginID] AS LoginID
        ,[SESSuspended] AS LoginSuspended
        ,[SESAdmin] AS Role_Admin
        ,[SESLoginID]
        ,[SESFirstName]
        ,[SESLastName]
        ,[SESEmail]
        ,[SESSuspended]
        FROM sesuser.SESLogin
        WHERE SESFirstName LIKE ''' + @searchStr + ''''

SET @sqlOfficeID = N' SELECT 
        [SESLoginID]
        FROM sesuser.SESLogin
        WHERE SESFirstName LIKE ''' + @searchStr + ''''

SET @sqlOffice = N' SELECT
        [OfficeName]
        FROM sesuser.Office
        WHERE OfficeID LIKE ''' + @sqlOfficeID + ''''

SET @sqlEndSTR = @sqlSTR + @sqlOffice

PRINT @sqlEndSTR

EXEC sp_ExecuteSQL @sqlEndSTR

END

so as I understand it, this code supposed to in a table of office IDs and equivalent Office Name to replace the office ID with a Name in the other table and return it.

I then use the retrieve data string in a method in my c# code:

public static DataTable GetUserList(string beginsWith, out string errMessage, string sortBy, string sortByDirection)
{
   DataTable dt = null;
   int errNum = 0;
   string sql = "[sesuser].[Login_GetUserList]";
   SqlDataAdapter adap = null;

   SqlParameter param = null;

   errMessage = "";
   SqlConnection conn = Functions.Database.DBConnect(out errNum);

   try
   {
      SqlCommand cmd = new SqlCommand(sql);
      cmd.Connection = conn;
      cmd.CommandType = CommandType.StoredProcedure;

      param = new SqlParameter("@beginsWith", SqlDbType.NVarChar, 40);
      param.Value = string.IsNullOrEmpty(beginsWith) ? "" : beginsWith.Trim();
      cmd.Parameters.Add(param);
      cmd.Parameters.AddWithValue("@SortBy", sortBy);
      cmd.Parameters.AddWithValue("@sortByDirection", sortByDirection);

      adap = new SqlDataAdapter(cmd);

      dt = new DataTable();
      adap.Fill(dt);
   }
   catch (Exception ex)
   {
      errMessage = ex.Message;
   }
   finally
   {
      Functions.Database.DBClose(conn);
   }

   return dt;
}

and later in my asp.net code I call back office name

<asp:BoundField DataField="OfficeName" HeaderText="Business Unit" ReadOnly="True"  />

The problem is, it errors, saying that it cant find OfficeName.

Is there something I forgot? Or am I doing it wrong?

  • 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-05-23T11:08:49+00:00Added an answer on May 23, 2026 at 11:08 am

    Looks like your stored procedure return two subsets.

    Amend your stored procedure like so:

    ALTER PROCEDURE [sesuser].[Login_GetUserList]
    (   @beginsWith NVARCHAR(20) = NULL,
        @SortBy nvarchar(20) = NULL,
        @sortByDirection nvarchar(5) = N'ASC')
    AS
    BEGIN    
        DECLARE @searchStr NVARCHAR(21)    
        IF @beginsWith IS NULL
            SET @beginsWith = N''
        ELSE
            SET @beginsWith = dbo.SuperTrim(@beginsWith);    
        IF LEN(@beginsWith) > 0
            SET @searchStr = @beginsWith + N'%'
        ELSE
            SET @searchStr = N'%'    
    DECLARE @sqlSTR NVARCHAR(4000)    
    SET @sqlSTR = N' SELECT 
             [OfficeName]
            ,[SESLoginID] AS LoginID
            ,[SESSuspended] AS LoginSuspended
            ,[SESAdmin] AS Role_Admin
            ,[SESLoginID]
            ,[SESFirstName]
            ,[SESLastName]
            ,[SESEmail]
            ,[SESSuspended]
            FROM sesuser.SESLogin
            INNER JOIN sesuser.Office
            ON sesuser.Office.OfficeID = sesuser.SESLogin.SESLoginID
            WHERE SESFirstName LIKE ''' + @searchStr + ''''    
    PRINT @sqlSTR    
    EXEC sp_ExecuteSQL @sqlSTR    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imaginary Situation: You’ve used mysqldump to create a backup of a mysql database. This
I have a difficult situation, let me put it this way, I want the
So this is the situation: I have 2 lists and want to put them
My current situation is: I have to read a file and put the contents
Put it another way: what code have you written that cannot fail. I'm interested
We put all of our unit tests in their own projects. We find that
Simply put, is there a way to create a 2D javascript array using similar
We put common prefixes on related tables to assure they display next to each
To put it simple, there's a simple java swing app that consists of JFrame
At some point in our lives we're put in the situation to learn a

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.