How to add database validation for c# windows form

Jan 14 2015 12:29 AM

I've created data updating form using C# winform. i need to block same data updating to the database. when we insert existing data to database from winfrom. need to show data already updated

here is my code

 
 
*
try
{
byte[] img = null;
FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
img = br.ReadBytes((int)fs.Length);
//cmd.CommandText = "INSERT INTO UserInfo (User_Name,Email,EPF_Number,Department,Laptop_Model,Laptop_Serial,Issu_Date,Hostname,)VALUES (@Name,@Email,@EPFNumber,@DPT,@Model,@Serial,@Date,@Host)";
//string sql = "INSERT INTO UserInfo (User_Name,Email,EPF_Number,Department,Laptop_Model,Laptop_Serial,Issu_Date,Hostname,Image) VALUES ( @Name,@Email,@EPFNumber,@DPT,@Model,@Serial,@Date,@Host,@Photo)";
string sql = "INSERT INTO UserInfo (User_Name,Email,EPF_Number,Department,Laptop_Model,Laptop_Serial,Issu_Date,Hostname,Image) VALUES ( @Name,@Email,@EPFNumber,@DPT,@Model,@Serial,@Date,@Host,@Photo)";
if (conn.State != ConnectionState.Open)
conn.Open();
cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add(new SqlParameter("@Photo", img));
cmd.Parameters.AddWithValue("@Name", txtUserName.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@EPFNumber", txtEPF.Text);
cmd.Parameters.AddWithValue("@DPT", cmbDpt.Text);
cmd.Parameters.AddWithValue("@Model", cmbLmdl.Text);
cmd.Parameters.AddWithValue("@Serial", cmdSerl.Text);
cmd.Parameters.AddWithValue("@Date", dobPicker.Text);
cmd.Parameters.AddWithValue("@Host", cmbHostName.Text);
conn.Close();
cmd.Connection = conn;
conn.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Successflully Saved");
}
else
{
MessageBox.Show("Error occured");
}
conn.Close();
cmd.Parameters.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
}
 

Answers (4)