Hi,
I encountered a conversion problem in my .net web app (c#), and I hope
someone can help me:
I'm working on a generic way to store data in a database with a few
generated classes.
I want to loop through posted values, store them in properties in a
class, and then save the data with the class function "Update", which
by the way works fine.
Now I have a problem with converting the posted string values to the
datatypes of the specific class members.
For instance, I have a Contract table with it's Contract class. The
contract class has members for each column in the contract table, and
also methods to insert, update, select, and delete.
Asuming a membername/membervalue pair is posted, I want to search the
contract object for the specific member, convert the membervalue to the
correct datatype, and then store the converted value in the contract
member.
My code looks like this:
*************** *************** *************** *****
//Membername:
string MemberName = "Owner";
//Membervalue:
string MemberValue = "Egbert";
//Create "contract"objec t:
cContract objContract = new cContract();
Type type = objContract.Get Type();
//Reference to member of objContract:
PropertyInfo _member = type.GetPropert y(memberName);
if(_member != null)
{
//Convert value to datatype of member:
object _value = Convert.ChangeT ype(memberValue , _member.Propert yType);
_member.SetValu e(oObject,_valu e,null);
}
*************** *************** *************** *****
The implicit conversion results in this error message:
"Invalid cast from System.String to System.Data.Sql Types.SqlString "
Is there a way to do an Explicit conversion, using the
_member.Propert yType datatype?
Thanks in advance!
Egbert.
I encountered a conversion problem in my .net web app (c#), and I hope
someone can help me:
I'm working on a generic way to store data in a database with a few
generated classes.
I want to loop through posted values, store them in properties in a
class, and then save the data with the class function "Update", which
by the way works fine.
Now I have a problem with converting the posted string values to the
datatypes of the specific class members.
For instance, I have a Contract table with it's Contract class. The
contract class has members for each column in the contract table, and
also methods to insert, update, select, and delete.
Asuming a membername/membervalue pair is posted, I want to search the
contract object for the specific member, convert the membervalue to the
correct datatype, and then store the converted value in the contract
member.
My code looks like this:
*************** *************** *************** *****
//Membername:
string MemberName = "Owner";
//Membervalue:
string MemberValue = "Egbert";
//Create "contract"objec t:
cContract objContract = new cContract();
Type type = objContract.Get Type();
//Reference to member of objContract:
PropertyInfo _member = type.GetPropert y(memberName);
if(_member != null)
{
//Convert value to datatype of member:
object _value = Convert.ChangeT ype(memberValue , _member.Propert yType);
_member.SetValu e(oObject,_valu e,null);
}
*************** *************** *************** *****
The implicit conversion results in this error message:
"Invalid cast from System.String to System.Data.Sql Types.SqlString "
Is there a way to do an Explicit conversion, using the
_member.Propert yType datatype?
Thanks in advance!
Egbert.