Hi Every one !!!
I want to know how i need to capture a duplicate value in database..For example if user enters the same E-mail which already in the database, I want to give a javascript alert or else re-direct to another page and wants to tell that "This is already reserved ".
I m using Access 2007 and VS 2008

My idea was these duplicate values are passed to Exception part..then i can capture it..
but it does not work

OleDbConnection conn = new OleDbConnection();
        OleDbCommand comm = new OleDbCommand();
        conn.ConnectionString = @ " database/path.mdb " ;
            string sql = "Insert into x ( a,b,c ) values (a,b,c)";
            comm = new OleDbCommand(sql, conn);
            Session["mov"] = a.ToString();
            Session["Hall"] = b.ToString();

        catch (Exception ex)
          //  Response.Write(ex.ToString());
           [B] Response.redirect("Errorpage.aspx");[/B]

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

7 Years
Discussion Span
Last Post by Chammarox

first make a check if the user exist in the database..

if user found in database then display error message..

else you are ready insert :)


Well , that means when some one enters email ( for an example )...I have to write a code which can check whether its already in the database....??
I m not sure how to do it :(
But hopefully due to Your guidance i got an idea how should i implement it..Thanks...

When user inserting data and Press the submit button..Entered values should have to go to the database..and have to search the database..If it found same data..It should display the message...
Am i right ???

also if you can better direct me , how implement that Searching part..
Thanks For your help


>I m not sure how to do it

You better know. Execute Select statement to find whether a record (value) is present in database or not.

>If it found same data..It should display the message...Am i right ???

Of course. Put your code here if it doesn't work.


There are quite a few ways to go about accomplishing this... First thing though, you do not want to use exceptions in that manner. Secondly, why not use the built in asp.net membership provider rather than creating one in access?

If you insist though your code would be something like...

        MsgBox("Your E-mail Address Has Been Subscribed !")   
    Catch ex As Exception   
        MsgBox("Your E-mail Address Has Been Already Subscribed Before !")

you can also use "IF NOT EXISTS" so your code would look something like

InsertCommand="IF NOT EXISTS(SELECT [username] FROM [usertable] WHERE [username] = ?) BEGIN INSERT INTO [usertable] ([username]) VALUES (?)"

Hope that helps - please mark as solved if it does.

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.