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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:42:46+00:00 2026-06-08T23:42:46+00:00

I currently have a query string for the jQuery Autocomplete plug in but should

  • 0

I currently have a query string for the jQuery Autocomplete plug in but should be using a stored procedure instead. Can anyone help me convert? It seems to not be working when I do it.

Original ASHX

public class Search_CS : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    string prefixText = context.Request.QueryString["q"];
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["Rollup2ConnectionString"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            //cmd.CommandText = "select NUID from T_USER where " +
            //"NUID like @SearchText + '%'";
            cmd.CommandText = "select rtrim(NUID) NUID, rtrim(FNAME) FNAME, rtrim(LNAME) LNAME from T_USER where NUID like @SearchText + '%' OR FNAME like @SearchText + '%' OR LNAME like @SearchText + '%'"; 
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            StringBuilder sb = new StringBuilder(); 
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    sb.Append(sdr["NUID"].ToString() + " ").Append(sdr["FNAME"].ToString() + " ").Append(sdr["LNAME"].ToString() + " ")
                        .Append(Environment.NewLine); 
                }
            }
            conn.Close();
            context.Response.Write(sb.ToString()); 
        }
    }
}

New ASHX for stored procedure:

public class Search_CS : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    string prefixText = context.Request.QueryString["q"];
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["Rollup2ConnectionString"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            //cmd.CommandText = "select NUID from T_USER where " +
            //"NUID like @SearchText + '%'";
            cmd.CommandText = "SP_AUTOCOMPLETE"; 
            cmd.Parameters.AddWithValue("@SearchText", prefixText);

            cmd.Parameters.Add(new SqlParameter("@SearchText", SqlDbType.VarChar));
            cmd.Parameters["@SearchText"].Value = prefixText;


            cmd.Connection = conn;
            StringBuilder sb = new StringBuilder(); 
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    sb.Append(sdr["NUID"].ToString() + " ").Append(sdr["FNAME"].ToString() + " ").Append(sdr["LNAME"].ToString() + " ")
                        .Append(Environment.NewLine); 
                }
            }
            conn.Close();
            context.Response.Write(sb.ToString()); 
        }
    }
}

Stored procedure:

@SearchText VARCHAR(255)
AS
BEGIN

    SET NOCOUNT ON;
    SELECT RTRIM(NUID) NUID, RTRIM(FNAME) FNAME, RTRIM(LNAME) LNAME 
    FROM T_USER 
    WHERE NUID like @SearchText + '%' OR FNAME like @SearchText + '%' OR LNAME like @SearchText + '%'

Thanks!

  • 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-08T23:42:48+00:00Added an answer on June 8, 2026 at 11:42 pm

    You need to set the SqlCommand ‘CommandType’ to ‘CommandType.StoredProcedure’.

    cmd.CommandType = CommandType.StoredProcedure;
    

    I would also recommend using a prefix other than ‘sp_’. That is what Microsoft used for their system procedures and you might accidentally overwrite one you want to keep around. 🙂

    This is how I generate parameters:

    public static SqlParameter GetParameter(string parameterName, object value, SqlDbType type, int size)
    {
        if (value == null)
        {
            value = DBNull.Value;
        }
    
        if (size <= 0 && type == SqlDbType.VarChar)
        {
            switch (type)
            {
                case SqlDbType.VarChar:
                    size = 8000;
                    break;
                case SqlDbType.NVarChar:
                    size = 4000;
                    break;
            }
        }
    
        SqlParameter parameter = new SqlParameter(parameterName, type, size);
        parameter.Value = value;
        parameter.IsNullable = true;
    
        return parameter;
    }
    

    And I just do this.

    cmd.Parameters.Add(GetParameter("@SearchText", searchText, SqlDbType.VarChar));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the jQuery Countdown plugin and have a quick query. My code currently
Looking to do a bit of refactoring... Using NHibernate I have this query currently
I have a WCF service that accepts requests from JQuery. Currently, I can access
I have an jQuery ajax database query and Im wondering how I can maybe
I am creating dynamic controls using jQuery by reading the query string parameter. These
I am using jQuery Autocomplete, not jQueryUI, and currently it will not show more
If I have a query like this: String Category = HttpContext.Current.Request.QueryString[Product].ToString(); IQueryable<ItemFile> pressReleases =
I currently have a query that looks like this: SELECT NON EMPTY ([Measures].[TOTAL]) ON
I currently have a query similar to: SELECT users.fname, users.lname, sales.date FROM users LEFT
I currently have a SQL query that returns a number of fields. I need

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.