Adding attributes to validator programmatically - c#

I'm building a function to allow me to add validators into dynamically created tables. They work, in that they show up on the page and function properly. But I'm trying to add the "Display" attribute via the codebehind, and any combination of parameters fails...
RequiredFieldValidator newRQValid = new RequiredFieldValidator();
newRQValid.SetFocusOnError = true;
newRQValid.ControlToValidate = txtID;
newRQValid.Display = "dynamic"; <<---
strID = "cv" + cellID;
newRQValid.ID = strID;
newRQValid.ErrorMessage = txtErrorMessage;
newRQValid.InitialValue = initval;
tCell.Controls.Add(newRQValid);
I've tried with and without quotes, but "Dynamic" doesn't even appear in the autocomplete, so I'm assuming I'm just plain mistaken.
I have similar issues adding attributes to a compare validator as well:
CompareValidator newCMValid = new CompareValidator();
newCMValid.SetFocusOnError = true;
newCMValid.ControlToValidate = cellID;
newCMValid.ControlToCompare = "txt_clm_dob";
newCMValid.Type = ValidationDataType(DateTime); <<==
newRGValid.Display = Dynamic; <<==
strID = "cv" + cellID;
newCMValid.ID = strID;
newCMValid.ErrorMessage = txtErrorMessage;
newCMValid.Operator = LessThanEqual; <<==
tCell.Controls.Add(newCMValid);
With several attempts on each of those as well.
So what's the right syntax there, or is adding those attributes somehow not allowed here?

newRQValid.Display = ValidatorDisplay.Dynamic;
newCMValid.Type = ValidationDataType.Date;
newCMValid.Operator = ValidationCompareOperator.LessThanEqual;

Related

how to remove en element from an array by using where(..).Select(..) in c#?

In c#, I am getting report parameters of a report (Microsoft Reporting Services) by using:
public ItemParameter[] GetServerReportParameterInfo(string rdlName)
{
//var startTime = DateTime.Now;
var reportPath = "/" + AppUtil.GetAppSettingValueByKey(Constants.ReportFolder) + "/" + rdlName.RemoveEnd(4);
const bool forRendering = false;
ItemParameter[] parametersItems;
var trustedUserHeader = new TrustedUserHeader();
var rs = new ReportingService2010SoapClient("ReportingService2010Soap");
if (rs.ClientCredentials != null)
rs.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
rs.GetItemParameters(trustedUserHeader, reportPath, null, forRendering, null, null, out parametersItems);
return parametersItems;
}
...
ItemParameters[] parameterCollection = GetServerReportParameterInfo(rdl);
It is returning the parameters correctly to me, however, there are some parameters whose .prompt property is null. I need to remove them from parameterCollection. I need a code like below, but could not find the correct syntax. Any help would be appreciated.
parameterCollection = parameterCollection.Where(o=>!String.IsNullOrEmpty(o.Prompt)).Select(o=>o);
Linq methods do not allow to modify collection/sequence (whether it is an array, list or any other collection).
You can create new filtered collection (which looks like an option in your case as you re-assigning collection):
parameterCollection = parameterCollection
.Where(o=>!String.IsNullOrEmpty(o.Prompt)) // filter
.ToArray(); // convert to new array
If you must use .Select you can add no-op select as in your question .Selsect(x=>x), but it does not give any benefits.

Filtering Google Calendar events with .C# API

