We are excited to announce that the IIS.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

Check if Record Exists Else do InsertRSS

2 replies

Last post Sep 20, 2019 06:58 AM by Jalpa Panchal

  • Check if Record Exists Else do Insert

    Sep 19, 2019 09:09 AM|nurcahaya|LINK

    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?

  • Re: Check if Record Exists Else do Insert

    Sep 20, 2019 06:12 AM|Jalpa Panchal|LINK

    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>

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
  • Re: Check if Record Exists Else do Insert

    Sep 20, 2019 06:58 AM|Jalpa Panchal|LINK

    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

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.