简体   繁体   English

布尔中的数据类型不匹配错误

[英]Data type mismatch error in boolean

I need to make an authentication on the VotingStatus(Boolean) of the user, once that his voting status is checked in the database he shouldn't be able to vote again. 我需要对用户的VotingStatus(Boolean)进行身份验证,一旦在数据库中检查了他的投票状态,他就不能再次投票。

This code below shows a data type mismatch error. 下面的代码显示数据类型不匹配错误。 What is wrong with this? 这有什么问题?

MessageBox.Show("Welcome!");
OleDbCommand comd1 = new OleDbCommand();
comd1.Connection = connection;
comd1.CommandText = "SELECT VoterID FROM tbl_voter where Uname='" + txt_user.Text + "' and Pword='" + txt_pass.Text + "'";
voterid = Convert.ToString(comd1.ExecuteScalar());
Program.VoterID = voterid;

string comd21 = "Select VotingStatus from tbl_voter where VoterID='" + voterid + "'";
OleDbCommand comd2 = new OleDbCommand(comd21, connection);
var vstatus = (String)comd2.ExecuteScalar();
if (vstatus == "true")
{
      MessageBox.Show("You cannot vote again!");
}

You should change the following lines 您应该更改以下几行

var vstatus = (String)comd2.ExecuteScalar();
if (vstatus == "true")

as below: 如下:

var vstatus = comd2.ExecuteScalar();
if (vstatus !=null && Convert.ToBoolean(vstatus))
{
    MessageBox.Show("You cannot vote again!");
}

Despite it is irrelevant with that you have asked, it should be mentioned that you must avoid using string concatenation in the creation of SQL statements. 尽管与您提出的要求无关,但应该指出,在创建SQL语句时必须避免使用字符串连接。 Not doing so you are open to SQL injections. 不这样做,您就可以接受SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM