Forum Moderators: open

Message Too Old, No Replies

ASP Error Messages

How to catch DB errors?

         

txbakers

2:07 pm on Jan 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For example, an ID field is a unique key. The user unwittingly enters a duplicate ID and hits submit.

The error screen is returned informing them of a duplicate value.

I can't seem to find information on how to avoid this before it happens.

Can anyone share with me?

Thanks.

circuitjump

3:17 pm on Jan 25, 2002 (gmt 0)

10+ Year Member



You're looking for a way for it not to tell you that you have a duplicate value?

If thats the case, just have it save the ID number in a variable and have ASP check that variable with the DB using a WHILE loop or a FOR
loop.

circuitjump

3:40 pm on Jan 25, 2002 (gmt 0)

10+ Year Member



Oh, I forgot to add, you the loop before you execute the SQL command that will insert the info you want into the DB.

So for example

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

' Make a DSN-less connection to the DB
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & _
Server.MapPath("test.mdb")

Dim ID_number, Check_DB

ID_number = Request.Form("id_input")

Check_DB ="SELECT id FROM table_name"

RS.Open Check_DB, Conn

While Not RS.EOF
If RS("id") = ID_number Then
Response.Write "The ID number you have chosen is already taken.<br>" & ID_number
End If
Wend

Something like that

txbakers

9:58 pm on Jan 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks CJ, that did the trick. Here is the final version of the code if anyone is interested:

var idn = document.stuForm.id.value;
<% if (!rsStudents.EOF ) { %>
 <% while (!rsStudents.EOF) { %>
   //alert("<%=rsStudents.Fields.Item("id").Value %>");
   if (idn == <%=rsStudents.Fields.Item("id").Value %> ) {
    alert("There is already a student with this id number.");
    document.stuForm.id.value="";
    document.stuForm.id.focus();
    return false;
  }
 <%rsStudents.MoveNext(); } %>
<% } %>

circuitjump

4:38 pm on Jan 28, 2002 (gmt 0)

10+ Year Member



Glad I could be of help. :)