簡體   English   中英

消息框不顯示

[英]Messagebox doesn't show

我已經使用mysql創建了一個數據庫,在form1上有兩個文本框,分別是用戶名和密碼以及一個登錄按鈕。 我的程序從數據庫中檢索信息,如果數據s與之匹配,則登錄,否則將彈出一個消息框,提示密碼用戶名錯誤(該密碼不顯示)

在發布代碼之前:我已經聲明:

public int logid;
public int loginid(strign name) // to set logid the id of the user
public void loginfun();        //  checks the data and logs in if id and password matches, else should give an error message.

我的代碼如下:

public int loginid(string name)
    {
        string conString = "Server=localhost;Database=ozturk;Uid=_____;pwd=_____";
        MySqlConnection mcon = new MySqlConnection(conString);
        string getid = "SELECT username,id from ozturk.admin WHERE username='" + name + "'";

        MySqlCommand cmd = new MySqlCommand(getid, mcon);
        MySqlDataReader myReader;

        mcon.Open();
        myReader = cmd.ExecuteReader();
        while (myReader.Read())
        {
            if (myReader["username"].ToString() == name)
            {
                return Convert.ToInt32(myReader["id"].ToString());
            }
        }
        return 0;

    }


public void loginfun()
    {
        string conString ="Server=localhost;Database=ozturk;Uid=_____;pwd=_____";
        MySqlConnection mcon = new MySqlConnection(conString);
        string selectCommand = "SELECT * FROM ozturk.admin";
        MySqlCommand cmd = new MySqlCommand(selectCommand,mcon);

        MySqlDataReader myReader;
        mcon.Open();
        myReader = cmd.ExecuteReader();

        while (myReader.Read())
        {
            try
            {
                if (myReader["username"].ToString() == txtuserid.Text && myReader["password"].ToString() == txtpassword.Text)
                {
                    // set logid to userid
                    logid = loginid(myReader["username"].ToString());
                    string updateCommand = "UPDATE ozturk.admin SET status = 'on' WHERE id='" + logid + "' ";

                    MySqlConnection newcon = new MySqlConnection(conString);
                    MySqlCommand cmd2 = new MySqlCommand(updateCommand, newcon);
                    MySqlDataReader myReader2;
                    newcon.Open();
                    myReader2 = cmd2.ExecuteReader();

                    Anasayfa anasayf = new Anasayfa();
                    anasayf.Show();
                    this.Hide();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Username or Password is incorrect");
                throw;
            }
        }
    }

問題是:如果用戶名和密碼正確,我的程序登錄並打開其他表格,但是如果用戶名或密碼錯誤,它什么也不做,我在這里錯過了什么? 任何幫助表示贊賞,謝謝

我已經解決了問題,這是我已經完成的工作:我將登錄功能從void更改為bool:這是最新的代碼:

public bool loginfun()
    {
        string conString ="Server=localhost;Database=ozturk;Uid=_____;pwd=_____";
        MySqlConnection mcon = new MySqlConnection(conString);
        string selectCommand = "SELECT * FROM ozturk.admin";
        MySqlCommand cmd = new MySqlCommand(selectCommand,mcon);

        MySqlDataReader myReader;
        mcon.Open();
        myReader = cmd.ExecuteReader();

        while (myReader.Read())
        {
            try
            {
                if (myReader["username"].ToString() == txtuserid.Text && myReader["password"].ToString() == txtpassword.Text)
                {
                    // giriş yapan kişinin id si
                    logid = loginid(myReader["username"].ToString());
                    string updateCommand = "UPDATE ozturk.admin SET status = 'on' WHERE id='" + logid + "' ";

                    MySqlConnection newcon = new MySqlConnection(conString);
                    MySqlCommand cmd2 = new MySqlCommand(updateCommand, newcon);
                    MySqlDataReader myReader2;
                    newcon.Open();
                    myReader2 = cmd2.ExecuteReader();


                    return true;
                }

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                throw;
            }
        }
        return false;

    }

在登錄按鈕中,代碼如下:

if (loginfun() == true)
        {
            Anasayfa anasayf = new Anasayfa();
            anasayf.Show();
            this.Hide();
        }
        else
        {
            MessageBox.Show("Username or Password is incorrect");
        }

謝謝大家的提示

這是您的代碼:

 bool flag = false;
    while (myReader.Read())
            {
                try
                {
                    if (myReader["username"].ToString() == txtuserid.Text && myReader["password"].ToString() == txtpassword.Text)
                    {
                        // set the flag to true, is credentials match and break from the loop
                        flag = true;
                        break;
                        // set logid to userid
                        logid = loginid(myReader["username"].ToString());
                        string updateCommand = "UPDATE ozturk.admin SET status = 'on' WHERE id='" + logid + "' ";

                        MySqlConnection newcon = new MySqlConnection(conString);
                        MySqlCommand cmd2 = new MySqlCommand(updateCommand, newcon);
                        MySqlDataReader myReader2;
                        newcon.Open();
                        myReader2 = cmd2.ExecuteReader();

                        Anasayfa anasayf = new Anasayfa();
                        anasayf.Show();
                        this.Hide();
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Username or Password is incorrect");
                    throw;
                }
            }
            if(!flag)
            {
                MessageBox.Show("Username or Password is incorrect");
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM