Link to home
Start Free TrialLog in
Avatar of mattybrigh
mattybrigh

asked on

0x80040E21 update error?

I'm getting an error and it's driving me crazy...I get this error when trying to update info. on my MYSQL database.  The fields are varchar(255) with an id integer field:

Microsoft OLE DB Provider for ODBC Drivers (0x80040E21)
Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
/brands/admin/submit.asp, line 36
 
 
<%
 

Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("brands")

Set RS = Server.CreateObject("ADODB.Recordset")

id = Request.Form("id")
If Request.Form("remove") <> "" Then remove = true
If Request.Form("add") <> "" Then addit = true
 

If remove Then
      sql = "DELETE FROM brands WHERE id = " & id
      Conn.Execute(sql)
      Response.Redirect("default.asp?delete=1&name=" & Server.URLEncode(return_field))
Else

      If NOT errs Then
       
       If addit Then
                  RS.Open "brands", Conn, adOpenKeyset, adLockOptimistic, adCmdTable
                  RS.AddNew
            Else
                  sql = "SELECT * FROM brands WHERE id = " & id
                  RS.Open sql, Conn, adOpenKeyset, adLockOptimistic
       End If

          'RS.Fields("id") = Request.Form("id")
              RS.Fields("brand") = Trim(Request.Form("brand"))      <---------------------------LINE 36
              RS.Fields("logo") = Trim(Request.Form("logo"))
              RS.Fields("pof_icon") = Trim(Request.Form("pof_icon"))
              RS.Fields("best_practice1") = Trim(Request.Form("best_practice1"))
              RS.Fields("best_practice2") = Trim(Request.Form("best_practice2"))
              RS.Fields("best_practice3") = Trim(Request.Form("best_practice3"))
              RS.Fields("best_practice4") = Trim(Request.Form("best_practice4"))
              RS.Fields("rebate1_link") = Trim(Request.Form("rebate1_link"))
              RS.Fields("rebate1") = Trim(Request.Form("rebate1"))
              RS.Fields("rebate2_link") = Trim(Request.Form("rebate2_link"))
              RS.Fields("rebate2") = Trim(Request.Form("rebate2"))
              RS.Fields("rebate3") = Trim(Request.Form("rebate3"))
              RS.Fields("rebate4") = Trim(Request.Form("rebate4"))
              RS.Fields("foodguide1_link") = Trim(Request.Form("foodguide1_link"))
              RS.Fields("foodguide1") = Trim(Request.Form("foodguide1"))
              RS.Fields("foodguide2") = Trim(Request.Form("foodguide1"))
              RS.Fields("foodguide3") = Trim(Request.Form("foodguide1"))
              RS.Fields("caption") = Trim(Request.Form("caption"))
              RS.Fields("promo1") = Trim(Request.Form("promo1"))
              RS.Fields("promo2") = Trim(Request.Form("promo2"))
              RS.Fields("promo3") = Trim(Request.Form("promo3"))
              RS.Fields("description") = Trim(Request.Form("description"))
              RS.Fields("thumbnail") = Trim(Request.Form("thumbnail"))
              RS.Fields("tagline") = Trim(Request.Form("tagline"))
               
                       
             
         
               RS.Update
               RS.Close
            '*********************************
       
       If addit Then
                  Response.Redirect("default.asp?add=1&name=" & Server.URLEncode(return_field))
            Else
                  Response.Redirect("default.asp?edit=1&name=" & Server.URLEncode(return_field))
            End If
      End If
 End if
 

Set RS = nothing
Set fromWhere = Request.Form
%>

 

            <h3 class="bold"><% If addit Then%>Add<%Else%>Edit<%End If%>&nbsp;Article</h3>

            <table border="0">
                  <tr>
                        <td>
                              <%If errs Then%>
                                    <span class="bold" style="color:#FF0000">Please provide information for all required fields and check to make sure the information you provided is accurate.</span>
                                    <!--#include file="_form.asp"-->
                              <%End If%>
                        </td>
                  </tr>
            </table>

 

<%
Conn.Close
Set Conn = nothing

%>

If this matters, this is on the error page too:

Browser Type:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

Page:
POST 308 bytes to /brands/admin/submit.asp

POST Data:
id=&brand=Matty%27s+Fish+Sticks&tagline=&thumbnail=&pof_icon=&best_practice1=&best_practice2=&best_practice3=&best_practice4=&rebate1_link=&rebate1=&rebate2_link=&rebate2=&rebate3=&rebate4=&foodguide1 . . .
 
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Perhaps:

       If addit Then
               RS.Open "brands", Conn, adOpenKeyset, adLockOptimistic, adCmdTable
               RS.AddNew                                             '<------------------ This is fine
          Else
               sql = "SELECT * FROM brands WHERE id = " & id
               RS.Open sql, Conn, adOpenKeyset, adLockOptimistic     '<------------------ But what about this branch?
       End If

