0

I need my app to get a true / false statement from database & do something with it.

In my Database i have a row called "ban" wich is TinyInt(1) for a true / false statement.

If a user types his license name & license number and clicks a button, my app wil compare the input data with specific rows in my database. So if ok = continue, if not ok, show error.

Also the true/false statement of the inserted input will then be added as a ListBox item.

Now i got that working, but actually i need my app to do something with true or false.

So what i need is to get the true or false from the inserted input, & then use it to say "if false then open new window if true show an error inside a TextBox"

Showing the errors is not a big deal, i got that working. The main thing here is using true or false to either continue or don't.

I know it has something to do with Boolean, i just don't know how to work with it.

Here's what i got so far: (not the complete code but i think this will do)

Thanks!

    public partial class MainWindow : Window
{
    string ConnectionString = Properties.Settings.Default.cloudpos_beheerCS;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        string Naamtxt = Naam.Text;
        string Licentietxt = Licentie.Text;

        MySqlConnection conn = new MySqlConnection(ConnectionString);
        MySqlCommand cmdDatabase = conn.CreateCommand();
        MySqlDataReader rdr;
        cmdDatabase.CommandText = "SELECT * FROM cloudposgebruikers WHERE licentie = @licentie AND naam = @naam";
        cmdDatabase.Parameters.AddWithValue("@naam", Naamtxt);
        cmdDatabase.Parameters.AddWithValue("@licentie", Licentietxt);

        try
        {
            conn.Open();
        }
        catch (Exception)
        {
            ErrorVerbinding.Content = "Er kan geen verbinding gemaakt worden met de database.";
            return;
        }

        rdr = cmdDatabase.ExecuteReader();
        int ollema = rdr.GetOrdinal("ban");
        while (rdr.Read())
            ListBox1.Items.Add(rdr.GetString(ollema));

            conn.Close();
        }

    }
DeMama
  • 1,134
  • 3
  • 13
  • 32
  • If you're asking what I _think_ you're asking, changing `int ollema = rdr.GetOrdinal("ban");` to `bool ollema = rdr.GetOrdinal("ban") != 0;` should work...? – Joachim Isaksson Jan 13 '13 at 10:01
  • Ok thanks. But here is the part where i'm lost.. How i can use the returned value to either say "clear to go" or "i got a true so show an error" – DeMama Jan 13 '13 at 10:08
  • You just want to know how to write an `if` statement? – PhoenixReborn Jan 13 '13 at 10:36
  • @PhoenixReborn haha no :D. i don't know how to implement the bool value in an if statement. like: if (bool value retrieved from db = false) then continue or: else if (bool value retrieved from db = true) then show error – DeMama Jan 13 '13 at 10:49
  • @DeMama Exactly like that. `if () { show error }` That is how you write and `if` statement.... – PhoenixReborn Jan 13 '13 at 10:53
  • @PhoenixReborn Yes i know that.. its the thing i have to put next to if (ex: if (rdr.HasRows)) but then for bool.. Seems i really can't get it to work.. (i'm a newb, can't blame me i guess) – DeMama Jan 13 '13 at 10:58
  • @DeMama - Well I don't know what to tell you. Joachim and I have both told you what it is that you are asking and you say you already know. So I am totally confused on what your real question is. – PhoenixReborn Jan 13 '13 at 11:03
  • @PhoenixReborn Well me neither i can't see the trees trough the forrest anymore.. I just need to know what code i need to write next to "if" (like my example i say: " if (rdr.HasRows)) what do i need to write next to " if " to use the returned bool value? I'm searching for days now lol – DeMama Jan 13 '13 at 11:10
  • @DeMama - You are making this way harder than it really is. To use your example, again, `if (ollema) { show error }` Or if you really like typing `if (ollema == true) { show error }`. – PhoenixReborn Jan 13 '13 at 11:15
  • @DeMama: Instead of editing the title to contain `[SOLVED]` you should accept the post who best answered your question (by clicking the outlined checkmark). If you find the answer yourself, you may answer your own question as well. See the [faq] for details. – knittl Jan 13 '13 at 12:19
  • Please DO NOT add [Solved] to the title. If one of the answers helped you select the tick/check mark beside the answer to accept it. As you found the solution yourself post that as answer the question yourself and accept that (after the prescribed delay) rather than edit it into the question. – ChrisF Jan 13 '13 at 12:21
  • @knittl Well i tried but yes, the delay lol.. – DeMama Jan 13 '13 at 12:26

2 Answers2

2

Ok i found the solution. I didn't need to make a bool for the task, just 2 lines of code and it works. Thanks anyway guys !

        if (rdr.HasRows)
        {
            while (rdr.Read()) //Make sure this is added or it won't work.
            {
                if (rdr.GetBoolean("ban"))
                {
                    ErrorSuspend.Text = "Uw licentie is verlopen of geblokkeerd.                      Contacteer uw verdeler om een nieuwe licentie te bekomen.";
                    return;
                }

                else
                {
                    Login Login = new Login();
                    this.Content = Login;
                }
            }
        }
        else
        {
            ErrorLN.Content = "Licentie of naam incorrect.";
            return;
        }
    }
DeMama
  • 1,134
  • 3
  • 13
  • 32
  • @DeMame good to see you solve your problem based on my post :o) – WiiMaxx Mar 15 '13 at 15:44
  • @WiiMaxx hehe, actually i solved this question 2 months ago :P [Here](http://stackoverflow.com/a/15436063/1968367) is the answer ;) Thx again ! – DeMama Mar 15 '13 at 15:51
0

Try this :

bool ollema = (bool)rdr.GetValue(Index OF Ban Field);

while (rdr.Read())
           ListBox1.Items.Add(rdr.GetString(ollema));
            conn.Close();
        }
aEk BKF
  • 131
  • 1
  • 2
  • 15