I'm using C# 2010 Express and Sql Compact. I have two-column table and a form to add a data to save to the table. But I dont want to save same entrys.
For example;
Name City
Sefa Istanbul
Sefa New york
Sefa London
Ben New York
......
is ok but if user want to add another
Name City
Sefa Istanbul
or already available another entry, the program must stop.
I have add this code to my project, but checkcmd.ExecuteNonQuery() command always returns '-1'
SqlCeConnection Baglanti = new SqlCeConnection(@"Data Source=|DataDirectory|\CeoDatabase.sdf;Password=CeoDB;Persist Security Info=True");
Baglanti.Open();
string checkcommand = "SELECT BarAdi, BarSehri FROM Barlar WHERE BarAdi = '"+ Txt_BarAdi.Text +"' AND BarSehri = '"+Txt_BarSehri.Text+"'";
SqlCeCommand checkcmd = new SqlCeCommand(checkcommand, Baglanti);
int kontrol = checkcmd.ExecuteNonQuery();
MessageBox.Show(kontrol.ToString());
return;
if (checkcmd.ExecuteNonQuery() < 1)
{
MessageBox.Show("Bu bilgiler ile kayıtlı bir bar zaten mevcut");
return;
}
else
{
SqlCeCommand Islem = new SqlCeCommand("INSERT INTO Barlar(BarAdi,BarSehri)VALUES('" + Txt_BarAdi.Text + "','" + Txt_BarSehri.Text + "')", Baglanti);
Islem.ExecuteNonQuery();
MessageBox.Show("İşlem başarıyla tamamlandı");
}
Baglanti.Close();
You can have it one of the ways
Either set Unique Key for that column in Database . When new record will be inserted it will be automatically checked (Preferred)
Or while Inserting new record check if that city already exist.
If you want to check for exist combination. Do somthing like this (I assume this how you have your data):