Line 36 (since line 35 is commented...and SHOULD be since that tries to replace the key) begins updating a record from your recordset.  If addit=FALSE, the second branch is execute, which opens the recordset, but does not trigger an .AddNew or .Update method.  What is the state of your variables at that point?
Avatar of mattybrigh
mattybrigh

ASKER

I guess I don't understand your question...the state of the variables?
Essentially I use this same block of code which works fine with Access DB and SQL server - it's giving me a problem with the MySQL, though.  Would there be something in the MySQL control center that I'd need to change?
Never mind, I did not see that you posted the state at the end of your question.  I'll learn to read some day.  :)

OK, according to your first post, Request.Form("remove") and Request.Form("add") are both NULL, since they were not listed in your POST list.  This means in your first IF statement, the code will fall through to the IF branch (since remove<>TRUE).  I did not see a reference to "errs", but I assume from the context it is boolean.  If it has not been set, it defaults to FALSE which means the second IF statement will flow through the main branch.  "addit" is FALSE (since the request variable "add" is not present), which means the third IF statement executes the ELSE branch.  This section is shown here:

       If addit Then
               RS.Open "brands", Conn, adOpenKeyset, adLockOptimistic, adCmdTable
               RS.AddNew
          Else
               sql = "SELECT * FROM brands WHERE id = " & id               <------------------- WE ARE HERE
               RS.Open sql, Conn, adOpenKeyset, adLockOptimistic
       End If

          'RS.Fields("id") = Request.Form("id")
            RS.Fields("brand") = Trim(Request.Form("brand"))      <---------------------------LINE 36
            RS.Fields("logo") = Trim(Request.Form("logo"))

If you look at your POST list, the request variable "id" is null, making your local variable "id" NULL as well.  This screws your SQL build, which now looks like this:

sql = "SELECT * FROM brands WHERE id = <null>"

Since [id] is likely the key field in that table, the recordset you open is not going to be populated (you can't have a record without a key, right?).  The next line to execute will attempt to update the [brand] field, for a non-existent record, AND without an rs.Edit statement to begin the process.

Which error would you like to start with?
Makes sense (sort of)...Let's just start with the first error and go from there - your call as you most likely know how to fix this whereas I'm just staring at the screen! ;-)
Here's what you need to do:

1) Provide a value for Request.Form("id") from the previous page, OR account for a NULL value in the variable when building the SQL.
2) Add rs.Edit to the third IF's ELSE branch (the "WE ARE HERE" section from my last post).  It should appear after you have opened the recordset.
3) Define "errs" or otherwise provide a value for it.

An alternative to #1 is to disallow a NULL value for Request.Form("id") at the start of the page.  You should never try to use a key field when the possibility of a NULL value exists, since every record MUST have a unique value for the key.  When you assign the value to your local "id" variable, check for NULL/Empty, and redirect the user accordingly.

These fixes are meant only to fix this one branch.  As far as this code working fine with Access and MSSQL, I can only guess that during your tests, this particular part of the code was never executed.  Access and MSSQL have very little to do with the execution...the server would return the error.  Be sure to test every possible logic branch of your code.  The one you do not test is the one that fails later.
Ok, in theory I understand what you mean, but I really don't know how to do what you want.  I just have this code that I got off some web site and have managed to modify it to suit my needs.  I suppose what I'm asking is if you could just post the changes in the code and I can cut/paste them to make it work for me.  

I'm all about learning how for the future, but time is kind of the essence for me right now...otherwise I'm totally lost.
The fix for #2 is easy:

       If addit Then
               RS.Open "brands", Conn, adOpenKeyset, adLockOptimistic, adCmdTable
               RS.AddNew
          Else
               sql = "SELECT * FROM brands WHERE id = " & id
               RS.Open sql, Conn, adOpenKeyset, adLockOptimistic
               RS.Edit        '<------------- The fix you need
       End If

#3 I have no idea.  You'll have to go through the code and see if you are even using the "errs" variable, how it is applied, what it is supposed to mean, etc.  For #1, it is a little more complicated.  From where do your users come to this page?  Whatever page they see before this, they should be selecting an ID to edit/add/delete.  Without that ID number, from what I can see, this page should not run AT ALL.  Instead, it should kick the user back to the previous page with an error message, or otherwise inform the user they did not supply an ID for the work.  Your code is obviously geared to delete the record, add a new record, or edit an existing one.  With delete or editing, you WILL need the ID of the record.  For adding, no problem, since the DB should provide that key value for you.

It sounds to me like you just need to understand more about the code and what it does.  Using someone else's code is always problematic because you enter into it not knowing exactly what they are doing.  You have to trace through it to understand the flow and how it works...then you'll be able to modify and tweak it to make it do exactly what you want.  IMHO, you can go back to the original author and tell him his code is incomplete and seriously lacks error handling and checking.
Well, for #2 I get this error:
Microsoft VBScript runtime (0x800A01B6)
Object doesn't support this property or method: 'RS.Edit'
/brands/admin/submit.asp, line 32

As for how they get there (and it's just me who'll be doing it) there's a default.asp page (I'm assuming this is what you're talking about)
<html>
 <head>
 <body bgcolor="#ffffff">
 <title>Admin - Main Page Postings</title>
 <link rel="stylesheet" href="../stylesheet.css">
 </head>


<a name="top"></a>
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("brands")
sql = "SELECT * FROM brands order by brand"
Set RS = Conn.Execute(sql)
%>

 

<table border="0" class="text"><tr><td valign="top">
TO ADD a new Item - <a class="reviews" href="addedit.asp?add=1" class="bold">Click Here</a></td>
</tr>


<tr><td valign="top">
TO EDIT an Item -  
<%If RS.EOF Then%>
   <p class="errormsg">(None currently available to edit.)</p>
<%Else%>
    <span class="bold">Click on the Job Title</td></tr></table>
   <table border="0" cellpadding="3" cellspacing="0" width="100%" style="border: 1px solid #8097C0" class="text">
       <tr bgcolor="#E0E4ED">
        
             <td class="bold" style="border-bottom: 1px solid #8097C0">Thumbnail</td>
           <td class="bold" style="border-bottom: 1px solid #8097C0">Brand</td>
               <td class="bold" style="border-bottom: 1px solid #8097C0">Tagline</td>
               
                          
       </tr>
       <%
       bgcolor="#f0f0f0"
       Do While NOT RS.EOF' AND cnt < RS.PageSize
           If bgcolor="#f0f0f0" Then
               bgcolor=""
           ElseIf bgcolor = "" Then
                 bgcolor="#f0f0f0"
           End If
       %>
       <tr bgcolor="<%=bgcolor%>">
         <td valign="top"><%=RS("thumbnail")%></td>
         <td valign="top"><a href="addedit.asp?id=<%=RS("id")%>"><%=RS("Brand")%></a></td>
         <td valign="top"><%=RS("Tagline")%></td>
         </tr>
       <%
           RS.MoveNext
       Loop
       %>
   </table>

<%
End If 'If RS.EOF
%>

 

<%
RS.Close
Set RS = nothing
Conn.Close
Set Conn = nothing
%>
<br><br><div align="center">
<a href="#top">Back to Top</a></div>

Otherwise - I'm still lost.  I'm considering just hard coding the whole damn thing...
DOH!  My apologies.  The .Edit method is for DAO...a little different.  ADO should not need the line I added.  The problem must be limited to only the NULL id value.

This other page (default.asp) looks fine so far, and it definitely sends the ID value.  This snippet is from near the end of the page:

       <tr bgcolor="<%=bgcolor%>">
        <td valign="top"><%=RS("thumbnail")%></td>
' ----------VVVVVV  The next line is where the link for the editing is created.  When you load this page, you should be able to see the links
'                            as:  addedit.asp?id=<the id value for this record>
        <td valign="top"><a href="addedit.asp?id=<%=RS("id")%>"><%=RS("Brand")%></a></td>
        <td valign="top"><%=RS("Tagline")%></td>
        </tr>

Now that I see the first page, I can give a good guess as to why ID was NULL in the second page: you're using the wrong object to collect the data.  You're not using a form in default.asp, which means that Request.Form is an empty object.  Change the reference to Request.QueryString, and the ID should pop up with no problem.  Here's a change you can test on your addedit.asp page.  From the beginning of your code, change the lines as follows:

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("brands")

Set RS = Server.CreateObject("ADODB.Recordset")

id = Request.QueryString("id")
If Request.QueryString("remove") <> "" Then remove = true
If Request.QueryString("add") <> "" Then addit = true

If that seems to work, make that replacement everywhere you see Request.Form.  Your other option is to actually build the form instead...depending on your needs, that may or may not be the better option for you.


Ok, I'm even more lost now that before (sorry, but when you don't really know how something works you get lost).  I'm posting all four of my pages (the actual form is an include file) and maybe you can just make changes where need be...as I'm about to call it quits (when things get difficult - I quit).

default.asp
=======
<!--#include virtual="/adovbs.inc"-->
 
 <html>
 <head>
 <body bgcolor="#ffffff">
 <title>Admin - Main Page Postings</title>
 <link rel="stylesheet" href="../stylesheet.css">
 </head>


<a name="top"></a>
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("brands")
sql = "SELECT * FROM brands order by brand"
Set RS = Conn.Execute(sql)
%>

 

<table border="0" class="text"><tr><td valign="top">
TO ADD a new Item - <a class="reviews" href="addedit.asp?add=1" class="bold">Click Here</a></td>
</tr>


<tr><td valign="top">
TO EDIT an Item -  
<%If RS.EOF Then%>
   <p class="errormsg">(None currently available to edit.)</p>
<%Else%>
    <span class="bold">Click on the Job Title</td></tr></table>
   <table border="0" cellpadding="3" cellspacing="0" width="100%" style="border: 1px solid #8097C0" class="text">
       <tr bgcolor="#E0E4ED">
        
             <td class="bold" style="border-bottom: 1px solid #8097C0">Thumbnail</td>
           <td class="bold" style="border-bottom: 1px solid #8097C0">Brand</td>
               <td class="bold" style="border-bottom: 1px solid #8097C0">Tagline</td>
       </tr>
       <%
       bgcolor="#f0f0f0"
       Do While NOT RS.EOF' AND cnt < RS.PageSize
           If bgcolor="#f0f0f0" Then
               bgcolor=""
           ElseIf bgcolor = "" Then
                 bgcolor="#f0f0f0"
           End If
       %>
       <tr bgcolor="<%=bgcolor%>">
        <td valign="top"><%=RS("thumbnail")%></td>
        <td valign="top"><a href="addedit.asp?id=<%=RS("id")%>"><%=RS("Brand")%></a></td>
        <td valign="top"><%=RS("Tagline")%></td>
        </tr>

       <%
           RS.MoveNext
       Loop
       %>
   </table>

<%
End If 'If RS.EOF
%>

<%
RS.Close
Set RS = nothing
Conn.Close
Set Conn = nothing
%>
<br><br><div align="center">
<a href="#top">Back to Top</a></div>




addedit.asp
=======
<!--#include virtual="/adovbs.inc"-->
<link rel="stylesheet" href="../stylesheet.css">
 
 
<%
If Request.Form("add") <> "" OR Request.QueryString("add") <> "" Then addit = true
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("brands")

If NOT addit Then
    id = Request.QueryString("id")
    If id = "" Then id = Request.Form("id")
    Set RS = Server.CreateObject("ADODB.Recordset")
    sql = "SELECT * from brands WHERE id = " & CLng(id)
    RS.Open sql, Conn', adOpenKeyset, adLockPessimistic
      set fromwhere = RS
Else
      set fromWhere = Request.Form
End If
%>

 
<table border="0" class="text"><tr><td valign="top">
             <b><%If addit Then%>Add<%Else%>Edit<%End If%>&nbsp;Job Title - </b><a href="javascript:history.back()" class="reviews">Back to List</a>
</td></tr></table>


            <table border="0">
                  <tr>
                        <td>
                              <!--#include file="_form.asp"-->
                        </td>
                  </tr>
            </table>

 

<%
If NOT addit Then
    RS.Close
      Set RS = nothing
End If

Conn.Close
Set Conn = nothing

%>





_form.asp
=======
(this is actually much longer, but just so you get the jist of what it looks like)
<tr bgcolor="#f0f0f0">
       <td  valign="top" nowrap>Promo 3:</td>
       <td nowrap><input type="text" name="promo3" id="promo3"  maxlength="255" value="<%=fromWhere("promo3")%>"></td>
       <td width="100%">&nbsp;</td>
   </tr>
   <tr bgcolor="#8097C0">
       <td></td>
       <td colspan="2"><!--explanation text goes here--></td>
   </tr>
   
     
   <tr bgcolor="#8097C0">
       <td></td>
       <td colspan="2"></td>
   </tr>
   </tr>
   <%If NOT addit Then%>
   <tr>
       <td>&nbsp;</td>
       <td><br>
           <input type="submit" value="Edit Posting">
           </form>
           <form action="submit.asp" method="post" onSubmit="return confirm_delete()">
              <input type="hidden" name="id" id="id" value="<%=fromWhere("id")%>">
               <input type="submit" value="Delete Posting" name="remove"><br>
           </form>
       </td>
   </tr>
