Conversion from type 'Guid' to type 'String' is not valid.

Conversion from type 'Guid' to type 'String' is not valid.

Old forum URL: forums.lhotka.net/forums/t/6442.aspx


jasona22 posted on Tuesday, February 17, 2009

First, let me start by admitting I am a mud-farmer compared to most of you on this CSLA stuff. 

Here is what I am running into and perhaps one of you kind folks can speak mud-farmer well enough to explain how to resolve my issue.

I have a property that is the datafield of a combo box.  This property is of type GUID.  The business rule I am trying to implement is that a value is required to be selected.  I tried using StringRequired, but get the "conversion" error in the Subject line above.  Although I can type ".ToString()" and get a value -- kind of confounds the simple.

I am currently playing with a work around that does a regex against the guid, but as it turns out, an empty guid is still a properly formated guid. I will continue down this path for a time or perhaps I will create a "string wrapper" around the guid property that does a convert... but what a hack that feels like.

What I would love to know is if there is a simpler way to validate a Guid as a "StringRequired" or some such thing instead of the regex route.  The fact I can do a ".ToString" on the Guid property says that the above error is incorrect; yet it still fails.

Help?

Thank you

Mud-Farmer

ajj3085 replied on Tuesday, February 17, 2009

What are you using for the combobox's DataSource? 

jasona22 replied on Tuesday, February 17, 2009

The DataSource is bound progmatically to the business object.  So there is a "WorkOrder" class that is my business object and it is the DataSource, the properties of WorkOrder are what the various controls of the form are using as DataFields.

Just as a note, my validation rules for strings and numeric fields all work perfectly.  It is just the properties (DataFields) that have a type of Guid that are giving me a fit.

'MyString works

ValidationRules.AddRule(AddressOf Validation.StringRequired, "MyString")

ValidationRules.AddRule(AddressOf Validation.StringMaxLength, New Validation.MaxLengthRuleArgs("MyString", 15))

 

 

'OrgGuid does NOT work

ValidationRules.AddRule(AddressOf Validation.StringRequired, "OrgGuid")

 

 

Public Property OrgGuid() As Guid

Get

Return _supportorganizationguid

End Get

Set(ByVal value As Guid)

_orgguid = value

PropertyHasChanged()

End Set

End Property

 

Does this help?

DocJames replied on Wednesday, February 18, 2009


I would do a Custom rule:

private static bool OrgGuidRequired(object target, RuleArgs e)
{
if (((WorkOrder)target).OrgGuid == Guid.Empty)
{
e.Description = "Org is required";
return false;
}
return true;
}

and add the rule with:

ValidationRules.AddRule(OrgGuidRequired, "OrgGuid", 1);

I hope this can help.

Thanks,
Jimmy

Dave Boal replied on Friday, November 06, 2009

...to add on to the previous solution, I would add a project to your solution adding a class that can be called from any project:

namespace CodeQwik.Common.CustomValidationRules
{
public static class CustomRules
{
///
/// Rule ensuring a Guid value is not Guid.Empty
///
/// Object containing the data to validate
/// Arguments parameter specifying the name of the string
/// property to validate
/// if the rule is broken
///
/// This implementation uses late binding, and will only work
/// against Guid property values.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
public static bool GuidRequired(object target, RuleArgs e)
{
Guid value = (Guid)Utilities.CallByName(
target, e.PropertyName, CallType.Get);
if (value != Guid.Empty)
{
e.Description = string.Format("{0} must be assigned.", e.PropertyName);
return false;
}
return true;
}

Dave Boal replied on Friday, November 06, 2009

...to add on to the previous solution, I would add a project to your solution adding a class that can be called from any project:

namespace CodeQwik.Common.CustomValidationRules
{
public static class CustomRules
{
///
/// Rule ensuring a Guid value is not Guid.Empty
///
/// Object containing the data to validate
/// Arguments parameter specifying the name of the string
/// property to validate
/// if the rule is broken
///
/// This implementation uses late binding, and will only work
/// against Guid property values.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
public static bool GuidRequired(object target, RuleArgs e)
{
Guid value = (Guid)Utilities.CallByName(
target, e.PropertyName, CallType.Get);
if (value == Guid.Empty)
{
e.Description = string.Format("{0} must be assigned.", e.PropertyName);
return false;
}
return true;
}
}
}


... which could be called in your business object as follows:
#region Validation Rules
private void addCustomRules()
{
ValidationRules.AddRule(
CodeQwik.Common.CustomValidationRules.CustomRules.GuidRequired, "ID", 1);
}
#endregion //Validation Rules

Copyright (c) Marimer LLC