Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------- HTML CODE --------
- <form action="/include/api.ashx" method="post">
- <div class="large-6 columns">
- <input type="text" value="" id="PLUID"/>
- </div>
- <div class="large-6 columns left">
- <input type="text" value="" id="Enabled"/>
- <input type="text" value="" id="WeighingMode" />
- <input type="text" value="" id="TemplateID" />
- </div>
- <div class="large-6 columns">
- <textarea id="PLUName"></textarea>
- </div>
- <input type="submit" name="name" value="Save" />
- </form>
- -------- C# --------
- namespace Project.include
- {
- public class API : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- object data = LoadAPI(context);
- string jsonData = JsonConvert.SerializeObject(data);
- context.Response.Write(jsonData);
- }
- public List<Dictionary<string, object>> LoadAPI(HttpContext context)
- {
- List<Dictionary<string, object>> output = new List<Dictionary<string, object>> { };
- SqlConnection sqlConnection1 = new SqlConnection(globalLibrary.NewConnectionString);
- SqlCommand cmd = new SqlCommand();
- SqlDataReader reader;
- string tablename = context.Request.QueryString["tablename"];
- string maybeID = context.Request.QueryString["ID"];
- string TableField = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.QueryString[tablename + "Name"]);
- if (tablename != null)
- {
- cmd.CommandText = "SELECT TOP 50 * FROM " + tablename;
- }
- if (maybeID != null && Regex.IsMatch(maybeID, @"^\d+$") == true)
- {
- cmd.CommandText += " WHERE " + tablename + "ID Like '%" + maybeID + "%'";
- }
- else if (TableField != null)
- {
- cmd.CommandText += " WHERE " + tablename + "Name LIKE '%" + TableField.Replace("'", "''") + "%'";
- }
- cmd.CommandType = CommandType.Text;
- cmd.Connection = sqlConnection1;
- sqlConnection1.Open();
- reader = cmd.ExecuteReader();
- while (reader.Read())
- {
- object[] rowValues = new object[reader.FieldCount];
- var headings = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToArray();
- Dictionary<string, object> rowDict = new Dictionary<string, object> { };
- reader.GetValues(rowValues);
- //list.Add(headings);
- //list.Add(rowValues);
- for (int i = 0; i < Math.Max(headings.Length, rowValues.Length); i++)
- {
- var key = headings[i];
- var value = rowValues[i];
- rowDict.Add(key, value);
- }
- output.Add(rowDict);
- //context.Response.Write(test);
- }
- sqlConnection1.Close();
- return output;
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement