troubleshooting Question

DbConnection CreateCommand Stored Procedure window form

Avatar of myebay
myebay asked on
.NET ProgrammingC#
3 Comments1 Solution1330 ViewsLast Modified:
Hello,

I have a function that works but I need to change it in Stored Procedure called "viewAllUsers". Could you tell me what I need to change to make it works?

private List<User> GetUsersFromDB(DbConnection cnx)
        {
            List<User> users = new List<User>();

            using (DbCommand cmd = cnx.CreateCommand())
            {
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "SELECT * FROM UserTable";

                 using (DbDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        User user = new User();
                        user.UserID = reader["UserID"] == DBNull.Value ? default(int) : int.Parse(reader["UserID"].ToString());
                        user.UserName = reader["Name"] == DBNull.Value ? default(string) : reader["Name"].ToString();
                        user.UserPassword = reader["Password"] == DBNull.Value ? default(string) : reader["Password"].ToString();
                        user.UserGender = reader["Gender"] == DBNull.Value ? default(string) : reader["Gender"].ToString();
                        user.UserAge = reader["Age"] == DBNull.Value ? default(int) : int.Parse(reader["Age"].ToString());
                        user.UserGeneral = reader["General"] == DBNull.Value ? default(string) : reader["General"].ToString();
                        user.UserTDI_Type = reader["TDI_Type"] == DBNull.Value ? default(string) : reader["TDI_Type"].ToString();
                        user.UserPhilisophy = reader["Philisophy"] == DBNull.Value ? default(string) : reader["Philisophy"].ToString();
                        user.UserReligion = reader["Religion"] == DBNull.Value ? default(string) : reader["Religion"].ToString();
                        user.UserPolitics = reader["Politics"] == DBNull.Value ? default(string) : reader["Politics"].ToString();
                        user.UserUseFrequency = reader["UseFrequency"] == DBNull.Value ? default(int) : int.Parse(reader["UseFrequency"].ToString());
                        user.UserViewFrequency = reader["ViewFrequency"] == DBNull.Value ? default(int) : int.Parse(reader["ViewFrequency"].ToString());

                        users.Add(user);
                    }
                }
            }
            return users;
        }
ASKER CERTIFIED SOLUTION
molku

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 3 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 3 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