Click here to Skip to main content
15,909,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using 3 tier architecture in c#.all values are inserting properly into the database table.my i want to check the value from the database. if it already exists inside the datatable column like (id number) . so one message should me display in the label that value is already exists please enter different value


Example
C#
public void insertdata()
       {
           try
           {
               BLL obbll = new BLL();
               BEL ob = new BEL();
               ob.fileno = txtfileno.Text;

               ob.name = txtname.Text;
               ob.status = statuslist.SelectedItem.Text;
               ob.name = txtname.Text;
               ob.fathername = txtfathername.Text;
               ob.designation = Designationlist.SelectedItem.Text;
               ob.type = typelist.SelectedItem.Text;
               ob.gender = genderlist.SelectedItem.Text;
               ob.group = grouplist.SelectedItem.Text;
               ob.PH = phlist.SelectedItem.Text;
               ob.DOB = Convert.ToDateTime(txtdoj.Text);
               ob.DOR = Convert.ToDateTime(txtdor.Text);
               ob.DOJ = Convert.ToDateTime(txtdoj.Text);
               ob.joinas = joinaslist.SelectedItem.Text;
               ob.inscale = float.Parse(txtinscale.Text);
               ob.presentscale = float.Parse(txtpresentscale.Text);
               ob.address = txtaddress.Text;
               ob.mobile = txtmobileno.Text;
               ob.email = txtemail.Text;
               ob.qualification = txtqualification.Text;
               ob.briefhistory = txtbriefhistory.Text;
               ob.remarks = txtremarks.Text;
               ob.category = Categorylist.SelectedItem.Text;
               ob.promotedas = "";
               ob.briefhistory = txtbriefhistory.Text;
               ob.MCAPdate = Convert.ToDateTime("1/1/1999");
               ob.substantivepost = "";
               ob.mcapgrantedinscale = 00;
               ob.dateofpromotion = Convert.ToDateTime("1/1/1999");

               obbll.insertpersonaldetails(ob);
           }
           catch(Exception ex)
           {
               labfileno.Text = ex.ToString();
           }
       }
Posted
Updated 19-Jul-15 23:44pm
v2

In my opinion the best way to handle this kind of requirement is to let the database prevent duplicates. This would mean that you create either a primary key for the table or a unique constraint. See
- Create Primary Keys[^]
- Create Unique Constraints[^]

This would mean that you don't check anything beforehand, just try to insert the data. If the key already exists you will receive an exception and you should handle that properly.

If you're not using a database as a backend but only a DataTable you can still use constraints. See UniqueConstraint Class[^]
 
Share this answer
 
v2
Comments
faizyab 2009 20-Jul-15 5:55am    
Sir i want to check it from the validation process
[no name] 20-Jul-15 6:03am    
That is reasonable, but still the suggestions from Mika Wendelius remains valid. Simply think about concurrent users.
Wendelius 20-Jul-15 6:06am    
Think about it this way. If you check the existence of a row from your code and you find that there is no row and start inserting it. At the same time another process may do the exact same and you will end up having a duplicate.

Databases prevent this by using locking. When the check is made during the insert (by the constraint), the row to be checked is exclusively locked if it exists. This prevents duplicates from being created.

Because of this I would advice to let the database take care of the duplicate validation, after all it's one of the things it's designed to do.
public bool IsExits()
{
SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings[ConnectionString].ToString());
SqlCommand sqlCmd = new SqlCommand("your stored proc", sqlConn);
sqlCmd.Parameters.AddWithValue("@Id", id);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
DataTable ds = new DataTable ();
sda.Fill(ds);
if(dt.rows.count>0)
{
return true;
}
return false;
}

public void insertdata()
{
try
{
bool flag=Isexists(your id);
if(flag)
{
// already exists
}
else
{
BLL obbll = new BLL();
BEL ob = new BEL();
ob.fileno = txtfileno.Text;

ob.name = txtname.Text;
ob.status = statuslist.SelectedItem.Text;
ob.name = txtname.Text;
ob.fathername = txtfathername.Text;
ob.designation = Designationlist.SelectedItem.Text;
ob.type = typelist.SelectedItem.Text;
ob.gender = genderlist.SelectedItem.Text;
ob.group = grouplist.SelectedItem.Text;
ob.PH = phlist.SelectedItem.Text;
ob.DOB = Convert.ToDateTime(txtdoj.Text);
ob.DOR = Convert.ToDateTime(txtdor.Text);
ob.DOJ = Convert.ToDateTime(txtdoj.Text);
ob.joinas = joinaslist.SelectedItem.Text;
ob.inscale = float.Parse(txtinscale.Text);
ob.presentscale = float.Parse(txtpresentscale.Text);
ob.address = txtaddress.Text;
ob.mobile = txtmobileno.Text;
ob.email = txtemail.Text;
ob.qualification = txtqualification.Text;
ob.briefhistory = txtbriefhistory.Text;
ob.remarks = txtremarks.Text;
ob.category = Categorylist.SelectedItem.Text;
ob.promotedas = "";
ob.briefhistory = txtbriefhistory.Text;
ob.MCAPdate = Convert.ToDateTime("1/1/1999");
ob.substantivepost = "";
ob.mcapgrantedinscale = 00;
ob.dateofpromotion = Convert.ToDateTime("1/1/1999");

obbll.insertpersonaldetails(ob);
}
catch(Exception ex)
{
labfileno.Text = ex.ToString();
}
}
}
 
Share this answer
 

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