locked
Check if Record Exists Else do Insert RRS feed

  • Question

  • User1079920034 posted

    Hi.

    I am trying to check either the data exist in the database or not. If it exists, it will not add the data and display 'not success' message in the page. Then, if the data not exists, it will add the data and willl redirect to display page and display 'success add'.

    Anyone can help me how to this?

    Thursday, September 19, 2019 9:09 AM

All replies

  • User-848649084 posted

    You could try to use the below code to check the data is exist or not and if not then it will be added to the database.

    <html>
    <head>
    <title>Form</title>
    </head>
    <body bgcolor="white" text="black">
    <!-- Begin form code --> 
     <form name="form" method="post" action="">
         Name: <input type="text" name="name" maxlength="20"> 
         <br>
         <input type="submit" name="submit" value="submit">
     </form> 
    <!-- End form code -->
    
    
    <%
    btnv=Request.form("submit")
    
    If btnv = "submit" Then
    
    
            Dim objConn
            Set objConn = Server.CreateObject("ADODB.Connection")
            objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\aspsamplesite\Database1.mdb;"
            objConn.Open
    		
    		SQLQuery1 = "Select * FROM Table2 WHERE name = '" &Request.Form("name")& "'"
    
    		Set Results = objConn.Execute(SQLQuery1)
    
    		'1. If linked response write. 
    		If Results.EOF  Then
    
    		 '1. Insert
    		  SQLQuery3 = "INSERT INTO Table2(" & "name) VALUES ('" &Request.Form("name")& "')"
    
    		  objConn.Execute(SQLQuery3)    
    		  Response.Write "Name added successfully."
    		else
    
    		  Response.Write "Name Already Exist"
    		  
    		end if
    
    
    End If
    %>
    </body>
    </html>

    Friday, September 20, 2019 6:12 AM
  • User-848649084 posted

    If you want to redirect to display after inserting data.try below code:

    insert page code:

    <html>
    <head>
    <title>Form</title>
    </head>
    <body bgcolor="white" text="black">
    
     <form name="form" method="post" action="">
         Name: <input type="text" name="name" maxlength="20"> 
         <br>
         <input type="submit" name="submit" value="submit">
     </form> 
    
    <%
    btnv=Request.form("submit")
    
    If btnv = "submit" Then
    
            Dim objConn
            Set objConn = Server.CreateObject("ADODB.Connection")
            objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\aspsamplesite\Database1.mdb;"
            objConn.Open
    		
    		SQLQuery1 = "Select * FROM Table2 WHERE name = '" &Request.Form("name")& "'"
    
    		Set Results = objConn.Execute(SQLQuery1)
    		
    		If Results.EOF  Then
    
    		 'Insert
    		  SQLQuery3 = "INSERT INTO Table2(" & "name) VALUES ('" &Request.Form("name")& "')"
    
    		  objConn.Execute(SQLQuery3)  
    Response.Redirect "dis.asp"
    	else
    		  Response.Write "Name Already Exist"
    		  
    		end if
    
    
    End If
    %>
    </body>
    </html>

    display page code:

    <html>
    <body> 
    
    
    
    <%
        
            Dim objConn
            Set objConn = Server.CreateObject("ADODB.Connection")
            objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\aspsamplesite\Database1.mdb;"
            objConn.Open
    
          
            Set str = objConn.execute("SELECT * FROM Table2;")
     
            Response.Write("<table border=1>")
        Response.Write "<tr><td> ID </td><td> name</td></tr>"
            If str.BOF And str.EOF Then
            ' No data
            Else
            Do While (Not str.EOF)
            Response.Write "<tr><td>" & str("ID") & "</td><td>" & str("name") & "</td></tr>"
            str.MoveNext
            Loop
            End If
            
    Response.Write("</table>")
    
    Response.Write("Record Added Succesfully")
    
    %>
    </body> 
    </html>

    Regards,

    Jalpa

    Friday, September 20, 2019 6:58 AM