向数据库提交数据时未显示消息框

时间:2014-08-15 05:39:36

标签: c# mysql

当我填写注册表单然后如果数据正确则单击提交它已成功添加到数据库但它没有显示消息"注册成功完全"

这是我的提交按钮

private void Submit_btn_Click(object sender, EventArgs e)
    {
        string phonetext = Phone_txt.Text;
        string myregex = "[\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d]";  

        Match look = Regex.Match(phonetext, myregex);

        if (UserName_txt.Text != "" && mask1_password_txt.Text != "" &&Email_txt.Text!= "" && Phone_txt.Text != "" && FName_txt.Text != "" && LName_txt.Text != "" &&Address_txt.Text != "")
        {


            if (look.Success && Phone_txt.Text.Length == 10) // if regex success & phone number has 10 digits
            {
                 Classes.Quaries qu = new Classes.Quaries();
                qu.insertIntoSignup(UserName_txt.Text, mask1_password_txt.Text, FName_txt.Text, LName_txt.Text, Address_txt.Text, Phone_txt.Text, Email_txt.Text);
            }
            else
            {
                MessageBox.Show("Invalid Phone Number");
            }
        }
        else
        {
            MessageBox.Show("All Fields Must Be Filled ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

这是方法

public void insertIntoSignup(string username, string password, string firstname, string lastname, string address, string phone, string email )
    {
        try
        {

            sql_quary = "Insert Into my_project_data.signup(UserName,Password,FirstName,LastName,Address,Phone,Email) Values('" + username + "','" + password + "','" + firstname + "','" + lastname + "','" + address + "','" + phone + "','" + email + "')";
            string dbuser = "Select UserName From my_project_data.signup where UserName = '" + username + "'";  //check whether username already excistes

            openConnection();
            cmd = new MySqlCommand(dbuser, conn);
            datar = cmd.ExecuteReader();
            int count1 = 0;

            while (datar.Read())
            {
                count1 += 1;
            }
            datar.Close();


            if(count1 == 1)
            {
                System.Windows.Forms.MessageBox.Show("UserName Already Exists Please Choose Another UserName");
            }
            else if (count1 == 0)
            {
                cmd = new MySqlCommand(sql_quary, conn);
                datar = cmd.ExecuteReader();

                while (datar.Read())
                {
                    System.Windows.Forms.MessageBox.Show("Registration Sucessfull "); //This message is not showing
                }
                datar.Close();
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Something is terribally wrong");
            }

        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Ooooops Error " + ex.Message);
        }

        finally
        {
            closeConnection();
        }
    }

1 个答案:

答案 0 :(得分:0)

你是一个混乱的伙伴。您正在使用Insert语句执行阅读器。

 cmd = new MySqlCommand(sql_quary, conn);
 datar = cmd.ExecuteReader();

其中sql_quary是

 sql_quary = "Insert Into my_project_data.signup(UserName,Password,FirstName,LastName,Address,Phone,Email) Values('" + username + "','" + password + "','" + firstname + "','" + lastname + "','" + address + "','" + phone + "','" + email + "')";

您需要首先通过

执行插入语句
cmd.ExecuteNonQuery();

就目前而言,您正在尝试读取insert语句的结果,在这种情况下,这不是什么

while (datar.Read())
{
    System.Windows.Forms.MessageBox.Show("Registration Sucessfull "); 
}

永远不会执行。

我也有这个方法

public void insertIntoSignup(string username, string password, string firstname, string lastname, string address, string phone, string email )

将一个Result变量返回给你的Ui并在那里执行你的消息。即返回int或甚至更好的枚举:

public int insertIntoSignup(string username, string password, string firstname, string lastname, string address, string phone, string email )

然后根据该值在Ui中显示一条消息或另一条消息。

将消息放入while循环意味着如果您的读者返回多个结果(行),则消息将一直弹出,每行一次。可能不是你想要的。