Link to home
Create AccountLog in
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;
        }
ASKER CERTIFIED SOLUTION
Avatar of molku
molku

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of nsanga
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)
Avatar of myebay

ASKER

thank you