示例#1
0
 public object this[int idx, Flds fld]
 {
     get
     {
         return(xTbl.Rows[idx][xListOrder[fld]]);
     }
     set
     {
         xTbl.Rows[idx][xListOrder[fld]]            = value;
         xTbl.Rows[idx][xListFlds[intFlds.Updated]] = true;
     }
 }
示例#2
0
        public xList(string xListName      = ""
                     , bool autoSave       = false
                     , bool autoCreate     = false
                     , string initTitle    = ""
                     , string[] initValues = null
                     , Flds orderBy        = Flds.Idx
                     , string dbName       = "Common"
                     , string andWhere     = ""
                     )
        {
            xAutoSave = autoSave;

            initStuff();
            DataRow xRow;

            SQLCxn = new SqlConnection(defns.cxn(dbName));

            SQLCxn.Open();
            if (xListName != "")
            {
                sqlTxt = "Select ListIdx,EntryParm from xList where ListType=0 and EntryDescr=@xLN;";
                SQLCmd = new SqlCommand(sqlTxt, SQLCxn);
                SQLCmd.Parameters.AddWithValue("@xLN", xListName);
                SQLRdr = SQLCmd.ExecuteReader();
                if (SQLRdr.Read())
                {
                    xlType   = (int)SQLRdr[0];
                    xSubList = SQLRdr[1].ToString();
                    SQLRdr.Close();
                }
                else   // auto-create here if requested...
                {
                    SQLRdr.Close();
                    if (autoCreate)
                    {
                        int maxIdx = 0;
                        sqlTxt = "Select Coalesce(Max(ListIdx),0) as MaxIdx from dbo.xList where ListType=0;";
                        SQLCmd = new SqlCommand(sqlTxt, SQLCxn);
                        //try {
                        //Int32.TryParse(SQLCmd.ExecuteScalar().ToString(), out maxIdx);
                        //maxIdx++;
                        maxIdx = (int)SQLCmd.ExecuteScalar() + 1;
                        //}
                        //catch (Exception exc) {
                        //    string bert = exc.GetType().ToString() + "-" + exc.Message;
                        //}
                        sqlTxt = "Insert xList (ListType,ListIdx,EntryDescr) values(0,@XI,@XD);";
                        SQLCmd = new SqlCommand(sqlTxt, SQLCxn);
                        SQLCmd.Parameters.AddWithValue("@XI", maxIdx);
                        SQLCmd.Parameters.AddWithValue("@XD", xListName);
                        if (Convert.ToInt32(SQLCmd.ExecuteNonQuery()) > 0)
                        {
                            xlType = maxIdx;
                            if (initTitle != "")
                            {
                                sqlTxt = "Insert xList (ListType,ListIdx,EntryDescr) values(@XT,@XI,@XD);";
                                SQLCmd = new SqlCommand(sqlTxt, SQLCxn);
                                SQLCmd.Parameters.AddWithValue("@XT", maxIdx);
                                SQLCmd.Parameters.AddWithValue("@XI", 0);
                                SQLCmd.Parameters.AddWithValue("@XD", initTitle);
                                if (Convert.ToInt32(SQLCmd.ExecuteNonQuery()) == 0)
                                {
                                    string bert = "was unable to add a new row...";
                                    bert += "to xlist during autocreate";
                                }
                            }
                            if (!initValues.Equals(null))   // && initValues.Length > 0) {
                            {
                                int Ctr = 1;
                                foreach (string newDesc in initValues)
                                {
                                    sqlTxt = "Insert xList (ListType,ListIdx,EntryDescr) values(@XT,@XI,@XD);";
                                    SQLCmd = new SqlCommand(sqlTxt, SQLCxn);
                                    SQLCmd.Parameters.AddWithValue("@XT", maxIdx);
                                    SQLCmd.Parameters.AddWithValue("@XI", Ctr++);
                                    SQLCmd.Parameters.AddWithValue("@XD", newDesc);
                                    if (Convert.ToInt32(SQLCmd.ExecuteNonQuery()) == 0)
                                    {
                                        string bert = "was unable to add a new row...";
                                        bert += "to xlist during autocreate";
                                        MessageBox.Show(bert, "Uh, HELP! - Don't know what to do!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    }
                                }
                            }
                        }
                    }
                }
                // SQLRdr.Close(); // needs to be above as it needs to be at the beginning of the 'else' clause as the cxn is reused!
            }
            if (xSubList != "")
            {
                SQLCmd = new SqlCommand("Select Count(Idx) from xList where ListType=0 and EntryDescr=@ed;", SQLCxn);
                SQLCmd.Parameters.AddWithValue("@ed", xSubList);
                if (Convert.ToInt32(SQLCmd.ExecuteScalar()) == 0)
                {
                    xSubList = "";
                }
            }
            sqlTxt = "Select * from xList as tblPri where ListType=@xLT";
            if (andWhere != "")
            {
                sqlTxt += " and " + andWhere;
            }
            sqlTxt += " Order By " + xListOrder[orderBy];
            sqlTxt += ";";
            SQLCmd  = new SqlCommand(sqlTxt, SQLCxn);
            SQLCmd.Parameters.AddWithValue("@xLT", xlType);
            SQLRdr = SQLCmd.ExecuteReader();
            int Lp1 = 0;

            while (SQLRdr.Read())
            {
                // old list style
                int idx = Convert.ToInt32(SQLRdr["ListIdx"]); // can NEVER be null
                if (xTitle == "" && idx != 0)
                {
                    xTitle             = (xListName == "" ? "xList Index" : xListName);// adGrp.LeftBrkt + (xListName == "" ? "xList Index" : xListName) + adGrp.RghtBrkt;
                    xRow               = xTbl.NewRow();
                    xRow["ListIdx"]    = 0;
                    xRow["EntryDescr"] = xTitle;
                    xTbl.Rows.Add(xRow);
                    //xTbl.Rows[0]["EntryDescr"] = Autumn.LeftBrkt + SQLRdr["EntryDescr"].ToString() + Autumn.RghtBrkt;
                }
                //else
                {
                    xIdx.Add(idx);
                    string txt = SQLRdr["EntryDescr"].ToString();
                    if (idx == 0)
                    {
                        xTitle = txt;
                    }                // adGrp.LeftBrkt + txt + adGrp.RghtBrkt; }
                    xDescr.Add(txt); // used in the inumerator...
                    //   xParm.Add(SQLRdr["EntryParm"] is DBNull ? "" : SQLRdr["EntryParm"].ToString());
                    //    xValue.Add(SQLRdr["EntryValue"] is DBNull ? 0 : Convert.ToInt32(SQLRdr["EntryValue"]));
                    //xWhen.Add(SQLRdr["EntryWhen"] is DBNull ? DateTime.MinValue : Convert.ToDateTime(SQLRdr["EntryWhen"]));
                    //xFlag.Add(SQLRdr["EntryFlag"] is DBNull ? (sbyte)0 : Convert.ToSByte(SQLRdr["EntryFlag"]));
                    // xUpdtd.Add(false);
                    xlCount++;

                    // new dataset/datatable style
                    xRow               = xTbl.NewRow();
                    xRow["Idx"]        = SQLRdr["Idx"];
                    xRow["ListIdx"]    = SQLRdr["ListIdx"];
                    xRow["EntryDescr"] = SQLRdr["EntryDescr"];
                    xRow["EntryParm"]  = SQLRdr["EntryParm"];
                    xRow["EntryValue"] = SQLRdr["EntryValue"];
                    xRow["EntryWhen"]  = SQLRdr["EntryWhen"];
                    xRow["EntryFlag"]  = SQLRdr["EntryFlag"];
                    xRow["Updated"]    = false;
                    xTbl.Rows.Add(xRow);
                    intIdx.Add(Convert.ToInt32(SQLRdr["ListIdx"]), Lp1++);
                }
            }
            SQLRdr.Close();

            sqlTxt = "Select Max(ListIdx) from xList as tblPri where ListType=@xLT";
            SQLCmd = new SqlCommand(sqlTxt, SQLCxn);
            SQLCmd.Parameters.AddWithValue("@xLT", xlType);
            xlMaxIdx = Convert.ToInt32(SQLCmd.ExecuteScalar());

            SQLCxn.Close();
            if (xTitle == "")
            {
                xTitle             = xListName; // adGrp.LeftBrkt + xListName + adGrp.RghtBrkt;
                xRow               = xTbl.NewRow();
                xRow["ListIdx"]    = 0;
                xRow["EntryDescr"] = xListName;
                xTbl.Rows.Add(xRow);
            }
        }