Jes Sie

Jes Sie

  • 741
  • 1.2k
  • 281.3k

How to call generic handler with 2 parameters in webforms

Feb 26 2018 1:40 AM
Hello everybody, I created a general handler which will display names of students. This handler has 2 parameters namely: SchoolCode and term. The first parameter is loaded in my webform on page load. The 2nd parameter is when I type the name of the student in the textbox. Please refer to my code snippet below,
  1. public void ProcessRequest(HttpContext context)  
  2.         {  
  3.             string SchoolCode = context.Request["SchoolCode"] ?? "";  
  4.             string term = context.Request["term"] ?? "";  
  5.             List<string> studentName = new List<string>();  
  6.             using (SqlConnection con = DBCS.GetConString())  
  7.             {  
  8.                 SqlCommand cmd = new SqlCommand("spDropdownList_Student", con);  
  9.                 cmd.CommandType = CommandType.StoredProcedure;  
  10.                 cmd.Parameters.AddWithValue("@SchoolCode", SchoolCode);  
  11.                 cmd.Parameters.AddWithValue("@term", term);  
  12.                 con.Open();  
  13.                 SqlDataReader rdr = cmd.ExecuteReader();  
  14.                 while (rdr.Read())  
  15.                 {  
  16.                     studentName.Add(rdr["StudentDetails"].ToString());  
  17.                 }  
  18.             }  
  19.   
  20.             JavaScriptSerializer js = new JavaScriptSerializer();  
  21.             context.Response.Write(js.Serialize(studentName));  
  22.         }  
Below is my stored proc:
 
  1. ALTER PROCEDURE [dbo].[spDropdownList_Student]   
  2.     -- Add the parameters for the stored procedure here  
  3.     @SchoolCode nvarchar(50),  
  4.     @term nvarchar(50)  
  5. AS  
  6. BEGIN  
  7.     -- SET NOCOUNT ON added to prevent extra result sets from  
  8.     -- interfering with SELECT statements.  
  9.     SET NOCOUNT ON;  
  10.   
  11.     -- Insert statements for procedure here  
  12.     SELECT   
  13.             [Fname] + ' ' + [Lname] + ' - ' + [NickName] as StudentDetails   
  14.     FROM [dbo].[tblStudentMaster]   
  15.     WHERE [SchoolCode] = @SchoolCode AND  [Fname] LIKE @term + '%'  
  16.     ORDER BY [Fname] ASC  
  17. END  
 I tested my sp and it's working fine. Below also is the result:
 
 
Now, in my webforms, I made a jquery function to call the handler:
  1. $(document).ready(function () {  
  2.             $('#MasterFileContentSection_txtStudentName').autocomplete({  
  3.                 source: '/Handlers/StudentName.ashx'  
  4.             });  
  5.         });  
but it didn't display the names of students.  I'm new jquery.

Answers (2)