Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#.net Windows Application Input data check before insert to Database
Posted

You have to write validations for data yourself.
 
Share this answer
 
Comments
krishna97 23-Nov-13 6:39am    
private bool RecordExists(string ItemName, string weightgm, string Weightmg, string Purity, string groupName)
{
string connect1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\BSTJewellers.accdb";
olcon = new OleDbConnection(connect1);
OleDbCommand cmd = new OleDbCommand();

//query for duplicate
cmd.CommandText = "select count(*) from StoreManagement where ItemName = @ItemName and groupName=@groupName and weightgm = @weightgm and Weightmg = @Weightmg and Purity=@Purity";
cmd.Parameters.AddWithValue("@ItemName", txtItemname.Text);
cmd.Parameters.AddWithValue("@groupName", cmbgrpname.Text);
cmd.Parameters.AddWithValue("@weightgm", txtweightgm.Text);
cmd.Parameters.AddWithValue("@Weightmg", txtweghtmg.Text);
cmd.Parameters.AddWithValue("@Purity", txtpurity.Text);

//cmd.CommandText = "select count(*) from StoreManagement where ItemName = '"+txtItemname.Text+"' and groupName='"+cmbgrpname.Text+"' and weightgm ='"+ txtweightgm.Text+"' and Weightmg ='"+ txtweghtmg.Text+"' and Purity='"+ txtpurity.Text+"'";


olcon.Open();

int recordCount = Convert.ToInt32(cmd.ExecuteScalar());
//cmd.ExecuteScalar();
olcon.Close();

return recordCount > 0;
//if (recordCount > 0)
//{
// MessageBox.Show("duplicate value insert");
//}
}

but error given it..??
User input data should be validated. Question is where you write that validation logic. Your applilcation is windows application. So some validation you should write in your presentation layer. For example

Example-1. The date of submit order should not be greater then current data or User name should not be empty.
That type of validation you should write in your windows windows form and call it when user press submit/save/edit etc button. You can also write that logic to your business model/entity and entity should contain a IsValid type method. This method will return validationResult type object which may contain any invalid data if entity contain.

Example-2> User wants to withdraw money from her account. In that type of validation you need to query your database for verify your input. In that type of validation logic you should write your business logic/domain service layer.

One important point you should remember that if you write data validation logic in your application but you first should add data constraint to your database first. It will ensure your data/referential integrity. For example Primary key/Unique index, Not Null etc.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900