Click here to Skip to main content
16,009,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table with email field and email type, i am done with checking if duplicate exists or not ,but when i edit some record and change only the email type field it checks if the email already exists ,since i have changed only email type i am not able to edit the record,can any one help me with this prob.can i do this with REPLACE,recordid is the primary key for this table.
public bool CheckDuplicate(string Email)
		{
			MySqlCommand cmd=null;
			MySqlDataReader dtr=null;
			bool bRv=false;
			try 
			{
				con.Open();
				string str=string.Format("SELECT * FROM tbl_users_email WHERE tbl_users_emailaddress='{0}'",Email);
				cmd=new MySqlCommand(str,con);
				dtr=cmd.ExecuteReader();
				while(dtr.Read())
				{
					bRv=true;
				}
				return bRv;
			} 
			catch (Exception ex) 
			{
				throw ex;
			}
                     }
Posted
Comments
girish sp 10-May-11 2:27am    
i have to check for duplicates while editing some record because,as both the fields are editable user may enter the email id which is already present in the database??so now i think you understood my problem..

If you are editing the record, why are you checking if it exists: you already know it does.
Why not just use the Sql UPDATE command:
C#
cmd = new MySqlCommand("UPDATE tbl_users_email SET email_type=@NEWVALUE WHERE tbl_users_emailaddress=@EMAIL", con);
cmd.Parameters.AddWithValue("@NEWVALUE", newEamilType);
cmd.Parameters.AddWithValue("@EMAIL", Email);

Note: Do not create SQL commands by concatenating strings, it leaves you wide open to an accidental or deliberate SQL Injection attack, which can destroy your database. Use Parametrized queries as I show above instead.
 
Share this answer
 
You can make it this way.

dtr.Edit
dtr.Email ="YourEmail@EmailServer.Com"
dtr.Update
 
Share this answer
 

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

  Print Answers RSS


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