troubleshooting Question

How can I populate this class?

Avatar of Camillia
CamilliaFlag for United States of America asked on
C#
22 Comments1 Solution241 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
p_davis

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 22 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 22 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros