Avatar of Camillia
Camillia
Flag 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;
               
 
                conn.Open();
 
                try
                {
                    myAdapter.SelectCommand = cmd;
                    myAdapter.Fill(ds);
                    
 
                    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

C#

Avatar of undefined
Last Comment
p_davis

8/22/2022 - Mon
p_davis

whats in that class.

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);

ASKER CERTIFIED SOLUTION
p_davis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
p_davis

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
p_davis

also -- to do this you will have to put the set in because you will be assigning values.
p_davis

i meant to say to do either option. you need to add the sets.
Camillia

ASKER
Let me try and see. Dont want to change the code that much. Might break something else and I'm new to this code.
Thanks.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
p_davis

i'm here if you need more help.
Camillia

ASKER
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; }
            }
p_davis

you will probably have to cast it

test.ID = (GUID) row[2];
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Camillia

ASKER
I tried this: test.Id = (Guid)row[2].ToString();
but didnt work
Camillia

ASKER
yeah, just saw your msg. Thanks so much for your help/ Kamila.
p_davis

not a problem
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
p_davis

the basic format of the get/set properties is...

private string _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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
yeah, thanks. This was very helpful.
Camillia

ASKER
Thanks so much for your help.
p_davis

thank you.  Good Luck

Perry
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy