Advertisement
Guest User

Untitled

a guest
Jan 6th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. -------- HTML CODE --------
  2.  
  3.         <form action="/include/api.ashx" method="post">
  4.             <div class="large-6 columns">
  5.                 <input type="text" value="" id="PLUID"/>
  6.             </div>
  7.             <div class="large-6 columns left">
  8.                 <input type="text" value="" id="Enabled"/>
  9.                 <input type="text" value="" id="WeighingMode" />
  10.                 <input type="text" value="" id="TemplateID" />
  11.             </div>
  12.             <div class="large-6 columns">
  13.                 <textarea id="PLUName"></textarea>
  14.             </div>
  15.             <input type="submit" name="name" value="Save" />
  16.         </form>
  17.  
  18. -------- C# --------
  19. namespace Project.include
  20. {
  21.     public class API : IHttpHandler
  22.     {
  23.         public void ProcessRequest(HttpContext context)
  24.        {
  25.             object data = LoadAPI(context);
  26.             string jsonData = JsonConvert.SerializeObject(data);
  27.             context.Response.Write(jsonData);
  28.         }
  29.  
  30.         public List<Dictionary<string, object>> LoadAPI(HttpContext context)
  31.         {
  32.             List<Dictionary<string, object>> output = new List<Dictionary<string, object>> { };
  33.  
  34.             SqlConnection sqlConnection1 = new SqlConnection(globalLibrary.NewConnectionString);
  35.             SqlCommand cmd = new SqlCommand();
  36.             SqlDataReader reader;
  37.             string tablename = context.Request.QueryString["tablename"];
  38.             string maybeID = context.Request.QueryString["ID"];
  39.             string TableField = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.QueryString[tablename + "Name"]);
  40.             if (tablename != null)
  41.             {
  42.                 cmd.CommandText = "SELECT TOP 50 * FROM " + tablename;
  43.             }
  44.             if (maybeID != null && Regex.IsMatch(maybeID, @"^\d+$") == true)
  45.             {
  46.                 cmd.CommandText += " WHERE " + tablename + "ID Like '%" + maybeID + "%'";
  47.             }
  48.             else if (TableField != null)
  49.             {
  50.                 cmd.CommandText += " WHERE " + tablename + "Name LIKE '%" + TableField.Replace("'", "''") + "%'";
  51.             }
  52.  
  53.             cmd.CommandType = CommandType.Text;
  54.             cmd.Connection = sqlConnection1;
  55.  
  56.             sqlConnection1.Open();
  57.  
  58.             reader = cmd.ExecuteReader();
  59.  
  60.             while (reader.Read())
  61.             {
  62.                 object[] rowValues = new object[reader.FieldCount];
  63.                 var headings = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToArray();
  64.                 Dictionary<string, object> rowDict = new Dictionary<string, object> { };
  65.                 reader.GetValues(rowValues);
  66.                 //list.Add(headings);
  67.                 //list.Add(rowValues);
  68.  
  69.                 for (int i = 0; i < Math.Max(headings.Length, rowValues.Length); i++)
  70.                 {
  71.                     var key = headings[i];
  72.                     var value = rowValues[i];
  73.                     rowDict.Add(key, value);
  74.                 }              
  75.                 output.Add(rowDict);
  76.                 //context.Response.Write(test);
  77.             }
  78.             sqlConnection1.Close();
  79.             return output;
  80.         }
  81.         public bool IsReusable
  82.         {
  83.             get
  84.             {
  85.                 return false;
  86.             }
  87.         }
  88.  
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement