Null reference when using a variable with findcontrol - findcontrol

I have a .aspx page with several textboxes, including textboxes with IDs of txtID1, txtID2, txtID3... and so on.
I am attempting populate the textboxes with a data from an XML file by looping through a node list. With each loop, I want to use the FindControl method to locate txtID1 and set its .Text to the value of the id attribute of the first node; then locate txtID2 and its .Text to the value of the id attribute of the second node, and so on.
When the following line of code is run, I get a null reference error for TextBox txtID, so it appears that I am doing something wrong with the FindControl method. Is my syntax incorrect? Do I need to use a different method?
int x = 1;
XmlNodeList getAuthors = getItem.SelectNodes("item/authors");
foreach (XmlNode getAuthor in getAuthors)
{
TextBox txtID = (TextBox)Page.FindControl("txtID" + x.ToString());
txtID.Text = getAuthor.Attributes["id"].Value.ToString();
x = x + 1;
}

After further research (that is, lots of Googling), it appears that I may be running into this problem because I am using a master page. Neither (TextBox)FindControl nor (TextBox)Page.FindControl was working so I have abandoned this approach. Here is an old article that seems to explain my problem.
http://weblog.west-wind.com/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl

Related

I need to pull the text of an image file name from an array but am only getting "pyimage#" or "tkinter.PhotoImage object at X" as replies