I try to filter Google Calendar events by date in a .Net program.
I found resources only in other environnements.
The following code retrieves all events. The optional parameters are not used :-(
I think I do not send them to Google at all. I do not understand the "ParameterType" use and found no list of valid values. I simply copied the original parameter "ParameterType" value.
Any help will be apreciated!
var rq = service.Events.List( "primary" );
string startRFC3339 = "2017-04-19T00:00:00Z";
string endRFC3339 = "2017-04-22T00:00:00Z";
rq.RequestParameters["timeMin"] = new Google.Apis.Discovery.Parameter() {
Name = "timeMin",
ParameterType = "query",
DefaultValue = startRFC3339
};
rq.RequestParameters["timeMax"] = new Google.Apis.Discovery.Parameter() {
Name = "timeMax",
ParameterType = "query",
DefaultValue = endRFC3339
};
var events = requete.Execute();
I found the answer: do not focus on the RequestParameters collection.
It describes allowed parameters. It doesn't contain the parameters values.
Those values are in the request object itself:
var rq = service.Events.List( "primary" );
rq.TimeMin = aDateTime;
rq.TimeMax = aDateTime.AddDays(1);
var events = rq.Execute();
So simple...
.Net is strongly typed. I should have looked there first.

Adding CompareValidator to dynamically added text box

I have a form that needs dynamic input boxes that have to be integers. I used a slightly modified version of the code found here to do that: http://www.learning2code.net/Learn/2009/8/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx
I have a the following code to add to the placeholder:
CompareValidator cmpVal = new CompareValidator();
cmpVal.ID = "cv" + textboxID;
cmpVal.ControlToValidate = textboxID;
DynamicTextBoxIntegerValidation.Controls.Add(cmpVal);
Obviously this is missing two very important pieces; the Type and Operator fields. The problem is I can't figure out how to add them. Any help would be appreciated.
Type and Operator are simply properties of CompareValidator. You can add them as follows:
CompareValidator cmpVal = new CompareValidator();
cmpVal.ID = "cv" + textboxID;
cmpVal.ControlToValidate = textboxID;
cmpVal.Type = ValidationDataType.Integer; //Set your type and operator here.
cmpVal.Operator = ValidationCompareOperator.Equal;
DynamicTextBoxIntegerValidation.Controls.Add(cmpVal);

Checkbox SelectedItem Text to Linq Database

I built an ASP.net usercontrol and I am trying to get the text from the checkbox to pass to the database. My problem is I keep getting an error on submission with the method I use. Here is what I used:
ResidenceHallInspection rhi = new ResidenceHallInspection();
rhi.versionId = version.id;
rhi.submitDate = DateTime.Now;
rhi.CheckInOrOut = ddlCheck.SelectedItem.Text;
rhi.StudentId = txtStudentId.Text;
rhi.FirstName = txtFirstName.Text;
rhi.MiddleName = txtMiddleName.Text;
rhi.LastName = txtLastName.Text;
rhi.Walls = chbxWalls.SelectedItem.Text;
rhi.Windows = chbxWindows.SelectedItem.Text;
rhi.Blinds = chbxBlinds.SelectedItem.Text;
rhi.Couch = chbxCouch.SelectedItem.Text;
rhi.CommonRoomCouch = chbxCRCouch.SelectedItem.Text;
rhi.CommonRoomChair = chbxCRChair.SelectedItem.Text;
rhi.Doors = chbxDoors.SelectedItem.Text;
rhi.Carpet = chbxCarpet.SelectedItem.Text;
rhi.Ceiling = chbxCeiling.SelectedItem.Text;
rhi.CommonRoomCounter = chbxCRCounter.SelectedItem.Text;
rhi.Cabinet = chbxCabinet.SelectedItem.Text;
rhi.Phone = chbxPhone.SelectedItem.Text;
rhi.Bed = chbxBed.SelectedItem.Text;
rhi.Desk = chbxDesk.SelectedItem.Text;
rhi.DeskChairs = chbxDeskChair.SelectedItem.Text;
rhi.Tub = chbxTub.SelectedItem.Text;
rhi.Vanity = chbxVanity.SelectedItem.Text;
rhi.Notes = txtNotes.Text;
rhi.Building = txtResHall.Text;
rhi.ApartmentNumber = txtSuitNo.Text;
rhi.BedSpace = txtBedSpace.Text;
house.AddToResidenceHallInspections(rhi);
house.SaveChanges()
It would be good to see more information, such as the error you received, and possible the code and/or HTML for the custom user control. However, there are a couple of possibilities which seem likely. One is that your SelectedItem property may be null, which would cause an ObjectReferenceException to occur. Another is that Text itself is null, and that your database doesn't allow a null value for one or more of the fields you're assigning.
You can get a better answer than this rather vague one if you provide more information.

Magento API v2 and C# - set custom attributes whilst adding product

I have added custom attribute with the code "my_price" with "Catalog Input Type for Store Owner" set to "Price" and assigned it to the "Default" (only) attribute set.
Now, I want to set its value, everytime I add/update product with API v2 (C#). Here is the code which does not work (the value is not being set):
// Connect & Auth:
Mage_Api_Model_Server_V2_HandlerPortTypeClient handler = new Mage_Api_Model_Server_V2_HandlerPortTypeClient();
session_id = handler.login(username, api_key);
// Getting attributes set:
catalogProductAttributeSetEntity[] attributeSets;
attributeSets = handler.catalogProductAttributeSetList(session_id);
attributeSet = attributeSets[0];
string attributeset_id = attributeSet.set_id.ToString();
// Adding product:
catalogProductCreateEntity mageProduct = new catalogProductCreateEntity();
// (...) setting product's name, sku, etc.
associativeEntity AdditionalAttributes = new associativeEntity();
AdditionalAttributes.key = "my_price";
AdditionalAttributes.value = "12,33";
associativeEntity[] AssociativeEntity = new associativeEntity[1];
AssociativeEntity[0] = AdditionalAttributes;
mageProduct.additional_attributes = AssociativeEntity;
handler.catalogProductCreate(session_id, "simple", attributeset_id, sku, mageProduct, "default");
What am I doing wrong?
Magento 1.6.1.0 has a bug which results with additional attributes error.
I have upgraded my Magento to 1.6.2.0 and the problem disappeared and additional attributes works perfectly.
Quick example of how it works:
associativeEntity[] AdditionalAttributes = new associativeEntity[1];
associativeEntity AdditionalAttribute = new associativeEntity();
AdditionalAttribute.key = "myprice";
AdditionalAttribute.value = getOriginalPrice(prices).ToString();
AdditionalAttributes[0] = AdditionalAttribute;
catalogProductAdditionalAttributesEntity AdditionalAttributesEntity = new catalogProductAdditionalAttributesEntity();
AdditionalAttributesEntity.single_data = AdditionalAttributes;
mageProduct.additional_attributes = AdditionalAttributesEntity;
Hope it helps someone.
Try this and let me know the result.
AdditionalAttributes.key = "myPrice";
handler.catalogProductCreate(session_id, "simple", attributeset_id, sku, mageProduct, "default");
Give valid storeview instead of default e.g. try this:
handler.catalogProductCreate(session_id, "simple", attributeset_id, sku, mageProduct, "1");

Categories

Resources