I have a class Cust_Result that accepts an integer parameter.
So on my main page when it loads I bind a formview to display the data that I retrieved. Now I would like to extract the value of my "id" label and assign it to a variable which I can than pass to my Cust_Result class but I keep receiving this error
"Unable to cast object of type 'System.Web.UI.WebControls.Label' to
type 'System.IConvertible'."
I am assuming it is because I am trying to send a string value to a parameter that is wanting an integer value but I am unsure on how to do the conversion.
My code
int cust;
cust = (Convert.ToInt32(FormView1.Row.FindControl("ID")));
You need a string to convert and that's the Label.Text property (not just the Label).
I'll split it into 2 steps:
Label lbl = FormView1.Row.FindControl("ID") as Label;
// option to bail out when lbl == null
cust = Convert.ToInt32(lbl.Text);
First convert the control to a Label
var label = (Label)FormView1.Row.FindControl("ID");
Then you can get the value in the label:
var cust = int.Parse(label.Text);
Look at the compiler error closely - it doesn't say anything about string and int - it's talking about IConvertible and Label - although the fact that it mentions Label instead of Control suggests that's not the code you've actually posted. Convert.ToInt32 doesn't konw what to do with a Control or a Label - in this case, I believe you want the text of the label, so I'd write this:
Label label = (Label) FormView1.Row.FindControl("ID");
// Potentially check for "label" being null here, i.e. the control wasn't found
int cust = Convert.ToInt32(label.Text);
It's not entirely clear where this value came from, but you may want to consider using int.TryParse instead of Convert.ToInt32, too.
I'd also note that Cust_Result is an unconventional name - try to:
Avoid abbreviations (Cust means Customer, I assume?)
Don't use underscores
Try to give more meaningful names - even CustomerResult doesn't really explain what it's the result of.
Related
If I create user setting variables called var1 and var2, is it possible to put the value of var2 inside of var1?
For exemple:
Properties.Settings.Default.var2 = "stackoverflow";
Properties.Settings.Default.var1; // //path/stackoverflow/morepath
Properties.Settings.Default.var2 = "test";
Properties.Settings.Default.var1; // //path/test/morepath
The short answer to your question is no.
A string is just a string and there is no piece of built-in functionality that will dynamically replace %var2% in your first variable with the actual value of the second or any other variable.
If you want to somehow change the values of the variables, you will have to write some code that does this for you.
Settings is essentially just an XML file, so there is no way you can dynamically inject a variable value into an entry.
However, you can achieve what you want at runtime by dynamically updating var1 based on var2's value using the Properties.Settings.Default.Properties object.
Here is an article that has some examples of dynamically updating entries:
https://codedocu.com/Net-Framework/WPF/Basics/Settings/WPF_colon_-Create,-write-and-read-settings-dynamically?2045
Properties.Settings.Default.Properties["var2"].DefaultValue = "your updated value goes here"
I have a table that I fill with one Repeater and one SQL query. Then, I want to color one of the columns depend of the result of one operation. But the problem is that I want to compare one column with another(more specific, intColumn1 <= intColumn2), but I cannot convert the Label into Int (both columns are ints)
I have this code:
Label lblStock = (Label)rptItem.FindControl("lblStockOLD");
int intStock = Convert.ToInt32(lblStock.Text);
And I try to lblStock without ".Text" in the convertion. The display error is this:
Input string was not in a correct format.
What can I do?
I would prefer to approach this problem by calculating the difference in int BEFORE you bind the data to the repeater. However if you have to get the string out of the label and convert it back to int, and the field is nullable, you must do null checking first.
Label lblStock = (Label)rptItem.FindControl("lblStockOLD");
//assume intStock is null and make sure you check for intStock.HasValue later.
int? intStock = null;
if (!string.IsNullOrWhiteSpace(lblStock.Text)) {
intStock = Convert.ToInt32(lblStock.Text);
}
"Input string was not in a correct format." means that what your trying to convert to an int is not a correct int. You could use Int.TryParse for better error handling. if the .Text contains a correct integer value it should be able to convert it. Also keep in mind that if you run the code before filling up the label it will also give errors .
On form load (edit action) I want to combobox be selected with appropriate value. These value is of type LangEnum.
I tried with comboBoxLanguage.SelectedValue = book.Language; but combo is always populated with default value (first from enum list).
update:
book.Language is declared as
public enum EnumLang { English = 1, German = 2, Other = 3 };
I tried both with
comboBoxLang.SelectedItem = (Book.EnumLang)book.Language;
and with
comboBoxLang.SelectedItem = book.Language;
and nothing works (default first value (English) is always set) and worth to mention is that on debug mode book.Language is set To German or Other but English is selected on combobox.
That looks right to me!
I am doing the same thing, are you sure that book.Language string is an EXACT match to one of the items in the list?
And is the list populated BEFORE you are trying to SelectedValue?
Better try:
comboBoxLanguage.SelectedItem = book.Language;
// or even
comboBoxLanguage.Text = book.Language.ToString(); //should work
You might want to set the ValueMember property to get or set the SelectedValue.
Works fine for me using SelectedItem:
comboBoxLanguage.SelectedItem = book.Language;
You didn't tell what kind of variable book is: is it of type LangEnum? Or is it a class with a Language property? (in this last case: what is the type of the Language property?)
If book is of type LangEnum you can use the SelectedItem property as others have said. (Check this SO question if you need more informations of the differences between the two combobox properties)
Otherwise you'll probably need a cast:
comboBoxLanguage.SelectedItem = (LangEnum)book.Language;
Moreover, if you are populating your combobox inside a WinForm event, you should also care about the order in which they are fired. Take a look at this SO question form more infos.
So I have 70 "nodes" which are all textboxes in WPF and I'm trying to change the value in the textbox from a function call.
I have a function called:
private void changeNode(int row, int column, int cost)
{
int nodeNumber= row * 10 + column;
call node"nodeNumber".Text = Convert.String(cost);
//example node0.Text = Convert.String(cost);
}
I determine what node I want to change then call nodeX.Text to change it however I want X to be a variable that I can rather than having to create 70 cases where I call the appropriate textbox.
I saw a couple of ways of doing this with reflection however it seemed to only work if the function had no parameters and also was within the function not a textbox in XAML.
Let me know if there is a simple way to convert say a string "node37" to call node37.Text = cost or something like that.
Sounds like your approach is wrong. Why do you have a set of strings which represent the names of the textboxes? You should instead have in-memory references to TextBox objects. If you have more than one, and you don't know how many there will be, then use an array of TextBox objects instead. You can index into the array with the number that represents the textbox you're looking to interact with.
Avoid the use of reflection, it is completely unnecessary here.
I assume you have put names for all your textboxes (you can do this dynamically if you haven't). Then you can use the answers for this question to find the appropriate control by name.
Are all your textboxes children of the same canvas or other control? Loop through the children and add the controls to a dictionary. Parse the name to get the number and use that as the key.
It is always better to use List when you are dealing with Data. Create an ObservableCollection with the DataObjects which you want to load, and now deal with the Data object rather than actual Controls.
In WPF, if you follow the rules, you should not point to the actual object. Check the sample application here :
http://www.abhisheksur.com/2010/08/woring-with-icollectionviewsource-in.html
I think you will get the approach.
I have a multi-column combobox where the datasource is a List<> in my Select Class
Select selection = new Select();
RadComboBox1.DataSource = selection.GetAcctUtilCo(e.Text, 10).Skip(e.NumberOfItems);
I have a few DataTextFields. My DataValueField is the AcctID.
Once an account is selected, I need the datatextfield values to populate some fields in a gridview.
I am trying to use the .Find() method by AcctID to retreive the data without success:(
int AcctID = Convert.ToInt32(RadComboBox1.SelectedValue); // *
List<Select> mylist = RadComboBox1.DataSource as List<Select>;
mylist.Find(delegate(SelectTop act) { return act.AcctID == acctID; }); // ** exception here
Label lblAcctNo = (Label)grdAccts.HeaderRow.FindControl("lblAcctNo");
lblAcctNo.Text = mylist.AccountNum;
When I debug, I get 'Object reference not set to the instance of an object' on the indicated line.
AcctID is NOT null when I hover over it. However when I hover over mylist, it says null.
I'm new with the .Find method & I'm really not sure if the problem is with that or with using the datasource of the combobox as mylist source.
Can someone please help enlighten me
Your combo-box's data source is not a list. When you use "as" the result is null if you try to cast to an invalid type, instead of throwing an exception like a standard cast. Since you used the Skip function to create your data source you actually have an "IEnumerable<>".
The problem is caused by RadComboBox1.DataSource - it is not persisted between page requests.
In your code that sets RadComboBox1.DataSource, save a copy of the RadComboBox1.DataSource value in the viewstate. eg. ViewState["RadComboBox1"] = RadComboBox1.DataSource;
In the event that runs your above code, restore the RadComboBox1.DataSource by reading the value from the viewstate. eg. RadComboBox1.DataSource = ViewState["RadComboBox1"];
You should hopefully find the values then persist between requests. Good luck!