Populate checkboxes depending on child node picked from treeview - c#

I have a TreeView and on a separate panel 3 checkboxes.
The Treeview data is being pulled from a database,this database has two tables Product and Model inside the Model table there are 3 columns - twoDoor, threeDoor and fiveDoor.
My TreeView looks like this;
CAR(PRODUCT)
->FORD(MODEL)
->BMW(MODEL)
->VW(MODEL)
On a separate panel there are 3 checkboxes;
Two Door
Three Door
Five Door
The state of these checkboxes are already defined in the Model db.
How can i display their state against whatever child Node(Model) is picked in the TreeView?
My Code:
private void Form1_Options_Load(object sender, EventArgs e)
{
String connectionString;
connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
String Sequel = "SELECT id,ProductName,ModelType FROM Product";
SqlDataAdapter da = new SqlDataAdapter(Sequel, conn);
DataTable dt = new DataTable();
conn.Open();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
parentNode = treeView1.Nodes.Add(dr["ProductName"].ToString());
//treeView1.Nodes.Add(dr["ModelType"].ToString());
PopulateTreeView(Convert.ToInt32(dr["Id"].ToString()), parentNode);
}
}
public void PopulateTreeView(int parentId, TreeNode parentNode)
{
String Seqchildc = "Select * From Model WHERE ProductId = "+ parentId + "";
SqlDataAdapter dachildmnuc = new SqlDataAdapter(Seqchildc, conn);
DataTable dtchildc = new DataTable();
dachildmnuc.Fill(dtchildc);
TreeNode childNode;
foreach (DataRow dr in dtchildc.Rows)
{
if (parentNode == null)
childNode = treeView1.Nodes.Add(dr["ModelName"].ToString());
//childNode.Nodes.Add(dr["ModelType"].ToString());
else
childNode = parentNode.Nodes.Add(dr["ModelName"].ToString());
//PopulateTreeView(Convert.ToInt32(dr["Id"].ToString()), childNode);
}
treeView1.ExpandAll();
//Connect();
}

Here is what you can do if I understand correctly the question.
First, create a class to hold your Model information:
class ModelInfo
{
public string Name { get; set; }
public bool TwoDoor { get; set; }
public bool ThreeDoor { get; set; }
public bool FiveDoor { get; set; }
}
Second, modify your PopulateTreeView method as follows:
public void PopulateTreeView(int parentId, TreeNode parentNode)
{
// ...
foreach (DataRow dr in dtchildc.Rows)
{
// Populate model info from the data
var modelInfo = new ModelInfo { Name = dr["ModelName"].ToString() };
//modelInfo.TwoDoor = dr[...];
//modelInfo.ThreeDoor = dr[...];
//modelInfo.FiveDoor = dr[...];
// Create and add a new node
var childNode = (parentNode == null ? treeView1.Nodes : parentNode.Nodes).Add(modelInfo.Name);
// Associate info with the node
childNode.Tag = modelInfo;
}
// ...
}
The essential part is to store the ModelInfo inside the Node.Tag property so it can be retrieved later.
Finally, subscribe to the TreeView.AfterSelect event and put inside the event handler something like this:
// Get the model info from the selected node
var modelInfo = e.Node != null ? e.Node.Tag as ModelInfo : null;
// Update the checkboxes accordingly
chkTwoDoor.Checked = modelInfo != null && modelInfo.TwoDoor;
chkThreeDoor.Checked = modelInfo != null && modelInfo.ThreeDoor;
chkFiveDoor.Checked = modelInfo != null && modelInfo.FiveDoor;
for the two-state checkbox style, or alternatively
chkTwoDoor.CheckState = modelInfo == null ? CheckState.Indeterminate : modelInfo.TwoDoor ? CheckState.Checked : CheckState.Unchecked;
chkThreeDoor.CheckState = modelInfo == null ? CheckState.Indeterminate : modelInfo.ThreeDoor ? CheckState.Checked : CheckState.Unchecked;
chkFiveDoor.CheckState = modelInfo == null ? CheckState.Indeterminate : modelInfo.FiveDoor ? CheckState.Checked : CheckState.Unchecked;
for the tri-state checkbox style.

Related

Create Treeview depending on DataGridView in C#