I'm trying to create an array with an assortment of different randomized image files in it to display on a set of buttons in Tkinter. When a given button is clicked I'd like to add the text of that file's name to a new array. Basically, when button with imageX is clicked add "imageX" to a new array.
Unfortunately, I always get a return that isn't the image's filename, or the variable that I've set to correspond to that image, but instead either:
"tkinter.PhotoImage object at X" (where is X is a location like "0x0000020FC894D2E0") if the command is populationbeta.append (population[0])
or
"pyimage#" (where # is an integer that seems to relate to the number of images in the source file), if I change the command to populationbeta.append (str(population[0]))
I feel like there should be a simple way of doing this and I've tried every work around I can think of but I'm not getting it to work. Any help would be very much appreciated! Thanks!
Here's a shortened/simplified version of the code in question:
master=tkinter.Tk()
master.title("Not working")
a1b1c1 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c1.png")
a1b1c2 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c2.png")
a1b1c3 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c3.png")
a1b2c1 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b2c1.png")
a1b2c2 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b2c2.png")
population = [a1b1c1, a1b1c2, a1b1c3, a1b2c1, a1b2c2]
populationbeta = []
populationbeta.append(population[0])
print(populationbeta)
This gives the result: "[<tkinter.PhotoImage object at 0x000001A419D4F070>]"
This gives the result: "[<tkinter.PhotoImage object at 0x000001A419D4F070>]"
Correct. That shows that you have a list of PhotoImage objects. If you want the filenames you can use .cget('file') on the objects. cget is a common tkinter method for getting the value of a configured option.
filenames = [image.cget('filename') for image in population]
Or, if you don't want to use a list comprehension to create a list of filenames, you can do it on an individual image like so:
populationbeta.append(population[0].cget("file"))

Concurrency Error WinForms Binding Source Navigator

I have a form with customer info that needs to be processed one transaction per page. I'm using the binding navigator to manage my pagination.
It works in all but some cases. In the cases where it doesn't work, I have to open a different window to look up information and return it to the main form. Here is the code for that:
// save current work
updateDataTable();
// Open a window and get new customer info
// CurrentCustomer is returned from the opened window
using (SqlConnection cx = new SqlConnection(GetConnectionString()))
{
DataRowView dataRow = (DataRowView)procBindingSource.Current;
dataRow.BeginEdit();
dataRow["CUSTOMER"] = CurrentCustomer;
dataRow.EndEdit();
updateDataItems();
SqlCommand cmd = new SqlCommand(
#" select acct_no from cust_processing where id = #id ", cx);
cmd.Parameters.AddWithValue("#id", (int)dataRow["ID"]);
cx.Open();
var results = cmd.ExecuteScalar();
if (results != null)
{
dataRow.BeginEdit();
dataRow["ACCT_NO"] = results.ToString();
dataRow.EndEdit();
updateDataItems(); <------ CONCURRENCY ERROR
}
}
The error I am getting is a concurrency error. I think that I have more than one version of the row possibly ? I thought I was making sure that I was on the most recent version of the row by calling updateDataTable(). I am the only user so I know I am creating the problem myself.
Here is my update method which is called when I change pages or save and exit or want to write the commit the data:
void updateDataItems()
{
this.procBindingSource.EndEdit();
this.procTableAdapter.Update(xyzDataSet);
xyzDataSet.AcceptChanges();
}
I have tried executing updateDataItems from various places such as after I assign dataRow["ACCT_NO"] = results.ToString() or before and after assigning that.
I'm pretty much down to guess and check so any thoughts, help and advice will be appreciated and +1.
Okay -- so the problem was that I was trying to update the current row from the program and also using the binding navigator. They were not working together properly.
The solution was to add a text box to the form in the forms designer and set visible = false and bind it to ACCT_NO. Once I got the results from my other form, I just needed to set the .text property of the ACCT_NO textbox to the new value and the binding navigator managed all my updates for me correctly.
txtAcct_No.text = results.ToString();

RPS, windows Form & Revit API (Python)

I've been trying to build a form to create and delete Revit print Sets.
I've 2 main issues:
1) I'm able to create a print set but I cannot access its content unless I restart the Form. I get the errors below (depending if I'm defining the view_set variable or not)
List_object_has_no_attribute_Views
Local_variable_referenced_before_assignment
This is the code of the function to display the sheets of the selected Print Set
def DisplaySheetsInSet (self, sender, args):
self.curItem = CurrentSetsListBox.SelectedItem
PrintSetForm_Load
try:
view_set=[]
for i in PrintSetForm.ViewSets:
if i.Name == str(self.curItem):
view_set = i
else:
continue
Sheets=[sheet.Name for sheet in view_set.Views]
SheetsLb.BeginUpdate()
SheetsLb.Items.Clear()
for sheet in Sheets:
SheetsLb.Items.Add(sheet)
SheetsLb.EndUpdate()
except Exception as e:
popup (str(e)
2) I'm able to delete print sets once. If I try do delete another one I get the following error and I need to restart the form ( code for the function that deletes the print sets shown below)
The_referenced_object_is_not_valid
def DelPrintSet(self, sender, args):
self.curItem = CurrentSetsListBox.SelectedItems
t = Transaction (doc, 'Delete printset')
t.Start()
for viewset in PrintSetForm.ViewSets:
if viewset.Name in [str(item) for item in self.curItem]:
doc.Delete(viewset.Id)
doc.Regenerate()
else:
continue
self.Refresh()
UpdateSetNames(CurrentSetsListBox)
t.Commit()
I've tried to build a function to restart/refresh the Form but it doesn't work (code below):
global PrintSetForm_Load
def PrintSetForm_Load(self, sender):
Application.Exit()
Application.Restart()
#self.Refresh()
#self.ResetBindings()
#self.ActiveForm.Close()
sd = PrintSetForm()
sd.ShowDialog()
This gif shows the form in action:
Manage Print Sets
Any ideas or suggestions?
Thank you.
3) If I try to populate the SheetsLb with a DataSource, just the first set clicked is shown.
Sheets=[sheet.Name for sheet in view_set.Views]
SheetNumber=[sheet.get_Parameter(BuiltInParameter.SHEET_NUMBER).AsString() for sheet in view_set.Views]
SheetsLb.BeginUpdate()
SheetsLb.DataSource = None
SheetsLb.Items.Clear()
UpdatedList=[]
for number,name in zip(SheetNumber,Sheets):
UpdatedList.append(number+" - "+ name + " [ ] ")
SheetsLb.DataSource=UpdatedList
SheetsLb.EndUpdate()
1) See if this works:
It would be worth checking that there is something selected in self.viewSetsLb. Ive added a check to the code below
The view_set variable could be initialised as a boolean instead of a list
Using break in the for loop keeps things a little snappier
Ive used the more pythonic for view in PrintSetForm.viewSets rather than for i in PrintSetForm.viewSets - keeping it nice and clear
This code works for me:
self.curItem = self.viewSetsLb.SelectedItem
if not self.viewSetsLb.SelectedItem:
print 'No Printset selected!'
return
view_set = False
for view in PrintSetForm.viewSets:
if view.Name == str(self.curItem):
view_set = view
break
else:
continue
Sheets=[sheet.Name for sheet in view_set.Views]
self.sheetsLb.BeginUpdate()
self.sheetsLb.Items.Clear()
for sheet in Sheets:
self.sheetsLb.Items.Add(sheet)
self.sheetsLb.EndUpdate()
2) Its because the data in your PrintSetForm.ViewSets list is out of date. Every time you change something (ie delete a viewset), repopulate this list:
PrintSetForm.ViewSets = FilteredElementCollector(doc).OfClass(ViewSheetSet).ToElements()
Also, you shouldnt need to build a refresh button, perhaps have a class function that repopulates the Printset list and ListBox, and clears the Sheet ListBox that you call after every action?
Sounds like youre having fun mate!
It sounds as if you have an issue with the scoping and lifetime of variables. For instance, some variables may have a lifetime limited to the form display, and therefore cannot be accessed after the form is closed. You could change the lifetime of these variables, e.g., by making them static class variables instead of local instance variables. I suggest you read up on .net static class variable scope.

how to write a recursive function to create a treeview from data in datawindow?

