Is it a good practice to compare combobox.selectedvalue to a string? - asp.net

I have a combo box and couple of text boxes in my web page, depending on combobox's selected value, I will set focus to specific text box. Following is my code:
if (cbo1.SelectedValue == "01")
txt1.Focus();
else
txt2.Focus();
This would work even when the combo box being just loaded and there is no selected item. My question is "is this a good practice?" since SelectedValue actually is an object. Normally I use cob1.SelectedValue.ToString(), but I got an exception when there is no selected item.

Good practice would be to declare a string constant:
private const string FIRST_FIELD_VALUE = "01";
(...)
if (cbo1.SelectedValue.Equals(FIRST_FIELD_VALUE))
txt1.Focus();
else
txt2.Focus();
Otherwise, yes. I think comparing strings with strings is good practice.

Add this condition
if( cbo1.SelectedIndex > 0)
{
if (cbo1.SelectedValue == "01")
txt1.Focus();
else
txt2.Focus();
}

Related

Getting char value from ComboBox in JavaFX

I need to get the selected value in a combobox as of a data type char. I know how to get the selected item it's the conversion which I've got stuck on. Any suggestions?
This is the combobox and it content:
idCharCombo = new ComboBox<>();
idCharCombo.getItems().addAll("A","B","G","H","L","M","P","Z");
Now i will be using this data in a method which passes an int and a char (bellow is the use of the method where the second element is still an object rather than a char):
if (checkStaffMemberById(Integer.parseInt(idNoTxtFld.getText()), idCharCombo.getValue()) == true){
AlertBox.display("ID Validation", "ERROR! ID Already Exists.");
hope i arranged adequately
Since your combo box appears to only hold single-character strings, and you want to treat them as chars, the most obvious thing to do is to use a ComboBox<Character> instead of a ComboBox<String>. I.e. replace your declaration, which presumably looks like
ComboBox<String> idCharCombo ;
with
ComboBox<Character> idCharCombo ;
and then you can do
idCharCombo.getItems().addAll('A','B','G','H','L','M','P','Z');
Then
idCharCombo.getValue()
will return a Character which will be autounboxed to a char as needed, so your method call
checkStaffMemberById(Integer.parseInt(idNoTxtFld.getText()), idCharCombo.getValue())
should work as-is.

Textbox dont allows null value in ASP .Net

i have column in database with Int data type, but textbox don't allows null. it gives error "Input string was not in a correct format".
objinsert.VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
Use TryParse instead.
int GrpID = 0;
int.TryParse(txtVehGroupID.Text, out GrpID)
if(GrpID > 0)
objinsert.VehGrpID = GrpID;
You can just special-case out empty values. I am, of course, assuming here that an empty value should map to null.
objinsert.VehGrpID = string.IsNullOrWhiteSpace(txtVehGroupID.Text) ? null : (int?)Convert.ToInt32(txtVehGroupID.Text);
You'll also want to adjust your code to set the textbox's text accordingly.
Your question is also a bit unclear, because you say the textbox won't allow nulls, but you haven't shown us any code that adjusts the textbox text.
You can also use DBNull.Value Field. If a database field has missing data, you can use the DBNull.Value property.
check this link http://msdn.microsoft.com/en-us/library/system.dbnull.value%28v=vs.110%29.aspx
try this
int VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
OR
int VehGrpIDtxtVehGroupID.Text = int.Parse(txtVehGroupID.Text);
The Text property of your textbox is a String type, so you have to perform the conversion in the code.

SelectedValue which is invalid because it does not exist in the list of items

I encounter this problem repeatedly, and haven't a clue what is causing it. I get an exception in the DataBind: SelectedValue which is invalid because it does not exist in the list of items.
Here are some important pieces of information:
I reload listOrgs periodically when the underlying data has changed.
The Organization.DTListAll call returns 2 Int, String pairs.
There are no duplicate or null values in the returned data
After the first two lines below, listOrgs.Items.Count is 0, and the Selected Value is 0
The selected value after the DataBind operation executes is the ID value from the first row in the data
This exception happens the very first time this code is executed after a fresh page load
listOrgs.Items.Clear();
listOrgs.SelectedValue = "0";
listOrgs.DataSource = new Organization().DTListAll(SiteID);
listOrgs.DataTextField = "OrganizationName";
listOrgs.DataValueField = "OrganizationID";
listOrgs.DataBind();
Apparently the solution I posted wasn't entirely effective... Eventually in my application I changed to this:
listOrgs.Items.Clear();
listOrgs.SelectedIndex = -1;
listOrgs.SelectedValue = null;
listOrgs.ClearSelection(); // Clears the selection to avoid the exception (only one of these should be enough but in my application I needed all..)
listOrgs.DataSource = new Organization().DTListAll(SiteID);
listOrgs.DataTextField = "OrganizationName";
listOrgs.DataValueField = "OrganizationID";
listOrgs.DataBind();
I kept getting this error.
Weird thing is that before I set the datasource and rebind after deleting an item the selected index = -1.
If I explicitly set the selectedIndex = -1; then it works and doesn't throw an error.
So even though it was already -1 setting it to -1 stops it from erroring.
Weird eh?
Try setting listOrgs.SelectedValue = "0" after you refresh the DataSource
At the moment you are trying to select the first item in an empty list.
Change the first two line with this :
listOrgs.SelectedItem.Selected = false;
listOrgs.Items.Clear();
In case you still have this problem this is how i resolved it:
listOrgs.SelectedIndex = -1; // Clears the SelectedIndex to avoid the exception
listOrgs.DataSource = new Organization().DTListAll(SiteID);
listOrgs.DataTextField = "OrganizationName";
listOrgs.DataValueField = "OrganizationID";
listOrgs.DataBind(); //Unless you have "listOrgs.AppendDataBoundItems = true" you don't need to clear the list
#PMarques answer helped me out and did solve my problem.
However whilst experimenting, it clicked in my head why I was gettign the error in the first place.
I was setting the "Text" attribute thinking that it might create an encompassing label or fieldset + legend, for me (which it doesn't).
The Text property for a list is infact the SelectedValue property for a ListControl.
So my mistake in misinterpreting what the text property did.
Not sure it is your case, but I had the same problem and apparently there was no explanation, then I realized doing a copy and paste on notepad of a field of database that at the beginning of the value there was a NULL.
Curious thing was that a select joining tables was working. I deleted the row and reinserted, after was working fine.
I was getting the same error repeatedly and try ending up by not setting the default selected value to Index -1.
I commented my code ddlDRIBidAmt.SelectedValue = -1
This value was set at the time where my Page Controls were reset to default values.
I know its too late to answer, but what I tried is an dirty solution but it worked.
After databinding, I am insert a item at index 0
ddl.Items.Insert(0, new ListItem("---Select---","-1"));
And on setting,
I am placing try catch, In catch i am setting Value to -1

Flex Rich Text Editor - Limiting the number of characters

Is there a way to restrict the number
of characters in the Flex Rich Text Editor?
I guess there should be, since it's possible
in a textarea. So, if I could get hold
of the textarea contained in the rich
text editor, I would be able to do it
I think this would be fairly easy in actionscript, although I'm not exactly sure how one would do it in mxml. It appears that there are two children that are contained in the RichTextEditor, one of them being TextArea. According to the documentation (http://livedocs.adobe.com/flex/3/langref/mx/controls/RichTextEditor.html#propertySummary), you can access the subcontrols like so:
myRTE.toolBar2.setStyle("backgroundColor", 0xCC6633);
With myRTE being the instance of your text editor. So my guess would be something like this would work:
myRTE.textArea.maxChars = 125;
With 125 being the number a characters you would want restricted to.
i just ran into this.
setting your maxChars on the textArea will provide a limit to the text area, but that won't be representative of the number of characters the user can type.
as the user is typing, markup is added behind the scenes, and that greatly increases the char count.
for example, if i type the letter 'a' into a RichTextEditor, i get a char count of 142 and this htmlText:
<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="10" COLOR="#0B333C" LETTERSPACING="0" KERNING="0">a</FONT></P></TEXTFORMAT>
i could not see a straightforward way to get a proper maxChar to work out of the box, so i extended RichTextEditor and gave it a maxChar. if maxChar > 0, i added a listener to "change" and did something like this in the event handler:
protected function handleTextChange(event:Event) : void
{
var htmlCount:int = htmlText.length;
// if we're within limits, ensure we reset
if (htmlCount < maxChars)
{
textArea.maxChars = 0;
this.errorString = null;
}
// otherwise, produce an error string and set the component so the user
// can't keep typing.
else
{
var textCount:int = textArea.text.length;
textArea.maxChars = textCount;
var msg:String = "Maximum character count exceeded. " +
"You are using " + htmlCount + " of " + maxChars + " characters.";
this.errorString = msg;
}
}
the idea is to apply a maxChars to the text area only when in the error state, so the user cannot type anything additional and will be prompted to erase some chars. once we leave the error state, we need to set the textArea.maxChars to zero so they can continue.

How can I tell if E4X expression has a match or not?

I am trying to access an XMLList item and convert it to am XML object.
I am using this expression:
masonicXML.item.(#style_number == styleNum)
For example if there is a match everything works fine but if there is not a match then I get an error when I try cast it as XML saying that it has to be well formed. So I need to make sure that the expression gets a match before I cast it as XML. I tried setting it to an XMLList variable and checking if it as a text() propertie like this:
var defaultItem:XMLList = DataModel.instance.masonicXML.item.(#style_number == styleNum);
if(defaultItem.text())
{
DataModel.instance.selectedItem = XML(defaultItem);
}
But it still give me an error if theres no match. It works fine if there is a match.
THANKS!
In my experience, the simplest way to check for results is to grab the 0th element of the list and see if it's null.
Here is your code sample with a few tweaks. Notice that I've changed the type of defaultItem from XMLList to XML, and I'm assigning it to the 0th element of the list.
var defaultItem:XML =
DataModel.instance.masonicXML.item.(#style_number == styleNum)[0];
if( defaultItem != null )
{
DataModel.instance.selectedItem = defaultItem;
}
OK I got it to work with this:
if(String(defaultItem.#style_number).length)
Matt's null check is a good solution. (Unless there is the possibility of having null items within an XMLList.. probably not, but I haven't verified this.)
You can also check for the length of the XMLList without casting it to a String:
if (defaultItem.#style_number.length() > 0)
The difference to String and Array is that with an XMLList, length() is a method instead of a property.

Resources