الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

How To Add validation( check if value exists in database) in this code

بدأه a.ss.suliman في 2 ديسمبر 2014 · 1 رد · 961 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

SqlConnection sqlcon = new SqlConnection(@"Server=ADMIN-PC\SQLEXPRESS;Database=ELMANAR; Integrated Security = True");
SqlDataAdapter da;
DataTable dt1 = new DataTable();
 
Data_Access_Layer.DataAccessLayer DAL = new Data_Access_Layer.DataAccessLayer();
SqlCommand cmd = new SqlCommand();
 

 

private void btnsave_Click(object sender, EventArgs e)
{
 
try
{

cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
 
cmd.CommandText = "insert into TBL_CATEGORYCUSTOMERS (catcustomer_id,catcustomer_name,catcustomer_note) values ('" + txtid.Text + "','" + txtname.Text + "','" + txtnote.Text + "')";
sqlcon.Open();
cmd.ExecuteNonQuery();


MessageBox.Show("Saved");
sqlcon.Close();
dt1.Clear();
da.Fill(dt1);
 
btnsave.Enabled = false;
btnadd.Enabled = true;
txtid.Clear();
txtname.Clear();
txtnote.Clear();

}

 
catch { }

تم تعديل هذه المشاركة بواسطة a.ss.suliman في 2 ديسمبر 2014 في 23:20

#2

just add your validation in CommandText

cmd.CommandText = @"
if not Exists (select Catcustomer_id from TBL_CATEGORYCUSTOMERS  where catcustomer_name = '" + txtName.Text + @"')
insert into TBL_CATEGORYCUSTOMERS (catcustomer_id,catcustomer_name,catcustomer_note) values ('" + 
txtid.Text + "','" + txtname.Text + "','" + txtnote.Text + "')";

note the using of @ symbole to enable you typing many lines in the string object

تم تعديل هذه المشاركة بواسطة ema2000_t في 4 ديسمبر 2014 في 09:20

2

أدعو الله أن يجعل أعمالنا خالصة لوجهه الكريم --- وأن ينفعنا بما علمنا

Dr Sayed Tohamy

Programming Instructor

مدرب برمجة

---------------

EmaSoft2000

-------------------

مواضيع مشابهة