</table>
<%Else%>
   <tr>
       <td>&nbsp;</td>
       <td><br><input type="submit" name="add" value="Add Job Posting"></td>
   </tr>
</table>

</form>
<%End If%>






submit.asp
=======
<link rel="stylesheet" href="/stylesheet.css">

<!--#include virtual="/adovbs.inc"-->
 
 
<%
'return_field = Trim(Request.Form("headline"))

Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("brands")

Set RS = Server.CreateObject("ADODB.Recordset")

id = Request.Form("id")
If Request.Form("remove") <> "" Then remove = true
If Request.Form("add") <> "" Then addit = true

If remove Then
      sql = "DELETE FROM brands WHERE id = " & id
      Conn.Execute(sql)
      Response.Redirect("default.asp?delete=1&name=" & Server.URLEncode(return_field))
Else

      If NOT errs Then
       
       If addit Then
               RS.Open "brands", Conn, adOpenKeyset, adLockOptimistic, adCmdTable
               RS.AddNew
          Else
               sql = "SELECT * FROM brands WHERE id = " & id
               RS.Open sql, Conn, adOpenKeyset, adLockOptimistic
               RS.Edit        '<------------- The fix you need
       
                  'sql = "SELECT * FROM brands WHERE id = " & id
                  'RS.Open sql, Conn, adOpenKeyset, adLockOptimistic
       End If

          'RS.Fields("id") = Request.Form("id")
              RS.Fields("brand") = Request.Form("brand")
              RS.Fields("logo") = Request.Form("logo")
              RS.Fields("tagline") = Request.Form("tagline")
              RS.Fields("pof_icon") = Request.Form("pof_icon")
              RS.Fields("best_practice1") = Request.Form("best_practice1")
              RS.Fields("best_practice2") = Request.Form("best_practice2")
              RS.Fields("best_practice3") = Request.Form("best_practice3")
              RS.Fields("best_practice4") = Request.Form("best_practice4")
              RS.Fields("rebate1_link") = Request.Form("rebate1_link")
              RS.Fields("rebate1") = Request.Form("rebate1")
              RS.Fields("rebate2_link") = Request.Form("rebate2_link")
              RS.Fields("rebate2") = Request.Form("rebate2")
              RS.Fields("rebate3") = Request.Form("rebate3")
              RS.Fields("rebate4") = Request.Form("rebate4")
              RS.Fields("foodguide1_link") = Request.Form("foodguide1_link")
              RS.Fields("foodguide1") = Request.Form("foodguide1")
              RS.Fields("foodguide2") = Request.Form("foodguide2")
              RS.Fields("foodguide3") = Request.Form("foodguide3")
              RS.Fields("caption") = Request.Form("caption")
              RS.Fields("promo1") = Request.Form("promo1")
              RS.Fields("promo2") = Request.Form("promo2")
              RS.Fields("promo3") = Request.Form("promo3")
              RS.Fields("description") = Request.Form("description")
              RS.Fields("thumbnail") = Request.Form("thumbnail")
             
         
             
         
               RS.Update
               RS.Close
            '*********************************
       
       If addit Then
                  Response.Redirect("default.asp?add=1&name=" & Server.URLEncode(return_field))
            Else
                  Response.Redirect("default.asp?edit=1&name=" & Server.URLEncode(return_field))
            End If
      End If
 End if
 

Set RS = nothing
Set fromWhere = Request.Form
%>

 

            <h3 class="bold"><% If addit Then%>Add<%Else%>Edit<%End If%>&nbsp;Article</h3>

            <table border="0">
                  <tr>
                        <td>
                              <%If errs Then%>
                                    <span class="bold" style="color:#FF0000">Please provide information for all required fields and check to make sure the information you provided is accurate.</span>
                                    <!--#include file="_form.asp"-->
                              <%End If%>
                        </td>
                  </tr>
            </table>

 

<%
Conn.Close
Set Conn = nothing

%>
 
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
after this lines of code
          Else
               sql = "SELECT * FROM brands WHERE id = " & id
               RS.Open sql, Conn, adOpenKeyset, adLockOptimistic

try to add this

RS.Update



Also check check the link below might help..

http://www.adopenstatic.com/faq/80040e21.asp
http://support.microsoft.com/default.aspx?scid=kb;en-us;228935
I just accepted that answer though my problem wasn't actually solved.  Anyway, thanks for everyone's help...