1

I'm trying to write a SQL Server stored procedure that returns a list of string, then I will use this returned list in AjaxAutoCompleteExtender in my ASP.Net website, so when the user start typing a string (e.g. 60), the procedure should return the names of all the users where there ids' starts with the input string (601, 602, 605, ..etc).

I wrote the following T-SQL stored procedure:

CREATE PROCEDURE [dbo].[_GetUser_AutoComplete]
    @user_id int
AS
    IF ISNULL (CONVERT(nvarchar(50), @user_id), '') is not null
    BEGIN
         SELECT 
             [User].user_fname + ' ' + [User].user_mname + ' ' + [User].user_lname
         FROM 
             [User]
         WHERE 
             CONVERT(nvarchar(50), [user_id]) LIKE CONVERT(nvarchar(50), @user_id)
END

In my ASP.Net website, I have written code to call this procedure like this:

public List<string> getUser_AutoComplete(int? user_id)
{
        SqlParameter UserId = new SqlParameter("@user_id ", user_id);
        UserId.SqlDbType = SqlDbType.Int;
        UserId.Direction = ParameterDirection.Input;

        var res = this.Database.SqlQuery<string>("exec _GetUser_AutoComplete @user_id", UserId).ToList<string>();
        return res;
    }

And in my ASP.Net website, I have the following markup in my Default.aspx:

<asp:TextBox ID="TextBoxUserId" runat="server"></asp:TextBox>
<act:AutoCompleteExtender 
     ServiceMethod="SearchUsers"
     MinimumPrefixLength="2" CompletionInterval="100" 
     EnableCaching="false" CompletionSetCount="10"
     TargetControlID="TextBoxUserId"
     ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
</act:AutoCompleteExtender>

At the SearchUsers code is this:

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchUsers(string prefixText, int count)
{
    List<string> users = new List<string>();
    int userId;

    bool isInputUserId = Int32.TryParse(prefixText, out userId);

    if(isInputUserId)
    {
            users = getUser_AutoComplete(userId);
    }

    return users;
}

The problem is that there is not any autocompletion until the user types the whole id of the user he is searching for (i.e. if I type 60 in the textbox nothing happen if there is not a user with the id 60, If I write 601 the autoCompleteExtender works and return the full name user with the id 601).

I guess that the problem iis in my SQL Server procedure.

Any ideas, and thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
M_B216
  • 103
  • 9

1 Answers1

1

If I understand correctly your objective, you want to retrieve all the users where the their user_id starts with the same digits passed to the stored procedure as @user_id parameter.

If this is correct you need to add a wildcard to the end of the search text used with the LIKE operator

ALTER PROCEDURE [dbo].[_GetUser_AutoComplete]
@user_id int
AS
declare @usertext nvarchar(50)
select @usertext = CONVERT(nvarchar(50), @user_id)
IF ISNULL (@usertext, '') is not null
BEGIN
    SELECT [User].user_fname + ' ' + [User].user_mname + ' ' + [User].user_lname
    FROM [User]
    WHERE CONVERT(nvarchar(50), [user_id]) LIKE @usertext + '%'
END

Here, adding the wildcard '%' extends the results of the LIKE search including every user_id that starts with the same digits passed. For example, if you pass 1 then the result will include the users with user_id equals to 1, 10, 11, 100, 101, 199 and so on...

Steve
  • 213,761
  • 22
  • 232
  • 286