出现错误时阻止按钮继续

编程入门 行业动态 更新时间:2024-10-28 08:15:41
本文介绍了出现错误时阻止按钮继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在出现错误时如何防止按钮继续显示?

How do i prevent a button to continue when there are an errors appears?

我已经可以检查数据库中用户名的可用性,但是即使该用户名不存在于数据库中,检查可用性"按钮仍会将该用户名识别为存在.

I already can check the availability of username in database, but even though the username is not exists in the database, the "Check Availability" button still recognized it as exists.

以下是屏幕截图:

上图显示用户名"Seranne"已经存在,但在数据库中,Seranne不存在.

Image above show the username "Seranne" already exists, but in the database, Seranne is not exists.

这是代码:

else if (textBox1.Text.Length > 0 && textBox2.Text == textBox3.Text) { label5.Visible = false; label7.Visible = false; conn.Open(); CheckUsername(); if (CheckUsername() == false) { return; } cmd.CommandText = "INSERT INTO [Member] ([Username], [Password], [UserType]) VALUES (@Username, @Password, @UserType)"; cmd.Parameters.Add("Username", System.Data.OleDb.OleDbType.VarChar); cmd.Parameters["Username"].Value = this.textBox1.Text; cmd.Parameters.Add("Password", System.Data.OleDb.OleDbType.VarChar); cmd.Parameters["Password"].Value = this.textBox2.Text; cmd.Parameters.Add("UserType", System.Data.OleDb.OleDbType.VarChar); cmd.Parameters["UserType"].Value = thisboBox1.Text; int numberOfRows = cmd.ExecuteNonQuery(); conn.Close(); System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav"); sound.Play(); var dialogresult = MessageBox.Show("Your username and password has been recorded", "Congratulations", MessageBoxButtons.OK); CreateTable(); if (dialogresult == DialogResult.OK) { this.Hide(); Form1 form = new Form1(); form.ShowDialog(); this.Close(); } private void CheckUsername() { OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb"; conn.Open(); OleDbCommand cmd = new OleDbCommand("SELECT [Username] FROM [Member]", conn); cmd.Parameters.Add("Username", System.Data.OleDb.OleDbType.VarChar); cmd.Parameters["Username"].Value = this.textBox1.Text; OleDbDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav"); sound.Play(); MessageBox.Show("Username already exists! Please use another username", "Warning"); return false; } else { System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav"); sound.Play(); MessageBox.Show("Username is not exists!", "Congratulations"); return true; } }

即使用户名在数据库中不存在,检查可用性"按钮仍然认为该用户名存在,这就是我无法继续的原因.

edit: even though the username is not exists in the database, "Check Availability" button still recognized it as exists and this is the reason that i can't proceed.

我该如何解决? 预先感谢!

How do i solve this? Thanks in advance!

推荐答案

private bool CheckUsername() { // return false if user name exists, otherwise tru } button_click(..) { if(CheckUsername() == false) { MessageBox.Show(Error Msg); return; } //save call }

您可以在CheckUserName中添加一个返回值,并在返回false时中断

You can add a return value in CheckUserName and break out if it return false

更多推荐

出现错误时阻止按钮继续

本文发布于:2023-10-24 07:49:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现错误   按钮

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!