Win Formsda kayıt formu yapıyordum. Türkçe karakter içermemesi için bunu ekledim.
Bu function Türkçe ve özel karakterleri tespit ediyor. Varsa msgbox veriyor hata.
Bunu butona ekleyip bir de if else ile veritabanına ekleme koydum. Türkçe karakter varsa hatayı veriyor fakat yok ise veritabanına kayıt etmiyor. Nerede hata yapıyorum?
Kod:
private bool CheckTrChars()
{
char[] trChars = { 'ö', 'Ö', 'ü', 'Ü', 'ç', 'Ç', 'İ', 'ı', 'Ğ', 'ğ', 'Ş', 'ş' };
string[] unkownChars = { "?", ",", ".", "-", "*", "/", "#", "(", ")", "+", "-", "@", "é", ";", ":", "<", ">", "!", "^", "'", "$", "%", "&", "=", "_", "½", "{", "]", "}", "[", "|" };
for (int i = 0; i < trChars.Length; i++)
{
if (txtUsername.Text.Contains(trChars[i]) || txtPassword.Text.Contains(trChars[i]) || txtUsername.Text.Contains(unkownChars[i]) || string.IsNullOrEmpty(txtUsername.Text) || string.IsNullOrEmpty(txtPassword.Text))
{
MessageBox.Show("Username or Password can not be blank and can not contains Turkish characters or special characters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
return true;
}
Bu function Türkçe ve özel karakterleri tespit ediyor. Varsa msgbox veriyor hata.
Bunu butona ekleyip bir de if else ile veritabanına ekleme koydum. Türkçe karakter varsa hatayı veriyor fakat yok ise veritabanına kayıt etmiyor. Nerede hata yapıyorum?
Kod:
try
{
if (CheckTrChars())
{
}
else
{
// open the connection
connectDB.Open();
// I defined the insert command and wrote the insert query.
MySqlCommand ekle = new MySqlCommand("insert into users (username,password) values ('" + txtUsername.Text + "','" + txtPassword.Text + "')", connectDB);
// running the query.
object sonuc = null;
sonuc = ekle.ExecuteNonQuery(); // the query worked, and the returned value passed to the object type variable if the variable is not empty, it is not added if it is empty.
if (sonuc != null)
{
MessageBox.Show("You have successfully registered.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Error while registering.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// close the connection
connectDB.Close();
}
}
}
catch (Exception HataYakala)
{
MessageBox.Show("Error: " + HataYakala.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
