Click here to Skip to main content
15,888,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

In my project I want to prevent the duplicate records to the database at inserting time

I wrote the code as follows




try
{
SqlCommand cmd = new SqlCommand();

//This is the query to check the duplicate

String str2 = "select code from state where code='" + this.txtstatecode.Text + "'";
cmd.CommandText = str2;
cmd.Connection = conn;
cmd.ExecuteNonQuery();

//This is the line I wrote to block the duplicate data


if (cmd.ToString ()=="")
{
String str = "insert into State(Code,Name)values ('" + this.txtstatecode.Text + "','" + this.txtstatename.Text + "')";

cmd.CommandText = str;
cmd.Connection = conn;
cmd.ExecuteNonQuery();

DataTable dt = new DataTable();
String str1 = "select code,name from State";// where code='" + this.txtstatecode.Text + "'";
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = str1;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(adp);

adp.Fill(dt);
bs.DataSource = dt;
statedg5.ItemsSource = bs;
adp.Update(dt);
this.statedg5.Items.Refresh();
}
else
{
System.Windows.MessageBox.Show ("Record already existing");
}

}
catch (Exception e1)
{
System.Windows.MessageBox.Show(e1.Message);
}




But the output is always "Record already existing" even for new records



Pls correct me the error
Posted

wrote:
if (cmd.ToString ()=="")


Well, that obviously doesn't work. You should read the documentation instead of guessing wildly.

ExecuteNonQuery means nothing is returned. You want to execute scalar perhaps, as only one value is expected, or you may want to select count(code) to make sure you expect one value.

wrote:
String str2 = "select code from state where code='" + this.txtstatecode.Text + "'";


There are at least three reasons I would fire you if you wrote this code for me. I assume you're not being paid, that would be obscene. But, if you want to learn, you should try to achieve a professional standard, not perpetuate the same mistakes.

1 - your variable name is meaningless
2 - you are writing SQL in the presentation layer
3 - your code means that if I wanted to attack your site, so long as I had access to this page, I could erase your entire database.

Read Code Complete for ideas on how to logically lay out readable code. Read up on n-tiered development and write data layers. Read up on SQL injection attacks, and protect against them. Buy a book on C#, so you can learn how ADO.NET works, instead of guessing.
 
Share this answer
 
You should have to use stored procedure which will handle duplicate records.
 
Share this answer
 
1. I believe this code is not real. Too bad for you if it is.
2. Use Constraints to prevent duplicates in DB layer
3. Use Custom Validation in GUI (learn MVC)
4. Never use inline sql: Not secured, works much slower, has to be recompiled on changes.
5. Use tiers (SQL -> DAL -> BLL -> GUI with a common infrastructure)
6. Use ObjectDataSource or any other DataSource controls if you write WebApp or DataBinding if you write WinForms
 
Share this answer
 
Do changes as suggested by other users, and after that change the first select query to get the count and not code value.
If the count is zero then you can go ahead to insert it.
Using a stored procedure is definitely a better option, throw back the result to front-end to display it there.
Hope this helps.
Regards,
 
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