I get data from tow database, db1,db2, then Now I want to create treeView for the data in the resulting datagridview; in datagrid view there are: id, name, director,the first record is the prim director, that mean he has not up director(he is owner), each record has no other record or has more records(child), and each child has grandchild and so on, this scenario Just Like in the this page:
I want to create treeview (parent and child and grandchild and so on), depending on xml file
when i used this snippet after some :
void setTree()
{
{
foreach(DataGridViewRow dt in DataGridView1.Rows)
{
var per = this.DataGridView1.Rows.Cast<DataGridViewRow>().Select(n => new person
{
name = dt.Cells[0].Value.ToString(),
Sex = dt.Cells[1].Value.ToString(),
Status = dt.Cells[2].Value.ToString(),
child = dt.Cells[3].Value.ToString(),
id = dt.Cells[4].Value.ToString(),
father = dt.Cells[5].Value.ToString()
}).ToList();
var rootTreeNode = GetTree(per, "").First();.........(1)
treeView1.Nodes.Add(rootTreeNode);
}
}
}
private TreeNode[] GetTree(List<person> per, string parent)
{
return per.Where(p => p.father == parent).Select(p =>
{
var node = new TreeNode(p.name);
node.Tag = p.id;
node.Nodes.AddRange(GetTree(per, p.id));
return node;
}).ToArray();
}
Now, when I use this code, I get error at mark(1),it say:Additional information: Sequence contains no elements.
thank you
after several readings in internet and attempts to solve this small problem, I successed finally.
this is the solution:
{
.............
TreeNode tn = new TreeNode(this.DataGridView2.Rows[0].Cells[0].Value.ToString());//text
tn.Tag = this.DataGridView2.Rows[0].Cells[4].Value.ToString();// id
tn.Name = this.DataGridView2.Rows[0].Cells[5].Value.ToString();//directorid
treeView1.Nodes.Add(tn);
settree(tn);
}
public void settree(TreeNode ns)
{
foreach (DataGridViewRow dr in DataGridView2.Rows)
{
if (dr.Cells[5].Value.ToString() == ns.Tag.ToString())
{
TreeNode tsn = new TreeNode(dr.Cells[0].Value.ToString());
tsn.Tag = dr.Cells[4].Value.ToString();
tsn.Name = dr.Cells[5].Value.ToString();
ns.Nodes.Add(tsn);
settree(tsn);
}
}
}
i will be happy if you benefit from this code.

Binding data to treeView c#

