2

The problem is, I need to show a list in my application.

I made a SQL int type for IsAdmin as I was asked, and in school they told me to make that int show bool in application, more precisely to show IsAdmin = True or False?

But when I start application and when I pass the login screen the exception shows with message:

"Specified cast is not valid!"

What I need to do?

  • I tried converting this (bool)reader["IsAdmin"].ToInt32, but it shows error.

  • I tried changing it to integer but then it shows me 1's and 0's.

        public static List<Korisnik> GetAllUsers() {

            List<Korisnik> korisnici = new List<Korisnik>();
            Korisnik korisnik = null;

            using (SqlConnection conn = new SqlConnection()) {

                conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
                conn.Open();

                SqlCommand command = new SqlCommand("SELECT ID, UserName, UserPass, IsAdmin FROM Users01", conn);

                using (SqlDataReader reader = command.ExecuteReader()) {

                    while (reader.Read()){

                        korisnik = new Korisnik((int)reader["ID"], (string)reader["UserName"], (string)reader["UserPass"], (bool)reader["IsAdmin"]);
                        korisnici.Add(korisnik);

                    }

                }

            }

            return korisnici;

        }
xVenum.dll
  • 73
  • 5
  • 2
    Paste your code not pictures, its easier for everyone – TheGeneral Mar 31 '19 at 01:06
  • 1
    What are the datatypes in the database? – Ernesto Gutierrez Mar 31 '19 at 01:08
  • @MichaelRandall Done. – xVenum.dll Mar 31 '19 at 01:09
  • 1
    You don't actually store the user password in the database, do you? – Enigmativity Mar 31 '19 at 01:09
  • @ErnestoAndresGutierrez OP has already indicated DB type is _"int[eger]"_ –  Mar 31 '19 at 01:09
  • @ErnestoAndresGutierrez UserName is NVarChar, same as UserPass, but IsAdmin is an int. If it's that you asked me. Sorry for my bad english, I started learning programming 6 months ago. – xVenum.dll Mar 31 '19 at 01:10
  • @Enigmativity The user password is inside that db, yes, they told me that in school to do like that.. – xVenum.dll Mar 31 '19 at 01:11
  • @MickyD I can post a whole code from this tab if you want to check fields, properties, constructors? – xVenum.dll Mar 31 '19 at 01:15
  • Can you try `CAST(IsAdmin AS BIT) AS IsAdmin` in the SqlCommand. – Arulkumar Mar 31 '19 at 01:15
  • @Arulkumar, I must do from constructors.. – xVenum.dll Mar 31 '19 at 01:17
  • 1
    @xVenum.dll - You might need to find a new school. You shouldn't store passwords anywhere. You should always hash passwords with a salt and then only ever store the hash. That way if your database is compromised no-one can work out what the password is. – Enigmativity Mar 31 '19 at 01:19
  • @Enigmativity Lol, I know, I'm feeling like I didn't learn anything, I'm only learning theoretical part of programming I have tiny bits of actually programming, I learnt from you that passwords mustn't be stored into databases, in my school we learn like make user, make pw, and all that stuff and show it. Sorry for bad english, I hope you understand me. – xVenum.dll Mar 31 '19 at 01:24

1 Answers1

4

Change this line:

korisnik = new Korisnik((int)reader["ID"], 
                        (string)reader["UserName"], 
                        (string)reader["UserPass"], 
                        (bool)reader["IsAdmin"]);

...to:

korisnik = new Korisnik((int)reader["ID"], (string)reader["UserName"], 
                        (string)reader["UserPass"], 
                        ((int)reader["IsAdmin"]) > 0 ? true : false ); // convert from int to bool

The conversion is necessary because you can't just assign an int to a bool. I am assuming that in the database, anything greater than 0 means that the user is an administrator.

I know it is a homework question, but going forward, it is much better to use boolean types in the database (in this case BIT) because

  1. they take less room in the database (more efficient)
  2. makes it more apparent what the column's purpose is
  3. eliminates conversion problems such that posted in the question

Tell me more about BIT