C# 向数据库提交数据时未显示消息框
当我填写注册表单,然后单击提交,如果数据正确,它将成功添加到数据库中,但它没有显示消息Registration success full 这是我的提交按钮C# 向数据库提交数据时未显示消息框,c#,mysql,C#,Mysql,当我填写注册表单,然后单击提交,如果数据正确,它将成功添加到数据库中,但它没有显示消息Registration success full 这是我的提交按钮 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]";
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();
}
}
你有点糊涂,伙计。您正在对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 + "')";
您需要首先执行insert语句,通常是通过
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 )
将结果变量返回到Ui并在那里执行消息。i、 e.返回一个整数或更好的枚举:
public int insertIntoSignup(string username, string password, string firstname, string lastname, string address, string phone, string email )
然后根据该值在Ui中显示一条或另一条消息
将消息放入while循环意味着,如果阅读器返回多个结果行,消息将持续弹出,每行一次。可能不是您想要的。在loopIt工作时将您的Messagebox放在外面谢谢您的帮助