I wish my program worked like this:
1.It takes data form MS SQL
2.Makes tree based on this data(data rows)
3.I have few textBoxex and I want to fill them with data based on clicked node.
I`ve made class for SQL connection:
class Baza
{
private SqlConnection connection;
string dbdir = "Data Source=CS24\\SQLEXPRESS;user id=sa;password=alamakota;database=SHARP;connection timeout=3";
public Baza()
{
connection = new SqlConnection();
connection.ConnectionString = dbdir;
connection.Open();
}
public DataTable get_data(string q)
{
DataTable dt = new DataTable();
SqlDataReader dr ;
SqlCommand sqlc = new SqlCommand(q);
sqlc.Connection = this.connection;
dr = sqlc.ExecuteReader();
dt.Load(dr);
return dt;
}
}
And treeView creator:
private void Form1_Load(object sender, EventArgs e)
{
Baza baza = new Baza();
DataTable dt = new DataTable();
dt = baza.get_data("Select * from Users order by Id asc");
foreach (DataRow dr in dt.Rows)
{
TreeNode node = new TreeNode(dr["Name"].ToString() + " " + dr["Surname"].ToString());
treeView1.Nodes.Add(node);
}
}
I thought that node should have ID which I can use to make data filling but I dont really know how to do it. Can you help me with that?
I had the same trouble and i did this :
(I had hierarchical data in columns ParentID, IsGroup, OrderInGroup)
Create a class for the TreeViewItems
public class xTreeViewItem : INotifyPropertyChanged
{
public int id { get; set; }
public string Header { get; set; }
public int parent;
public List<xTreeViewItem > children { get; set; }
public bool isGroup = false;
public bool ParentResolved = false;
public float order {get;set;}
public void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
Now make a list with the SQL Data
private void LoadTree()
{
ObservableCollection<xTreeViewItem> treeSource = new ObservableCollection<xTreeViewItem>();
List<xTreeViewItem> Buffer = new List<xTreeViewItem>();
foreach (DataRow row in table.Rows)
{
xTreeViewItem itm = new TreeViewItem();
// Load the required heirarchial data
Buffer.Add(itm);
}
for (int i = Buffer.Count - 1; i >= 0; i--)
{
if (Buffer[i].parentID != 0)
{
if (Buffer[i].ParentResolved)
Buffer.RemoveAt(i);
else
{
Buffer[i].ParentResolved = true;
while (Buffer.Any(c => (c.parentID == Buffer[i].menuID && !c.ParentResolved)))
{
Buffer[i].children.Add(FindChildren(Buffer[i]));
}
xTreeViewItem parent = Buffer.First(c => c.menuID == Buffer[i].parentID);
parent.children.Add(Buffer[i]);
Buffer[i].parent = parent;
Buffer.RemoveAt(i);
}
}
}
// remaining items acts as mainItems, so add it to the treeSource
foreach (xTreeViewItem x in Buffer)
{
treeSource.Add(x);
}
treeView.ItemsSource = treeSource;
}
xTreeViewItem FindChildren(xMenuItem x)
{
List<xTreeViewItem > subBuffer = Buffer.Where(c => (c.parentID == x.menuID && !c.ParentResolved)).ToList(); // if there is, get all of them to a list
subBuffer = subBuffer.OrderBy(c => c.order).ToList(); // << THIS IS CRAZY, BUT FIXES :P // order all of them by their 'order' property
int indx = Buffer.IndexOf(subBuffer[0]); // get the first item after ordering
Buffer[indx].ParentResolved = true;
Buffer[indx].parent = x; // changing parentresolved property to prevent infinte loops
while (Buffer.Any(c => (c.parentID == Buffer[indx].menuID && !c.ParentResolved))) // again checking if the current child got any other child.
{
Buffer[indx].children.Add(FindChildren(Buffer[indx])); // adding all the childs
}
return Buffer[indx]; // return the child
}
In XAML : put this as the TreeView's ItemTemplate :)
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding children}">
<ContentPresenter Content="{Binding Header}" Margin="2,0"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
This did the work for me :P
and to get the selected item, just use :
xTreeViewItem selectedItem = (xTreeViewItem)treeView.SelectedItem;
make your view update with this 'selectedItem'; .. :)
Hope it helped :)
** Sometimes the items may appear reversed. Simply give the List.Reverse(); in the point where it is so, i havent run the code myself, anyway this should work for all types, like loading MenuItems from Database and desplaying (useful if you have multiple languages) :)
** Im just 18 yrs old :P havent got a degree in CS, this is just a code that i came up with :) yea there might be flaws but it works for me :P
:)

Dropdownlist control value not saving to database

I have two dropdownlist controls on an add/edit form page, one for countries (called CountryCode) and one for regions/states (called ProvinceCode). The code has been set up so that, when users select "United States" from the country dropdownlist, then the region dropdownlist is populated with all valid US states. If the user selects "Canada" from the country dropdownlist, then the region dropdownlist is populated with valid provinces. This functionality works fine, but when "Canada" is selected, for some reason, the region dropdownlist's value is never saved to the database. I am not sure why this is, especially since I can save US states just fine.
This is the code that is meant to populate the country and region dropdownlists:
protected void Page_Init(object sender, EventArgs e)
{
CountryCode.DataSource = CountryDataSource.LoadAll("Name");
CountryCode.DataBind();
InitCountryAndProvince();
}
private void InitCountryAndProvince()
{
//MAKE SURE THE CORRECT ADDRESS IS SELECTED
Address address = this.Address;
bool foundCountry = false;
if (!string.IsNullOrEmpty(address.CountryCode))
{
ListItem selectedCountry = CountryCode.Items.FindByValue(address.CountryCode);
if (selectedCountry != null)
{
CountryCode.SelectedIndex = CountryCode.Items.IndexOf(selectedCountry);
foundCountry = true;
}
}
if (!foundCountry)
{
Warehouse defaultWarehouse = AbleContext.Current.Store.DefaultWarehouse;
ListItem selectedCountry = CountryCode.Items.FindByValue(defaultWarehouse.CountryCode);
if (selectedCountry != null) CountryCode.SelectedIndex = CountryCode.Items.IndexOf(selectedCountry);
}
//MAKE SURE THE PROVINCE LIST IS CORRECT FOR THE COUNTRY
UpdateCountry();
//NOW LOOK FOR THE PROVINCE TO SET
ListItem selectedProvince = ProvinceCode.Items.FindByValue(address.Province);
if (selectedProvince != null) ProvinceCode.SelectedIndex = ProvinceCode.Items.IndexOf(selectedProvince);
}
private void UpdateCountry()
{
//SEE WHETHER POSTAL CODE IS REQUIRED
string[] countries = AbleContext.Current.Store.Settings.PostalCodeCountries.Split(",".ToCharArray());
//PostalCodeRequired.Enabled = (Array.IndexOf(countries, Country.SelectedValue) > -1);
//SEE WHETHER PROVINCE LIST IS DEFINED
IList<Province> provinces = ProvinceDataSource.LoadForCountry(CountryCode.SelectedValue);
if (provinces.Count > 0)
{
IEnumerable<Province> sortedProvinces = provinces.OrderBy(f => f.Name);
provinces = sortedProvinces.ToList();
ProvinceCode.Visible = false;
ProvinceCode.Visible = true;
ProvinceCode.Items.Clear();
ProvinceCode.Items.Add(string.Empty);
foreach (Province province in provinces)
{
string provinceValue = (!string.IsNullOrEmpty(province.ProvinceCode) ? province.ProvinceCode : province.Name);
ProvinceCode.Items.Add(new ListItem(province.Name, provinceValue));
}
ListItem selectedProvince = FindSelectedProvince();
if (selectedProvince != null) selectedProvince.Selected = true;
ProvinceCode.Enabled = true;
ProvinceCode.Text = string.Empty;
}
else
{
ProvinceCode.Visible = true;
ProvinceCode.Visible = false;
ProvinceCode.Items.Clear();
//Province2Required.Enabled = false;
}
}
private ListItem FindSelectedProvince()
{
string defaultValue = ProvinceCode.Text;
if (string.IsNullOrEmpty(defaultValue)) defaultValue = Request.Form[ProvinceCode.UniqueID];
if (string.IsNullOrEmpty(defaultValue)) return null;
defaultValue = defaultValue.ToUpperInvariant();
foreach (ListItem item in ProvinceCode.Items)
{
string itemText = item.Text.ToUpperInvariant();
string itemValue = item.Value.ToUpperInvariant();
if (itemText == defaultValue || itemValue == defaultValue) return item;
}
return null;
}
protected void Country_Changed(object sender, EventArgs e)
{
//UPDATE THE FORM FOR THE NEW COUNTRY
UpdateCountry();
}
This is the section of the save method that contains the country and region dropdownlist controls:
private void SaveCustomerInfo(int CustID)
{
int currentUserID = AbleContext.Current.UserId;
string editQuery = "UPDATE Customers SET BillToRegion = #BillToRegion, BillToCountry = #BillToCountry WHERE CustomerID = #CustomerID";
string addQuery = "INSERT INTO Customers (BillToRegion, BillToCountry) VALUES(#BillToRegion, #BillToCountry)";
try
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.Parameters.Add(new SqlParameter("#BillToRegion", ProvinceCode.SelectedValue));
cmd.Parameters.Add(new SqlParameter("#BillToCountry", CountryCode.SelectedValue));
cmd.ExecuteNonQuery();
cn.Close();
}
}
catch (Exception exception)
{
Logger.Warn("Admin\\People\\Customers\\EditCustomer.aspx - SaveCustomerInfo", exception);
}
And this is the code that is called when the page loads to populate fields with values from the database (if the user is editing an existing entry):
protected void Page_Load(object sender, EventArgs e)
{
_CustomerID = AlwaysConvert.ToInt(Request.QueryString["CustomerID"]);
int.TryParse(Request.QueryString["CustomerID"], out _CustomerID);
if (_CustomerID == 0)
{
AddBtn.Visible = true;
EditBtn.Visible = false;
}
else
{
custIDHidden.Value = _CustomerID.ToString();
AddBtn.Visible = false;
EditBtn.Visible = true;
}
if (!Page.IsPostBack)
{
if (_CustomerID != 0)
{
string selectQuery = "BillToRegion, BillToCountry FROM Customers WHERE CustomerID = #CustomerID";
try
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
cn.Open();
SqlCommand cmd = new SqlCommand(selectQuery, cn);
cmd.Parameters.Add(new SqlParameter("#CustomerID", custIDHidden.Value));
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ProvinceCode.SelectedValue = reader["BillToRegion"].ToString();
CountryCode.SelectedValue = reader["BillToCountry"].ToString();
}
}
cn.Close();
}
}
catch (Exception x)
{
Logger.Warn("Admin\\People\\Customers\\EditCustomer.aspx - Page_Load", x);
}
}
}
}

display items inside a session variable inside a repeater?

I am trying to get the items stored in a sessions variable into a repeater for users to see. However, I am not entirely sure how to do it (I'm new to session variables). Basically, when users enter in quantities for items on once page the hit submit, they are taken to an "order summary" page, which will display what they plan to purchase. I have successfully set up a session variable to contain the sku and quantity of each product the user selects, but I do not know how to get the information out.
I've stored the information in the session variable as [sku],[quantity];[sku],[quantity];[sku],[quantity] and so on. I figure I must do a split or something based on the commas and semicolons but I am not sure how to do so with a session variable.
The code for the product listing page that contains the information to be stored in the session variable:
public partial class GojoptproductlistSublayout : System.Web.UI.UserControl
{
private void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Item CurrentItem = Sitecore.Context.Item;
Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoMarket");
if (HomeItem != null)
{
Item ProductGroup = HomeItem.Axes.SelectSingleItem(#"child::*[##templatename='gojoMarketOfficeBuildigProductMap']/*[##templatename='gojoProductList']");
if (ProductGroup != null)
{
Item[] LocationList = ProductGroup.Axes.SelectItems(#"child::*[##templatename='gojoProductLocation' and #Active = '1']");
if (LocationList != null)
{
DataSet ds = new DataSet();
DataTable locations = ds.Tables.Add("locations");
locations.Columns.Add("LocationName", Type.GetType("System.String"));
locations.Columns.Add("LocationID", Type.GetType("System.String"));
foreach (Item LocationItem in LocationList)
{
DataRow dr = locations.NewRow();
dr["LocationName"] = LocationItem.Fields["Header"].Value;
dr["LocationID"] = LocationItem.ID.ToString();
locations.Rows.Add(dr);
}
locationRepeater.DataSource = ds;
locationRepeater.DataMember = "locations";
locationRepeater.DataBind();
}
}
}
}
}
protected void SetInner(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType != ListItemType.Footer) & (e.Item.ItemType != ListItemType.Header))
{
Label refID = (Label)e.Item.FindControl("refID");
Label test = (Label)e.Item.FindControl("test");
Repeater areaRepeater = (Repeater)e.Item.FindControl("areaRepeater");
Database db = Sitecore.Context.Database;
Item LocationAreaItem = db.Items[refID.Text];
if (LocationAreaItem != null)
{
Item[] AreaList = LocationAreaItem.Axes.SelectItems(#"child::*[##templatename='gojoProductLocationArea' and #Active = '1']");
if (AreaList != null)
{
DataSet dset = new DataSet();
DataTable areas = dset.Tables.Add("areas");
areas.Columns.Add("AreaName", Type.GetType("System.String"));
areas.Columns.Add("Sku", Type.GetType("System.String"));
areas.Columns.Add("ProductName", Type.GetType("System.String"));
areas.Columns.Add("masterSku", Type.GetType("System.String"));
areas.Columns.Add("masterName", Type.GetType("System.String"));
areas.Columns.Add("Size", Type.GetType("System.String"));
areas.Columns.Add("SkuID", Type.GetType("System.String"));
areas.Columns.Add("AreaID",Type.GetType("System.String"));
areas.Columns.Add("productID", Type.GetType("System.String"));
foreach (Item AreaItem in AreaList)
{
DataRow drow = areas.NewRow();
drow["AreaName"] = AreaItem.Fields["Header"].Value;
drow["AreaID"] = AreaItem.ID.ToString();
areas.Rows.Add(drow);
Item[] SkuList = AreaItem.Axes.SelectItems(#"child::*[(##templatename='gojoPTRefill' or ##templatename = 'gojoPTAccessories' or ##templatename = 'gojoPTDispenser' or ##templatename = 'gojoPTSelfDispensed') and #Active = '1']");
foreach (Item ChildItem in SkuList)
{
Item MarketProduct = db.Items[ChildItem.Fields["Reference SKU"].Value];
drow["productID"] = ChildItem.ID.ToString();
if (MarketProduct != null)
{
Item MasterProduct = db.Items[MarketProduct.Fields["Master Product"].Value];
if (MasterProduct != null)
{
DataRow newRow = areas.NewRow();
if(MasterProduct.TemplateName == "gojoSKUSelfDispensed" || MasterProduct.TemplateName == "gojoSKURefill")
{
newRow["Size"] = MasterProduct.Fields["Size"].Value;
}
else
{
newRow["Size"] = "-";
}
newRow["Sku"] = MasterProduct.Fields["SKU"].Value;
newRow["productID"] = MasterProduct.ID.ToString();
Item MasterProductName = db.Items[MasterProduct.Fields["Complete Product Name"].Value];
if (MasterProductName != null)
{
newRow["ProductName"] = MasterProductName.Fields["Complete Name"].Value;
}
areas.Rows.Add(newRow);
}
}
}
}
areaRepeater.DataSource = dset;
areaRepeater.DataMember = "areas";
areaRepeater.DataBind();
}
}
}
}
protected bool checkQtys(ref int ItemCnt, ref ArrayList LinesToOrder)
{
Repeater locationRepeater = (Repeater)FindControl("locationRepeater");
bool validQtys = true;
string productID = "";
int qty;
qtyErrorMsg.Text = "";
qtyErrorMsgTop.Text = "";
foreach (RepeaterItem repItem in locationRepeater.Items)
{
if (repItem != null)
{
Repeater areaRepeater = (Repeater)repItem.FindControl("areaRepeater");
if (areaRepeater != null)
{
foreach (RepeaterItem skuItm in areaRepeater.Items)
{
if (skuItm != null)
{
Label SkuID = (Label)skuItm.FindControl("SkuID");
Label qtyID = (Label)skuItm.FindControl("qtyID");
PlaceHolder inner = (PlaceHolder)skuItm.FindControl("ProductTable");
if (inner != null)
{
foreach (Control ct in inner.Controls)
{
if (ct is TextBox)
{
TextBox lineQty = (TextBox)ct;
Label prodID = (Label)inner.FindControl("productID");
if (lineQty.Text != "")
{
try
{
int.Parse(lineQty.Text);
productID = prodID.Text;
qty = int.Parse(lineQty.Text);
if (qty > 0)
{
noItemMsg.Visible = false;
noItemMsgTop.Visible = false;
ItemCnt++; //only count items with valid qty values
LinesToOrder.Add(new LineItem(productID, qty));
}
else
{//Qty is 0 or less error
validQtys = false;
qtyErrorMsg.Text = "Quantity must be a number<br />";
qtyErrorMsgTop.Text = "Quantity must be a number<br />";
}
}
catch
{//NaN - display error msg
validQtys = false;
qtyErrorMsg.Text = "Quantity must be a number<br />";
qtyErrorMsgTop.Text = "Quantity must be a number<br />";
}
}
}
}
}
}
}
}
}
}
return validQtys;
}
class LineItem
{//This class will store the product information
public string SKUID;
public int Qty;
public LineItem(string InSKUID, int InQty)
{
this.SKUID = InSKUID;
this.Qty = InQty;
}
}
protected void orderSubmit(object sender, EventArgs e)
{
int ItemCnt = 0;
bool validQtys = true;
ArrayList LinesToOrder = new ArrayList();
Label lb = FindControl("order") as Label;
if (checkQtys(ref ItemCnt,ref LinesToOrder))
{
if (ItemCnt == 0)
{//make sure at least one item with proper qty amount is entered before submitting the order
validQtys = false;
noItemMsg.Visible = true;
noItemMsg.Text = "You must order at least one item<br />";
noItemMsgTop.Visible = true;
noItemMsgTop.Text = "You must order at least one item<br />";
}
if (validQtys)
{//save the information to a session variable and send users to order review page
try
{
foreach (LineItem WorkLine in LinesToOrder)
{
lb.Text += WorkLine.SKUID + ", " + WorkLine.Qty + ";";
}
Session["orderComplete"] = lb.Text;
}
catch (Exception x)
{
Response.Write(x.Message.ToString());
}
Response.Redirect("/united-states/market/office-buildings/obproductmap/OrderReview");
}
}
}
}
Here is the designer code with the repeater that is meant to hold the order summary:
<asp:Repeater ID="orderRepeater" runat="server" >
<headertemplate>
<tr>
<th>SKU</th>
<th>Quantity</th>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td><%#Eval("sku") %></td>
<td><%#Eval("qty") %></td>
</tr>
</itemtemplate>
</asp:Repeater>
And here is the code behind:
private void Page_Load(object sender, EventArgs e)
{
Item CurrentItem = Sitecore.Context.Item;
Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoMarket");
if (Session["orderComplete"] != "")
{
if (HomeItem != null)
{
Item ProductGroup = HomeItem.Axes.SelectSingleItem(#"child::*[##templatename='gojoMarketOfficeBuildigProductMap']/*[##templatename='gojoOrderReview']");
if (ProductGroup != null)
{
//this is where I am confused on how to proceed
}
}
}
}
Everything is working and I did a Response.Write test on the session variable to make sure it had the correct information and it did.
Thanks in advance!
Before I try to address your question, lets take a look at some basics. A Session can hold an object, not just a string object. I would change:
if (Session["orderComplete"] != "")
to
if (Session["orderComplete"] != null && Session["orderComplete"] != "")
if you don't, and Session["orderComplete"] is null, Session["orderComplete"] != "" will throw an error object not set to an instance of an object
And now to your question. Setting your session variable to [sku],[quantity];[sku],[quantity];[sku],[quantity], is not a good idea. For one, its not object oriented and 2, its not going to bind to any repeater or data source. You should create an object and bind a list of those objects to your control:
pseudo code:
class Program
{
static void Main(string[] args)
{
List<Order> orders = new List<Order>();
orders.Add(new Order { Sku = "ABC", Qty = 10});
}
}
public class Order {
public String Sku { get; set; }
public int Qty { get; set; }
}
Then you can bind orders to your repeater. For example:
if (Session["orderComplete"] != null && Session["orderComplete"] != ""){
List<Order> orders = Session["orderComplete"] as List<Order>;
myRepeater.DataSource = orders;
myRepeater.DataBind();
}

C# Observable Collection LDAP Paths Children for WPF TreeView

I'm hoping someone can help. A long time windows forms/aspx user, moving to WPF.
Not expecting a coded answer to this, but any pointers on a different way to approach would be greatly appreciated - I am probably approaching this in a very backward way.
So the objective is to have an ObservableCollection with sub ObservableCollection "childen" within to then bind to my WPF treeview control.
I can bind my collection to the treeview without issues, and have styled it with checkboxes images as desired, frustratingly, its the ObservableCollection with children of children of children I am having trouble generating in the first place.
I have a table in SQL with LDAP Paths, and various other information I'm storing against that LDAP path, which I read into my ObservableCollection.
Single level, no problem, the bit I'm struggling with is sorted the sub objects of sub objects by LDAP Path, so when I bind to the treeview is presented as AD OU's are structured.
EG:
TopOU
Users
Front Office Users
Helpdesk Users
Example LDAP Paths in my DB
LDAP://OU=Front Office Users,OU=Users,OU=TopOU,DC=dev,DC=local
LDAP://OU=Helpdesk Users,OU=Users,OU=TopOU,DC=dev,DC=local
LDAP://OU=OU=Users,OU=TopOU,DC=dev,DC=local
LDAP://OU=OU=TopOU,DC=dev,DC=local
private ObservableCollection<AssignmentData> OUTreeAssignmentsCollection = new ObservableCollection<AssignmentData>();
public class AssignmentData : INotifyPropertyChanged
{
public Int32 AssignmentID { get; set; }
public String AssignmentName { get; set; }
public AssignmentTypes AssignmentType { get; set; }
//other stuff....
//For TreeView all sub nodes
public ObservableCollection<AssignmentData> Children { get; set; }
}
I then start to read from my db in a rather nasty way, and this is where it all goes wrong, and I could use some pointers.
cmd = new SqlCommand("SELECT UserGroups.UserGroupID, UserGroups.Name, UserGroups.LDAPPath FROM UserGroups WHERE UserGroups.TypeID=1", DBCon);
reader = cmd.ExecuteReader();
while (reader.Read())
{
String strLDAPHierarchical = GetLDAPHierarchical(reader[2].ToString());
AssignmentData newItem = new AssignmentData()
{
AssignmentID = Convert.ToInt32(reader[0]),
AssignmentName = reader[1].ToString(),
AssignmentImage = ouIcon,
AssignmentLDAPPath = reader[2].ToString(),
AssignmentCNPath = GetCNFromLDAPPath(reader[2].ToString()),
AssignmentTooltip = GetADSLocationTooltip(reader[2].ToString()),
AssignmentType = AssignmentTypes.UserOU,
AssignmentLDAPHierarchical = strLDAPHierarchical
};
if (strLDAPHierarchical.Contains(","))
{
//Now check all the root nodes exist to continue
String strLDAPHierarchicalCheckPath = strLDAPHierarchical;
String[] SplitLDAPHierarchical = strLDAPHierarchical.Split(new Char[] { ',' });
Int32 reverseI = SplitLDAPHierarchical.Length - 1;
String prevPath = "";
for (int i = 0; i < SplitLDAPHierarchical.Length; i++)
{
String path = SplitLDAPHierarchical[reverseI];
//now check if this node is already there and if not look it up and create it
if (path != "")
{
if (i == 0) { strLDAPHierarchicalCheckPath = path; }
else { strLDAPHierarchicalCheckPath = path + "," + prevPath; }
WriteLog("CHECK:" + strLDAPHierarchicalCheckPath);
LookupItemByLDAPHierarchical(strLDAPHierarchicalCheckPath, newItem);
if (i == 0) { prevPath = path; }
else { prevPath = path + "," + prevPath; }
reverseI = reverseI - 1;
}
}
}
else
{
//is top level object, so create at the root of the collection
UserOUCollection.Add(newItem);
}
Function to add sub items :-/
internal AssignmentData LookupItemByLDAPHierarchical(String strLDAPHierarchical, AssignmentData fromItem)
{
AssignmentData currentItem = null;
foreach (AssignmentData d in UserOUCollection)
{
if (d.AssignmentLDAPHierarchical == strLDAPHierarchical) { currentItem = d; break; }
if (d.Children != null)
{
currentItem = CheckChildNodesByLDAPHierarchical(d, strLDAPHierarchical);
if (currentItem != null) { break; }
}
}
String strMessage = "null";
if (currentItem != null) { strMessage = currentItem.AssignmentLDAPPath; }
if (currentItem == null)
{
String strWhere = "LDAPPath LIKE 'LDAP://" + strLDAPHierarchical + "%'";
SqlConnection DBCon = new SqlConnection(SQLString);
DBCon.Open();
SqlCommand cmd = new SqlCommand("SELECT UserGroupID, Name, LDAPPath FROM UserGroups WHERE " + strWhere + " AND TypeID=1", DBCon);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
strLDAPHierarchical = GetLDAPHierarchical(reader[2].ToString());
AssignmentData newItem = new AssignmentData()
{
AssignmentID = Convert.ToInt32(reader[0]),
AssignmentName = reader[1].ToString(),
AssignmentImage = ouIcon,
AssignmentLDAPPath = reader[2].ToString(),
AssignmentCNPath = GetCNFromLDAPPath(reader[2].ToString()),
AssignmentTooltip = GetADSLocationTooltip(reader[2].ToString()),
AssignmentType = AssignmentTypes.UserOU,
AssignmentLDAPHierarchical = strLDAPHierarchical
};
String strLDAPHierarchicalCheckPath = strLDAPHierarchical;
foreach (String path in strLDAPHierarchical.Split(new Char[] { ',' }))
{
//now check if this node is already there and if not look it up and create it
if (path != "")
{
strLDAPHierarchicalCheckPath = strLDAPHierarchicalCheckPath.Replace(path + ",", "");
currentItem = LookupItemByLDAPHierarchical(strLDAPHierarchicalCheckPath, currentItem);
if (null == currentItem)
{
UserOUCollection.Add(newItem); //new root item
}
else
{
if (currentItem.Children == null)
{
//add new child
currentItem.Children = new ObservableCollection<AssignmentData> { newItem };
}
else
{
//add more children to exisiting
currentItem.Children.Add(newItem);
}
}
currentItem = null;
}
}
//Find a current Item to add the node to
//currentItem = LookupItemByLDAPHierarchical(strLDAPHierarchical);
}
reader.Close();
reader.Dispose();
DBCon.Close();
DBCon.Dispose();
}
return currentItem;
}
With my current solution, I get a treeview, with sub nodes of sub nodes, but they are wrong/lots of duplication etc. I have spent literally days trying to fix my probably overcomplicated attempt above - but have come to the conclusion I'm probably going about it the wrong way.
Any help greatly appreciated!
Just having a peruse ;) through your code. Think I can see why you have lots of duplications. Looks like your first SQL query get's all parent/child records. Then the second query will go and get some of those records again, if that makes sense.
One approach would be to only get the top level items in your first query. Possibly by getting SQL to count the number of commas.
SELECT UserGroups.UserGroupID, UserGroups.Name, UserGroups.LDAPPath,
LENGTH(LDAPPath) - LENGTH(REPLACE(LDAPPath, ',', '')) as CommaCount
FROM UserGroups
WHERE UserGroups.TypeID=1
AND CommaCount = 2
Since you asked for different approach id say it's not very efficient to repeatedly query the database in a loop. When I'm building a tree of parent child objects I'd normally get all parent/child records in one query. Build a flat dictionary of all the objects. Then loop through it and make the parent/child associations.
The dictionary can also be useful to lookup your objects later on either directly by key or to loop through without having to make a recursive function that crawls the tree.
So I'd suggest that you break it down into 2 blocks of code.
First block: Using your existing query that get's all of the items, create a flat Dictionary with everything in.
They key of each item should probably be the result from GetLDAPHierarchical().
Second block: Next loop through the dictionary and create the hierarchy. Add anything with no parent directly to the UserOUCollection
foreach(AssignmentData d in myDictionary.Values)
{
String parentKey = GetParentLDAPKey(d.AssignmentLDAPHierarchical);
if (myDictionary.ContainsKey(parentKey))
{
myDictionary(parentKey).children.Add(d);
}
else
{
UserOUCollection.Add(d);
}
}
GetParentLDAPKey() will need to produce the same key as it's parent by removing the first part of the LDAP Path.
Hope that points you in the right direction.
H
(SMASH)
Thanks so much to hman, who pointed me in a much more logical direction. I used LDAPPath as my dictionary key.
Dictionary<String, AssignmentData> OUDictionary = new Dictionary<String, AssignmentData>();
//Read from DB
cmd = new SqlCommand("SELECT UserGroups.UserGroupID, UserGroups.Name, UserGroups.LDAPPath FROM UserGroups WHERE UserGroups.TypeID=1", DBCon);
reader = cmd.ExecuteReader();
while (reader.Read())
{
AssignmentData newItem = new AssignmentData()
{
AssignmentID = Convert.ToInt32(reader[0]),
AssignmentName = reader[1].ToString(),
AssignmentImage = ouIcon,
AssignmentLDAPPath = reader[2].ToString(),
AssignmentCNPath = GetCNFromLDAPPath(reader[2].ToString()),
AssignmentTooltip = GetADSLocationTooltip(reader[2].ToString()),
AssignmentType = AssignmentTypes.UserOU,
};
UserOUDictionary.Add(reader[2].ToString(), newItem);
}
reader.Close();
reader.Dispose();
//Now Read OU List into TreeView Collection
foreach (AssignmentData d in UserOUDictionary.Values)
{
String parentKey = GetParentLDAPPath(d.AssignmentLDAPPath);
if (UserOUDictionary.ContainsKey(parentKey))
{
AssignmentData parentItem = UserOUDictionary[parentKey];
if (parentItem.Children == null) { parentItem.Children = new ObservableCollection<AssignmentData> { d }; } //add first child
else { parentItem.Children.Add(d); } //add more children to exisiting
}
else
{
UserOUCollection.Add(d); //add to root of control
}
}
private String GetParentLDAPKey(String strLDAPPath)
{
String retParentKey = strLDAPPath;
if (strLDAPPath.Contains(","))
{
retParentKey = retParentKey.Replace("LDAP://", "");
retParentKey = retParentKey.Remove(0, retParentKey.IndexOf(",") + 1);
retParentKey = "LDAP://" + retParentKey;
}
return retParentKey;
}

Categories

Resources