I have a parent-child data in a datawindow from a table mytab(id, pid....)
Then I want to use to data to create a tree in treeview.
I try to use recursive function, but have problem with datawindow as I use filter to change the data in datawindow.
here is my code:
of_addnode(treeviewitem node, rootrow):
int li_rows,li_newitem, i
treeviewitem li_tvitem
dw_1.SetFilter("pid = " + string(node.data))
dw_1.Filter( )
li_rows = dw_1.RowCount()
if li_rows = 0 then
return
end if
for i = 1 to li_rows
li_tvitem.level = node.level +1
li_tvitem.data = dw_1.GetItemNumber(i,"id")
li_tvitem.label = dw_1.GetItemString(i,"description")
li_tvitem.pictureindex = 1
li_tvitem.selectedpictureindex = 2
li_newitem = tv_1.insertitemsort (rootrow, li_tvitem) // insert a new node
of_addnode(li_tvitem,li_newitem)
dw_1.SetFilter("pid = " + string(node.data)) //restore data back to filter, problem here. tree will have duplicate item
dw_1.Filter( )
next
how to create a recursive function from one datasource datawindow for this case?
Here are some ideas... it sounds like you want to use the treeview control and I don't blame you as the treeview dataobject is not very refined because it shows the expand button even if there aren't any children and I haven't found an elegant way to deal with this.
One thing to consider, will manipulating the data on back-end help you? This works for using the treeview dataobject type or might help in making the recursion logic more simple.
This database view is in MySQL but could be developed for other databases. I snipped it at three levels of recursion and yes it was hard coded for six levels of recursion the max supported by this application. This works great if using a treeview dataobject control but you are stuck with the expand button problems when there are no children. This can also be useful for making recursion logic simpler by effectively giving you "path" to the item.
**DATABASE VIEW TO FLATTEN PARENT AND CHILD (HIERARCHY) RELATIONSHIPS **
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`#`%`
SQL SECURITY DEFINER
VIEW `v_category` AS
select
`t1`.`parent_id` AS `parent1`,
`t1`.`id` AS `id1`,
`t1`.`title` AS `title1`,
`t1`.`sort_order` AS `sort1`,
`t2`.`parent_id` AS `parent2`,
`t2`.`id` AS `id2`,
`t2`.`title` AS `title2`,
`t2`.`sort_order` AS `sort2`,
`t3`.`parent_id` AS `parent3`,
`t3`.`id` AS `id3`,
`t3`.`title` AS `title3`,
`t3`.`sort_order` AS `sort3`
from
(((((`ld_category` `t1`
left join `ld_category` `t2` ON ((`t2`.`parent_id` = `t1`.`id`)))
left join `ld_category` `t3` ON ((`t3`.`parent_id` = `t2`.`id`)))
left join `ld_category` `t4` ON ((`t4`.`parent_id` = `t3`.`id`)))
BUILDING TREEVIEW USING RECURSION
You are on the right track for populating the treeview control but without seeing your dataobject it is impossible to know if your setfilter and filter are working properly and what is going on. The key is to get the level from the insertitemXXX function and pass that into the first argument of the next insertitemXXX function. This simple example of the PB help always gets me on the right track. I see you are using treeviewitems but that doesn't really change things. Hope this helps.
long ll_lev1, ll_lev2, ll_lev3, ll_lev4
int index
ll_lev1 = tv_list.InsertItemLast(0,"Composers",1)
ll_lev2 = tv_list.InsertItemLast(ll_lev1, "Beethoven",2)
ll_lev3 = tv_list.InsertItemLast(ll_lev2, "Symphonies",3)
FOR index = 1 to 9
ll_lev4 = tv_list.InsertItemSort(ll_lev3, "Symphony # " + String(index), 4)
NEXT

Using HTMLDocument to manipulate HTML and show it in WebBrowser-control

I am trying to manipulate a requested document in the WPF WebBrowser-control. I already managed it to invoke JavaScript on loaded document, but I am not able to change the shown HTML-code in the control itself.
My (very simplified) code in the OnNavigating-Handler looks like this:
mshtml.HTMLDocument doc = (mshtml.HTMLDocument)View.browser.Document;
HTMLTableClass table = doc.getElementById("someTable") as HTMLTableClass;
if (table != null)
{
table.appendChild((IHTMLDOMNode)(doc.createElement("<tr>") as IHTMLElement));
}
doc.close();
The -element doesn't get appended to displayed document in the control.
Any hints are very appreciated!
I finally got it. Its only possible to change the content of the table by adding rows and cells which i wanted to avoid in first place. My approach was to directly change the content of the -tag, which didnt work.
mshtml.IHTMLTableRow row = table.IHTMLTable_insertRow(-1) as mshtml.IHTMLTableRow;
mshtml.IHTMLElement c = (mshtml.IHTMLElement)row.insertCell(0);
c.innerText = "some";
mshtml.IHTMLElement c1 = (mshtml.IHTMLElement)row.insertCell(1);
c1.innerText = "text";

Resources