mary jean ligas

mary jean ligas

  • NA
  • 420
  • 196.7k

Please help me to convert php in c# using web method

Apr 13 2015 3:03 AM

Hi Guys i wanna try to convert php code below into c# code using web method functionality..i want to implement a search functionality like in apple that are having categories directly in an autocomplete text search please refer to this site http://www.apple.com/ (that is i wanted to implement).
i found an search demo on net but it uses php language..please help me how to convert it into web method.


USING PHP
calling method in php source
$.post("rpc.php", {queryString: ""+inputString+""}, function(data) { // Do an AJAX call
   $('#suggestions').fadeIn(); // Show the suggestions box
   $('#suggestions').html(data); // Fill the suggestions box
  }); 
------------------
<p id="searchresults">
<?php
 // PHP5 Implementation - uses MySQLi.
 // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
 $db = new mysqli('localhost', 'sa', 'admin1', 'search');
 
 if(!$db) {
  // Show error if we cannot connect.
  echo 'ERROR: Could not connect to the database.';
 } else {
  // Is there a posted query string?
  if(isset($_POST['queryString'])) {
   $queryString = $db->real_escape_string($_POST['queryString']);
   
   // Is the string length greater than 0?
   if(strlen($queryString) >0) {
    $query = $db->query("SELECT * FROM search s INNER JOIN categories c ON s.cat_id = c.cid WHERE name LIKE '%" . $queryString . "%' ORDER BY cat_id LIMIT 8");
    
    if($query) {
     // While there are results loop through them - fetching an Object.
     
     // Store the category id
     $catid = 0;
     while ($result = $query ->fetch_object()) {
      if($result->cat_id != $catid) { // check if the category changed
       echo '<span class="category">'.$result->cat_name.'</span>';
       $catid = $result->cat_id;
      }
             echo '<a href="'.$result->url.'">';
             echo '<img src="search_images/'.$result->img.'" alt="" />';
             
             $name = $result->name;
             if(strlen($name) > 35) {
              $name = substr($name, 0, 35) . "...";
             }             
             echo '<span class="searchheading">'.$name.'</span>';
             
             $description = $result->desc;
             if(strlen($description) > 80) {
              $description = substr($description, 0, 80) . "...";
             }
             
             echo '<span>'.$description.'</span></a>';
            }
            echo '<span class="seperator"><a href="http://www.marcofolio.net/sitemap.html" title="Sitemap">Nothing interesting here? Try the sitemap.</a></span><br class="break" />';
    } else {
     echo 'ERROR: There was a problem with the query.';
    }
   } else {
    // Dont do anything.
   } // There is a queryString.
  } else {
   echo 'There should be no direct access to this script!';
  }
 }
?>
</p>


USING WEB METHOD IN C#
calling method using json
$.ajax({
             type: "POST",
             contentType: "application/json; charset=utf-8",
             url: "Default.aspx/GetAutoCompleteData",
             data: "{'search':'" + inputString + "'}",
             //dataType: "json",
             dataType: "html",
             success: function(data) {
                 $('#suggestions').fadeIn(); // Show the suggestions box
                 $('#suggestions').html(data); // Fill the suggestions box
                 //response(data.d);
             },
             error: function(result) {
                 alert("Error");
             }
         });
------------------
[WebMethod]
 public static List<string> GetAutoCompleteData(string search)
 {
  List<string> result = new List<string>();
  String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["searchConnectionString"].ConnectionString;
  SqlConnection con = new SqlConnection(strConnString);
  
  using (con)
  {
   using (SqlCommand cmd = new SqlCommand("SELECT * FROM search_content s INNER JOIN categories c ON s.cat_id = c.cid WHERE name LIKE '%'+@SearchText+'%' ORDER BY cat_id", con))
   {
    con.Open();
    cmd.Parameters.AddWithValue("@SearchText", search);
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
     result.Add("<span class="+"\"category\""+">"+ dr["cat_name"].ToString()+"</span>");
    }
    return result;
   }
  }
 }


Answers (4)