Outofrange exception when removing items form listview - c#-4.0

I have a listview which has multiple rows and check boxes is enabled I tried to wrote a code that remove item when I unchecked the item checkbox as it is checked by default
In the itemcheck event
If(list.Focused == true)
{
If (e.newvalue == checkState.unchecked)
{
list.items.removeAt( e.index);
list.Refresh();
}
}
I got a outofrange exception and 1 is not a valid for index
'1' refer to the index of the item I removed

You have to test if index is in range (because the collection shrink). Add this test before removing elements :
if(list.items.count < e.index && e.index > 0)

Here is the solution in check event you have current value and new value the new value not be applied till the check event is complete if you delete the item the class throw outofrange as it is try to assign a property of deleted item
You can use a list to add the items in and in the event checked remove items and clear the list

Related

How to trigger an action when deleting value from an autocomplete field on Apex 18.2

I have created an autocomplete item on my page and i want to trigger the change on this field. I am using Apex 18.2 and for the autocomplete item i have used a dynamic action with event Update [Text field with autocomplete].
This works fine when i change the value from the autocomplete and put another value from the list, but if i want to delete the value, the DA is not triggered.
I have tried also ojupdate custom event, but the same problem.
I have tried event change when item is null, but no success.
What i want to do is when deleting the value from the item (when item is null) to trigger an action.
Is there a DA that can help me on this particular issue?
Item: P37_ART
Type: Text field with autocomplete
Settings: Contains & Ignore Case
Lazy Loading: YES
Maximum values in List: 20
Item based on SQL Query and the format is number1||' - '||text1
Example: 1245 - Groceries
Dynamic Action:
Event: Update [Text field with autocomplete]
Action: Execute Javascript code
alert('test');
create another dynamic action thats has client side condition type javascript condition and check if your Text field is empty or is null using JS code, for example:
document.getElementById("textfield").value == "" || document.getElementById("textfield").value.length == 0 || document.getElementById("textfield").value== null

Querying Sharepoint - Value does not fall within the expected range

I'm attempting to retrieve the value of a listitem but keep getting a ArgumentException - Value does not fall within the expected range.
My code is as follows:
if (resultList.Count > 0)
{
SPListItem result = resultList[0];
if (result[Column] != null)
{
return result[Column].ToString();
}
}
In the immediate window I can verify the column does exist and the value can be found in the object tree structure.
result.Fields.GetField(Column).Id
returns a Guid but using it to retrieve the value of the Field results in another ArgumentException:
result[result.Fields.GetField(Column).Id]
This could happen if you get your list item collection from view (list.GetItems(view)) or from query with ViewFields property set, in this case only the fields included in ViewFields are returned.
You need to use InternalName of the field to get its value from SPListItem
result[result.Fields.GetField(Column).InternalName]

MissingRequiredFields property on SPListItem always returning true after initial validation

I am updating some list items through code.
Here is an example of what I am trying to do
SPListItem item = GetListItem();
item["Field1"] = GetField1ValueFromControl();
item["Field2"] = GetField2ValueFromControl();
item.Update();
if (!item.MissingRequiredFields)
{
SuccessRedirect();
}
else
{
Error("Fields missing");
}
In this example the Field2 is set as a required field, so if the user doesn't enter a value then it would show an error and they could enter a value.
The problem I seem to be having is that after the first error, even after they have entered a value for the required field the MissingRequiredFields property is still returning true after they have resubmitted the page
Any one got any ideas?
I worked this out.
You need to use the Page.IsValidated method to check the controls.
The item will always update whether the required fields are entered or not.
The MissingRequiredFields is not valid until after the update.

SPList Item get value - ArgumentException

I have an SPListItem and I have an array of column names.
When I try to access the SPListItem values using the code below:
for(int i=0;i<arrColName.length;i++)
{
string tempValue = item[arrColName[i]].ToString();
// Works fine in case the the specific column in the list item is not null
// Argument exception - Values does not fall witing expected range
// exception in case the value //is null
}
I think that you used an SPQuery to get the list items and forgot to add the field into the viewfields property of SPQuery.
query.ViewFields = string.Format("<FieldRef Name=\"{0}\" Nullable=\"True\" />", mFieldName);
Usually when you test your program with the farm account the code will work, with normal users you get an ArgumentException.
Another problem/feature which causes ArgumentException is the new ListView Threshold. If th elist you try to access has too many items, this Exception is raised. A way to handle this is to increase the threshold with powershell for the list.
Not only check if item != null but also item["FieldName"] != null. Because if you will try to call .ToString() on null, you will get exception.
And if that field with internal name "FieldName" name does not exist, you will also get an exception. So you would probably try
SPFieldCollection fields = list.Fields;
foreach (SPListItem item in list.Items) {
if (fields.Contains("FieldName") && item["FieldName"] != null) {
string fieldValue = item["FieldName"].ToString();
}
}
I had a similar situation with custom cascade field (or column). I did it following way and it seemed to work for the custom field types.
item.Properties["Country"] = "Mexico"; // custom field
item.Properties["nCity"] = "Cancun"; // custom field
item["Document Descriptions"] = "Test document description.";
Note: I added item.Properties for the custom columns. No need to add properties for built in field type (else they don't work).
Does your array contain the internal names or the display names of the columns? If it's the latter you might try item[item.Fields[arrColName[i]].InternalName].ToStrinng(); instead.
Sharepoint Lists aren't stored as a array with a static size.
You have to use the built in sharepoint iterator to go through each element
For example:
SPList checklist = //Some initiliaztion
foreach (SPListItem item in checklist.Items){
//work
}
This will do work on each item in your SPlist
Edit:
Wrong advice, I didn't see the code until after the edit.
Maybe try a cast?
(String)item[colname]

My look up column shows items but when created shows blank values

I have a list named "Projects" which has a number column named "Job Number",
I have attached an ItemAdded event handler to Job Number to increment.
Within the Projects list, I created a look up column named "Original Job" which looks up the value from the column Job Number.
When I click new item, I see the drop down values of all my Job numbers but when I choose one and click create item, there is no value, shows empty.
Here is my code:
SPList lookupList = currentSite.Lists["Projects"];
SPField lookupField = lookupList.Fields["Job Number"];
projectList.Fields.AddLookup("Original Job", lookupList.ID, false);
SPFieldLookup lookup = (SPFieldLookup)projectList.Fields["Original Job"];
lookup.LookupField = lookupField.InternalName;
lookup.Update();
Here is an image
http://pic100.picturetrail.com/VOL783/3746656/21927341/372654494.jpg
Can someone please explain why this occurs?
YOu need to call the Update method of the Item you're itself, not the SPFieldLookup lookup
variable, your code should look like this:
protected override ItemAdded(properties)
{
var item = web.Lists[properties.ListId].GetItemById(properties.ListItemId);
if (item != null)
{
if (item.Fields.ContainsField("Job Number"))
{
// get selectedValue from the original job??
item["Job Number"] = selectedValue;
item.Update();
//or item.SystemUpdate();
}
}
}

Resources