I have a class called :customer. It has ID, firstname, lastname, phone number, etc.
I have a stored proc that gets ID, firstname,lastname, phone. It doesnt get ALL the properties of customer ..just the ones I need.
I have the dataset below to read the stored proc results. How can I populate this class, I triedbut got an error that the property is read only.
foreach (DataRow row in ds.Tables[0].Rows)
{
test.EmailAddress = row[0].ToString();
}
do you have properties defined with public get and set methods?
Camillia
ASKER
I just looked at it...it only has "get" property...For example:
public string NamePrefix
{
get { return GetValue(Columns.NamePrefix); }
}
Let me ask you this..if I can get this one , then I'm set:
You see the dataset above..anyway to pouplate a List with a dataset..
My list is like this:
List customerMatches = new List();
I tried: customerMatches.Add(ds.row[0].tostring()); -- this of coruse doesnt work...
--- I dont know how to do that...that's why I did:
Customer test = new Customer();
Then wanted to pouplate this Customer class,
then wanted to do customerMatches.Add(test);
another option is to set up a constructor in your customer class that takes a datareader. you can convert that table to a datareader and pass it in to the constructor.
of course you will still have to do the same thing as above and assign all of the fields in the reader to property values.
Yep, that would work, it's beautiful :) and It's not breaking the ex-developers code.
Just one more thing. One of the class properties is an ID. It's defined as GUID. I added the "set" to it:
public Guid Id
{
get { return GetValue(Columns.Id); }
set { _ID = value; }
}
But now, when I want to assign the ID value from dataset, I cant do this because ID is not a string.
test.ID = row[2].ToString();
--I created below but that wont work. "ID" is used all over the code. I have to use ID. How can I do this??
public string cID
{
set { _ID = value; }
}
something strange with "set" and "get".
I get the list populated. I right click on the list and i see the Customer items. I see for example, lets take the lastname. I have:
1.public string LastName
{
get { return GetValue(Columns.LastName); }
set { _lastName = value; }
}
2. I get the dataset and do this:
Customer customerInfo = new Customer();
customerInfo.LastName = row[3].ToString();
3. After the list is populated, i look at one row. I see a value for : _lastName but LastName is null.
Same for all the properties. For example, for "id" which is a GUID (see above)..I see _ID as having a value but ID as having all zeroes.
This wont work when I bind the list. Shouldnt "ID" and "_ID" both have the same value? My code looks for value in "ID" column not in "_ID" column..
you know what I'm explaining? The orig ex-developer code works and 'ID" or "LastName" properties are populated. But when I added the "sets" , they dont get populated. Only the private members are populated...
I'm going home now but will check and response to your answer.
Camillia
ASKER
I think this is happening:
When I set the LastName, the value goes to the variable _lastName. But I am trying to retrieve it back from Column.LastName .
I dont want to change the "get" properties. Might break ex-developer's code.
How can I save it _lastname into LastName??
p_davis
well, i am not sure that i understand fully. _lastName will be what ever you assign to customer.LastName.
public String LastName
{
get{return _lastName;}
set{_lastName = value;}
}
Camillia
ASKER
yeah, the "get" is ex-developer's code and i didnt change that. He has some fancy code and i dont want to change his "get" ...i know this class is used in other places..
--Anyway to have a flag to say... use this "ex developer's GET" if flag=OLD. Use "My GET" if flag=New
I want to keep his "get" but also add my "get"...
p_davis
you would have to create a new property to do that. whether it be in this class or in a new one.
do you have properties defined with public get and set methods?