2017-05-23 4 views
0

사용자의 VotingStatus(Boolean)에 대한 인증을해야합니다. 일단 투표 상태가 데이터베이스에서 선택되면 다시 투표 할 수 없습니다.부울 값의 데이터 형식 불일치 오류

아래의 코드는 데이터 유형 불일치 오류를 보여줍니다. 이게 뭐가 잘못 되었 니?

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!"); 
} 

답변

1

다음과 같은 라인

var vstatus = (String)comd2.ExecuteScalar(); 
if (vstatus == "true") 
다음과 같이

변경해야합니다 : 그것은 당신이 사용하지 않도록해야합니다, 그것은 언급해야 물었다 그와 무관에도 불구하고

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

에게 SQL 문의 작성시. 자열 병합. 그렇게하지 않으면 SQL 인젝션을 사용할 수 있습니다.

+0

유권자가 이미 투표했는지 확인하기 위해 boolean 값인 "VotingStatus"와 "true/false"를 비교해야합니다. – Theodore

+0

데이터베이스의'VotingStatus' 값은 varchar 유형으로 'true'또는'false'입니까? – Christos

+0

varchar 유형은 무엇을 의미합니까? – Theodore