0

I am using jquery autocomplete and I am getting data from a generic handler that makes a call to database.

Java Script:

<script type="text/javascript">
    $(document).ready(function () {
        $('#managerNo').autocomplete({
            source: 'ManagerHandler1.ashx'
        });
    });

</script>

Generic Handler: (ManagerHandler1.ashx)

public void ProcessRequest(HttpContext context)
    {
        string term = context.Request["term"] ?? "";
        List<string> listManagerNames = new List<string>();

        string myConnection = ConfigurationManager.ConnectionStrings["ArtistManagementSystem"].ConnectionString;
        using(SqlConnection conn = new SqlConnection(myConnection))
        {
            SqlCommand cmd = new SqlCommand("spGetManagerNames",conn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName = "@term",
                Value = term
            });
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                listManagerNames.Add(dr["Cell"].ToString());
            }
        }

        JavaScriptSerializer js = new JavaScriptSerializer();
        context.Response.Write(js.Serialize(listManagerNames));

    }

Actually I am trying to load numbers using autocomplete and that is working just fine. What I want to do is, Load numbers where ID = someid.

I am getting UserID from Session on Page_Load() function.

Page_Load()

if (Session["UserID"] != null)
        {
            USERid = Session["UserID"].ToString();
        }

I have changed the stored procedure for that and its also working fine.

StoredProcedure:

CREATE proc spGetManagerNames 
@term varchar(50) = NULL,
@UsrID bigint 
as
Begin
    Select Cell
    from Manager
    where Cell like @term + '%' and [User ID]=@UsrID
End

Now since I am getting the User ID from Session, so I want to pass that to the handler, how can I do that? Kindly help me out.

el323
  • 2,760
  • 10
  • 45
  • 80
  • `since I am getting the User ID from Session` - where are you doing this, it's not evident from your code - there's no reference to `context.Session["UserID"]` in the code whatsoever – Jaromanda X Oct 30 '16 at 01:40
  • OK, so you have `USERid` - just add that to the cmd.Parameters just like you did for the `term` parameter ... wait, does an `.ashx` page even have a `Page_Load()` method? – Jaromanda X Oct 30 '16 at 01:49
  • No it doesn't have a Page_Load(). How am I supposed to get the Session value in my Generic Handler class? – el323 Oct 30 '16 at 01:53
  • 1
    so use `context.Session["UserID"]` in `ProcessRequest` like I already suggested - what confused me was the statement that you were getting the userid, I assumed you meant in the handler - so, you actually are not getting the userid in the handler - which is the opposite – Jaromanda X Oct 30 '16 at 01:53
  • 1
    You need to implement the `IRequiresSessionState` interface to access session variables in a Generic Handler. See http://stackoverflow.com/questions/4889437/asp-net-generic-handlers-session – VDWWD Oct 30 '16 at 01:59
  • Oh yeah, that too – Jaromanda X Oct 30 '16 at 02:02
  • Thanks a lot. Its working now. – el323 Oct 30 '16 at 02:09

0 Answers0