Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

How can I populate this class?

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();
DataSet ds;
        Customer test = new Customer();
       ds = new DataSet();     
        using (SqlConnection conn = Deck.Core.Database.NewConnection())
            using (SqlCommand cmd = conn.CreateCommand())
                SqlDataAdapter myAdapter = new SqlDataAdapter();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SearchLastName";
                cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = customerName;
                    myAdapter.SelectCommand = cmd;
                    foreach (DataRow row in ds.Tables[0].Rows)
                        //test.EmailAddress = row[0].ToString();
                        //test.FirstName = row[1].ToString();
                        //test.Id = row[2].ToString();
                        //test.LastName = row[3].ToString();
                        //test.MiddleName = row[4].ToString();
                        //test.NamePrefix = row[5].ToString();
                        //test.AlternatePhoneNumber = row[6].ToString();
                        //test.PrimaryPhoneNumber = row[7].ToString();

Open in new window

Avatar of p_davis

whats in that class.

do you have properties defined with public get and set methods?
Avatar of Camillia


I just looked at 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);

Avatar of p_davis

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
also -- to do this you will have to put the set in because you will be assigning values.
i meant to say to do either option. you need to add the sets.
Let me try and see. Dont want to change the code that much. Might break something else and I'm new to this code.
i'm here if you need more help.
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; }
you will probably have to cast it

test.ID = (GUID) row[2];
I tried this: test.Id = (Guid)row[2].ToString();
but didnt work
yeah, just saw your msg. Thanks so much for your help/ Kamila.
not a problem
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.
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??
well, i am not sure that i understand fully. _lastName will be what ever you assign to customer.LastName.
the basic format of the get/set properties is...

private string _lastName

public String LastName
      get{return _lastName;}
      set{_lastName = value;}
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"...
you would have to create a new property to do that. whether it be in this class or in a new one.
yeah, thanks. This was very helpful.
Thanks so much for your help.
thank you.  Good Luck