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