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 " ;
        try
        {
            conn.Open();
            string sql = "Insert into x ( a,b,c ) values (a,b,c)";
            comm = new OleDbCommand(sql, conn);
            comm.ExecuteNonQuery();
            Session["mov"] = a.ToString();
            Session["Hall"] = b.ToString();
            Response.Redirect("Ho.aspx");

        }
        catch (Exception ex)
        {
          //  Response.Write(ex.ToString());
           [B] Response.redirect("Errorpage.aspx");[/B]
        }
        finally
        {
            conn.Close();
        }

Recommended Answers

All 7 Replies

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...

Try  
        AccessDataSource1.Insert()   
        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.

It really helps...Thanks DhCoder... Thanks a lot :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.