Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is my code in project :-

C#
s = "insert into Cars (Car_id , Car_No, Car_No_2, Car_shs, Car_Motor)
s = s + "values (@Car_id , @Car_No, @Car_No_2, @Car_shs, @Car_Motor
sCommand = new SqlCommand(s, con);
con.Open();
sCommand.Parameters.AddWithValue("@Car_id", txt_Car_id.Text);
sCommand.Parameters.AddWithValue("@Car_No", txt_Car_No.Text);
sCommand.Parameters.AddWithValue("@Car_No_2", txt_Car_No_2.Text);
sCommand.Parameters.AddWithValue("@Car_shs", txt_Car_shs.Text);
sCommand.Parameters.AddWithValue("@Car_Motor", txt_Mator_No.Text);
sCommand.ExecuteNonQuery();


What I have tried:

before value saved in database check if Car_id is exist in database
Posted
Updated 16-Jul-19 1:35am

There are two ways.

One: create a select or select count(*) with the same parameters and check if there are any results > 0 returned. Don't perform the second insert unless the select has no results

Two: (the correct way) You can use Select even if you only have variables to insert. You can use this syntax to add a Where clause

Add a clause on the insert to avoid duplicates:
C#
//use @ for a literal string.  That way you just just multiline it
s = @"
insert into Cars (Car_id , Car_No, Car_No_2, Car_shs, Car_Motor)
Select @Car_id , @Car_No, @Car_No_2, @Car_shs, @Car_Motor
Where not exists (
    select * 
    from Cars 
    where 
        Car_id = @Car_id
    and Car_No = @Car_No,
    and Car_No_2 = @Car_No_2
    and Car_shs = @Car_shs
    and Car_Motor = @Car_Motor)
"
sCommand = new SqlCommand(s, con);
con.Open();
sCommand.Parameters.AddWithValue("@Car_id", txt_Car_id.Text);
sCommand.Parameters.AddWithValue("@Car_No", txt_Car_No.Text);
sCommand.Parameters.AddWithValue("@Car_No_2", txt_Car_No_2.Text);
sCommand.Parameters.AddWithValue("@Car_shs", txt_Car_shs.Text);
sCommand.Parameters.AddWithValue("@Car_Motor", txt_Mator_No.Text);
sCommand.ExecuteNonQuery();


This will not insert if it exists.

You can check if it was inserted by checking the return value of sCommand.ExecuteNonQuery(). It returns a count of the rows affected:

C#
var recordsAffected = sCommand.ExecuteNonQuery();
return recordsAffected == 1;


Hope that helps ^_^
Andy
 
Share this answer
 
Comments
[no name] 21-Mar-16 5:37am    
Nice trick :)
Andy Lanng 21-Mar-16 5:42am    
Yeah, it's not well advertised that ExecuteNonQuery() returns an int so I like to spread the knowledge whenever I can ^_^
Try with below code:
C#
// Checking car exist in Database
string s = @"SELECT COUNT(*) FROM Cars WHERE Car_id = @Car_id))";
sCommand = new SqlCommand(s, con);
sCommand.Parameters.AddWithValue("@Car_id", txt_Car_id.Text);
con.Open();
int records = (int)sCommand.ExecuteScalar();

if(records == 0)
{
	sCommand.Parameters.Clear();
	s = @"insert into Cars (Car_id , Car_No, Car_No_2, Car_shs, Car_Motor)
		values (@Car_id , @Car_No, @Car_No_2, @Car_shs, @Car_Motor)";
	sCommand = new SqlCommand(s, con);

	sCommand.Parameters.AddWithValue("@Car_id", txt_Car_id.Text);
	sCommand.Parameters.AddWithValue("@Car_No", txt_Car_No.Text);
	sCommand.Parameters.AddWithValue("@Car_No_2", txt_Car_No_2.Text);
	sCommand.Parameters.AddWithValue("@Car_shs", txt_Car_shs.Text);
	sCommand.Parameters.AddWithValue("@Car_Motor", txt_Mator_No.Text);
	sCommand.ExecuteNonQuery();
}
else
{
	Response.Write("Records Exists");
}
 
Share this answer
 
I assumed that you are using a MSSQL database, Use SQL MERGE statement on database side.
 
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