Avatar of myebay
myebay
 asked on

DbConnection CreateCommand Stored Procedure window form

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;
        }
.NET ProgrammingC#

Avatar of undefined
Last Comment
myebay

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
molku

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.
nsanga

check this......


http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx


the concept is same just use  DbConnection incase of SqlConnection ( same for command and reader)
myebay

ASKER
thank you
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