Listing of PostData.cs
using System; using System.Web; using System.Collections; using System.Collections.Generic; /// <summary> /// Class for retrieving post variables /// </summary> //----------------------------------------------------------------------- // Class which holds one individual post variable //----------------------------------------------------------------------- public class PostDataEntry { public string sqlName; // name used in sql query public int type; // 0 = integer public int update; // 1 means include in update public string qValue; // for sql queries public int iValue; // integer value private string _value; // string value, masked below //------------------------------------------------------------------- // set the three types of values //------------------------------------------------------------------- public string sValue { get { return _value; } set { _value = value; try { iValue = Convert.ToInt32(value); } catch { iValue = 0; } if (type > 0) qValue = "'" + Utility.encodeString(value) + "'"; else qValue = value; } } //------------------------------------------------------------------- // constructor //------------------------------------------------------------------- public PostDataEntry(int type, string name, string sqlName, string value) { this.type = type; this.sqlName = sqlName; this.update = 1; this.sValue = value; } } //----------------------------------------------------------------------- // Class for holding the sql variables //----------------------------------------------------------------------- public class PostData { private List<PostDataEntry> _pv = new List<PostDataEntry>(); private List<string> _name = new List<string>(); private HttpContext _context; //------------------------------------------------------------------- // constructor //------------------------------------------------------------------- public PostData(HttpContext context) { _context = context; } //------------------------------------------------------------------- // add a new element //------------------------------------------------------------------- public void Add(int type, string name) { init(type, name, name); } public void Add(int type, string name, string sqlName) { init(type, name, sqlName); } //------------------------------------------------------------------- // does the real work for the above two routines //------------------------------------------------------------------- private void init(int type, string name, string sqlName) { string value; try { value = _context.Request[name].ToString().Trim(); } catch { value = type > 0 ? "" : "0"; } _pv.Add(new PostDataEntry(type, name, sqlName, value)); _name.Add(name); } //------------------------------------------------------------------- // the default get is to return the SQL form of the variable // set transfers the string value //------------------------------------------------------------------- public string this[int i] { get { return _pv[i].qValue; } set { _pv[i].sValue = value; } } public string this[string name] { get { int i = _name.IndexOf(name); return _pv[i].qValue; } set { int i = _name.IndexOf(name); _pv[i].sValue = value; } } //------------------------------------------------------------------- // return the size of the array //------------------------------------------------------------------- public int Length { get { return _name.Count; } } //------------------------------------------------------------------- // returns the index of a given variable name //------------------------------------------------------------------- public int IndexOf(string name) { return _name.IndexOf(name); } //------------------------------------------------------------------- // sets the value of the update property //------------------------------------------------------------------- public void Update(string name, int value) { int i = _name.IndexOf(name); _pv[i].update = value; } //------------------------------------------------------------------- // get the value as a string (without any extra quotes) //------------------------------------------------------------------- public string Value(string name) { int i = _name.IndexOf(name); return _pv[i].sValue; } public string Value(int i) { return _pv[i].sValue; } //------------------------------------------------------------------- // return the name for a particular index //------------------------------------------------------------------- public string Name(int i) { return _name[i]; } //------------------------------------------------------------------- // The next three methods are used for generating sql. // Names return a comma separated list of names for an insert statement //------------------------------------------------------------------- public string Names() { string s = ""; for (int i = 0; i < _name.Count; i++) { if (_pv[i].update == 1) { if (s != "") s += ","; s += _pv[i].sqlName; } } return s; } //------------------------------------------------------------------- // Value return a comma separated list of values //------------------------------------------------------------------- public string Values() { string s = ""; for (int i = 0; i < _name.Count; i++) { if (_pv[i].update == 1) { if (s != "") s += ","; s += _pv[i].qValue; } } return s; } //------------------------------------------------------------------- // NameValues return a comma separated list of name = value //------------------------------------------------------------------- public string NameValues() { string s = ""; for (int i = 0; i < _name.Count; i++) { if (_pv[i].update == 1) { if (s != "") s += ","; s += NameValue(i); } } return s; } //------------------------------------------------------------------- // NameValue return a comma separated pair of name = value //------------------------------------------------------------------- public string NameValue(int i) { return _pv[i].sqlName + "=" + _pv[i].qValue; } }