PDA

View Full Version : How can I check if a record exists before inserting


doggyDivemaster
05-05-2009, 05:40 AM
I need to check whether a record already exists with the same description as that in a form being submitted for insert by DataAssist and, if it does, don't insert and redirect.

Previously, I could have fudged it by using Dreamweaver's 'Check New Username' behaviour and substituting my description field for the behaviour's username.

I need to use the DataAssist insert record behaviour because I am also using Digital File Pro on the same page but Check New User Name doesn't work with DataAssist's insert record behaviour.

Is their an easy way to check if a record exists prior to insert using WebAssist's tools?

Danilo Celic
05-05-2009, 06:54 AM
One way to do this would be to apply to a test page the built in Dreamweaver Insert Record and the Check New User Name using the same connection/table/column that you want to check in the "real" page. Then copy and paste the code for the Check New Username over to your real page.

This method will likely show a red exclamation mark next to the Check New Username in the Server Behaviors panel due to there not being a built in Dreamweaver Insert Record on the page, but it should work for you as long as you make sure to copy over all of the Check New Username code.

doggyDivemaster
05-05-2009, 07:45 AM
Thanks, I needed to check whether there was a built-in DataAssist behaviour but as there wasn't, I have followed your advice.

Because I was ALSO inserting the filename from a file uploaded by WA DigitalFilePro, I had to modify the Check New User Name code.

Here it is for anyone else reading this post. I have commented out the Dreanweaver lines I had to modify.

' *** Redirect if username exists
'MM_flag = "MM_insert" ***** Removed
'If (CStr(Request(MM_flag)) <> "") Then *****Replaced code with the line below
if (cStr(WA_DFP_UploadObj.Form("Insert.x")) <> "") Then
Dim MM_rsKey
Dim MM_rsKey_cmd

MM_dupKeyRedirect = "index.asp"
'MM_dupKeyUsernameValue = CStr(Request.Form("jobDescription")) *****Replaced code with the line below
MM_dupKeyUsernameValue = cStr(WA_DFP_UploadObj.Form("jobDescription"))
Set MM_rsKey_cmd = Server.CreateObject ("ADODB.Command")
MM_rsKey_cmd.ActiveConnection = MM_printShop_STRING
MM_rsKey_cmd.CommandText = "SELECT jobDescription FROM jobDescriptions WHERE jobDescription = ?"
MM_rsKey_cmd.Prepared = true
MM_rsKey_cmd.Parameters.Append MM_rsKey_cmd.CreateParameter("param1", 200, 1, 50, MM_dupKeyUsernameValue) ' adVarChar
Set MM_rsKey = MM_rsKey_cmd.Execute
If Not MM_rsKey.EOF Or Not MM_rsKey.BOF Then
' the username was found - can not add the requested username
MM_qsChar = "?"
If (InStr(1, MM_dupKeyRedirect, "?") >= 1) Then MM_qsChar = "&"
MM_dupKeyRedirect = MM_dupKeyRedirect & MM_qsChar & "requsername=" & MM_dupKeyUsernameValue
Response.Redirect(MM_dupKeyRedirect)
End If
MM_rsKey.Close
End If
%>


SUGGESTION - You should add this invaluable behaviour to DataAssist

Ray Borduin
05-05-2009, 03:34 PM
Another option is to create a recordset filtered by the field you want unique and then use validation toolkit to validate the result returns zero records. That is the method I prefer.

Cologne
05-06-2009, 09:06 AM
How does that work?

Ray Borduin
05-06-2009, 09:25 AM
You create a recordset filtered by the form element, say the email address... then that recordset will set a record count variable. You use that variable in validation toolkit and validate it as a number that is equal to zero. If it isn't zero the validation fails.

This is how it is done in the User Registration Solution and the Powerstore solution if you happen to have them as examples.

Cologne
05-06-2009, 12:25 PM
No I dont have em. I got Validation tool kit, security assist and dataassist.

Hmm, I dont understand that. where do i create the record? in the page of the form?

Ray Borduin
05-11-2009, 05:41 AM
You create the recordset and add validation on the action page of the form, the same page that has the insert server behavior applied.

Ray Borduin
05-11-2009, 05:41 AM
Look through the forums, I have walked several people through the process in the past.

paul377172
05-11-2009, 06:29 AM
I've read through the other forums and looked at the run through, but I'm using asp it doesn't help reading the php coding.

I know you create a recordset and filter it by the field you want to use but when you goto server vaildation, what do you choose as your validation type "number" ? then do you choose server variable as total records in the recordset.

Whatever I have tried hasn't worked for me

Your support here would be appreciated.

Ray Borduin
05-11-2009, 06:42 AM
That should work. Type number and use the recordcount of your recordset. What happens when you try. What is the resulting code? Your seem to understand the theory, now the problem is with execution.

paul377172
05-11-2009, 07:00 AM
Hi nothing happens, it just enters the record.

The code for the recordset and validation

<%
Dim Recordset1
Dim Recordset1_cmd
Dim Recordset1_numRows

Set Recordset1_cmd = Server.CreateObject ("ADODB.Command")
Recordset1_cmd.ActiveConnection = MM_hostspaced_STRING
Recordset1_cmd.CommandText = "SELECT Title FROM software WHERE Title = ?"
Recordset1_cmd.Prepared = true
Recordset1_cmd.Parameters.Append Recordset1_cmd.CreateParameter("param1", 200, 1, 255, Recordset1__MMColParam) ' adVarChar

Set Recordset1 = Recordset1_cmd.Execute
Recordset1_numRows = 0
%>

<%
if (Request.ServerVariables("Request_Method") = "POST") then
WAFV_Redirect = ""
Session("WAVT_insertproduct_Errors") = ""
if (WAFV_Redirect = "") then
WAFV_Redirect = cStr(Request.ServerVariables("SCRIPT_NAME"))
end if
WAFV_Errors = ""
WAFV_Errors = WAFV_Errors & WAValidateNM((Recordset1_total) & "",0,0,"",",.",true,4)

if (WAFV_Errors<> "") then
PostResult WAFV_Redirect,WAFV_Errors,"insertproduct"
end if
end if
%>

Ray Borduin
05-11-2009, 07:11 AM
Below your recordset you would need to add the code to get the recordcount. It is added by DW when you add the record count variable to the page, but you can add it manually it is:

<%
' *** Recordset Stats, Move To Record, and Go To Record: declare stats variables

' set the record count
Recordset1_total = Recordset1.RecordCount

' set the number of rows displayed on this page
If (Recordset1_numRows < 0) Then
Recordset1_numRows = Recordset1_total
Elseif (Recordset1_numRows = 0) Then
Recordset1_numRows = 1
End If

' set the first and last displayed record
Recordset1_first = 1
Recordset1_last = Recordset1_first + Recordset1_numRows - 1

' if we have the correct record count, check the other stats
If (Recordset1_total <> -1) Then
If (Recordset1_first > Recordset1_total) Then Recordset1_first = Recordset1_total
If (Recordset1_last > Recordset1_total) Then Recordset1_last = Recordset1_total
If (Recordset1_numRows > Recordset1_total) Then Recordset1_numRows = Recordset1_total
End If
%>
<%
' *** Recordset Stats: if we don't know the record count, manually count them

If (Recordset1_total = -1) Then

' count the total records by iterating through the recordset
Recordset1_total=0
While (Not Recordset1.EOF)
Recordset1_total = Recordset1_total + 1
Recordset1.MoveNext
Wend

' reset the cursor to the beginning
If (Recordset1.CursorType > 0) Then
Recordset1.MoveFirst
Else
Recordset1.Requery
End If

' set the number of rows displayed on this page
If (Recordset1_numRows < 0 Or Recordset1_numRows > Recordset1_total) Then
Recordset1_numRows = Recordset1_total
End If

' set the first and last displayed record
Recordset1_first = 1
Recordset1_last = Recordset1_first + Recordset1_numRows - 1
If (Recordset1_first > Recordset1_total) Then Recordset1_first = Recordset1_total
If (Recordset1_last > Recordset1_total) Then Recordset1_last = Recordset1_total

End If
%>


Currently it looks like the total isn't set correctly so it is always zero and alway valid.

paul377172
05-11-2009, 07:31 AM
Sorry its just keeps going back to the insert_record page so it must showing as invalid everytime. Sorry but it was actually doing that before.

The full code is

<%
Dim Recordset1
Dim Recordset1_cmd
Dim Recordset1_numRows

Set Recordset1_cmd = Server.CreateObject ("ADODB.Command")
Recordset1_cmd.ActiveConnection = MM_hostspaced_STRING
Recordset1_cmd.CommandText = "SELECT Title FROM software WHERE Title = ?"
Recordset1_cmd.Prepared = true
Recordset1_cmd.Parameters.Append Recordset1_cmd.CreateParameter("param1", 200, 1, 255, Recordset1__MMColParam) ' adVarChar

Set Recordset1 = Recordset1_cmd.Execute
Recordset1_numRows = 0
%>
<%
' *** Recordset Stats, Move To Record, and Go To Record: declare stats variables

' set the record count
Recordset1_total = Recordset1.RecordCount

' set the number of rows displayed on this page
If (Recordset1_numRows < 0) Then
Recordset1_numRows = Recordset1_total
Elseif (Recordset1_numRows = 0) Then
Recordset1_numRows = 1
End If

' set the first and last displayed record
Recordset1_first = 1
Recordset1_last = Recordset1_first + Recordset1_numRows - 1

' if we have the correct record count, check the other stats
If (Recordset1_total <> -1) Then
If (Recordset1_first > Recordset1_total) Then Recordset1_first = Recordset1_total
If (Recordset1_last > Recordset1_total) Then Recordset1_last = Recordset1_total
If (Recordset1_numRows > Recordset1_total) Then Recordset1_numRows = Recordset1_total
End If
%>
<%
' *** Recordset Stats: if we don't know the record count, manually count them

If (Recordset1_total = -1) Then

' count the total records by iterating through the recordset
Recordset1_total=0
While (Not Recordset1.EOF)
Recordset1_total = Recordset1_total + 1
Recordset1.MoveNext
Wend

' reset the cursor to the beginning
If (Recordset1.CursorType > 0) Then
Recordset1.MoveFirst
Else
Recordset1.Requery
End If

' set the number of rows displayed on this page
If (Recordset1_numRows < 0 Or Recordset1_numRows > Recordset1_total) Then
Recordset1_numRows = Recordset1_total
End If

' set the first and last displayed record
Recordset1_first = 1
Recordset1_last = Recordset1_first + Recordset1_numRows - 1
If (Recordset1_first > Recordset1_total) Then Recordset1_first = Recordset1_total
If (Recordset1_last > Recordset1_total) Then Recordset1_last = Recordset1_total

End If
%>


<%
if (Request.ServerVariables("Request_Method") = "POST") then
WAFV_Redirect = ""
Session("WAVT_insertproduct_Errors") = ""
if (WAFV_Redirect = "") then
WAFV_Redirect = cStr(Request.ServerVariables("SCRIPT_NAME"))
end if
WAFV_Errors = ""
WAFV_Errors = WAFV_Errors & WAValidateNM((Recordset1_total) & "",0,0,"",",.",true,1)

if (WAFV_Errors<> "") then
PostResult WAFV_Redirect,WAFV_Errors,"insertproduct"
end if
end if
%>
<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
If (Request.QueryString("Title") <> "") Then
Recordset1__MMColParam = Request.QueryString("Title")
End If
%>

In that order.

Thanks

Ray Borduin
05-11-2009, 09:00 AM
Well this code should be at the top above the recordset:
<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
If (Request.QueryString("Title") <> "") Then
Recordset1__MMColParam = Request.QueryString("Title")
End If
%>

If that doesn't fix it, I guess do some debugging... update:


WAFV_Errors = ""
WAFV_Errors = WAFV_Errors & WAValidateNM((Recordset1_total) & "",0,0,"",",.",true,1)


and add some debug code like:


WAFV_Errors = ""
WAFV_Errors = WAFV_Errors & WAValidateNM((Recordset1_total) & "",0,0,"",",.",true,1)

Response.Write("total: " & Recordset1_total & "<BR>")
Response.Write(WAFV_Errors)
Response.End

See what that says. It looks like it should work to me. Post back with the result and I see if I can spot the problem with more information.

paul377172
05-11-2009, 09:19 AM
Hi I added that bit of code to the top like you said and it now its validating everything.

I added the debugging code where you said and it now comes with total: 0 after submitting form.

Thanks

mustang_sally_85344510
05-11-2009, 09:27 AM
I having the same issues. I tried this idea. And a couple of problems...when you make a test page, you have to make a form (i called my test) when you copy and paste this over to the other page, it has it's own form. This didn't work for me, it still allowed me to enter information..? :(

paul377172
05-11-2009, 09:31 AM
Yes Sally thats the method that doesn't use webassist software, which I haven't tried myself. Have you tried the method Ray is suggesting? It was so easy with addt.

Ray Borduin
05-11-2009, 09:58 AM
OK, so the problem now is that the Total number is still zero, even when it should be returning a record? or is it working now?

paul377172
05-11-2009, 10:03 AM
Thats right its returning zero everytime even when i put in a title which already exists

Ray Borduin
05-11-2009, 10:20 AM
OK, I'm not sure of the problem, so lets try a different aproach.

Lets try removing all the recordcount stuff and do something like:

<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
If (Request.QueryString("Title") <> "") Then
Recordset1__MMColParam = Request.QueryString("Title")
End If
%>
<%
Dim Recordset1
Dim Recordset1_cmd
Dim Recordset1_numRows

Set Recordset1_cmd = Server.CreateObject ("ADODB.Command")
Recordset1_cmd.ActiveConnection = MM_hostspaced_STRING
Recordset1_cmd.CommandText = "SELECT Count(Title) AS TitleCount FROM software WHERE Title = ?"
Recordset1_cmd.Prepared = true
Recordset1_cmd.Parameters.Append Recordset1_cmd.CreateParameter("param1", 200, 1, 255, Recordset1__MMColParam) ' adVarChar

Set Recordset1 = Recordset1_cmd.Execute
Recordset1_numRows = 0
%>

<%
if (Request.ServerVariables("Request_Method") = "POST") then
WAFV_Redirect = ""
Session("WAVT_insertproduct_Errors") = ""
if (WAFV_Redirect = "") then
WAFV_Redirect = cStr(Request.ServerVariables("SCRIPT_NAME"))
end if
WAFV_Errors = ""
WAFV_Errors = WAFV_Errors & WAValidateNM((Recordset1.fields("TitleCount")) & "",0,0,"",",.",true,1)

if (WAFV_Errors<> "") then
PostResult WAFV_Redirect,WAFV_Errors,"insertproduct"
end if
end if
%>

paul377172
05-11-2009, 10:31 AM
Sorry its still doing the same thing.

Ray Borduin
05-11-2009, 10:43 AM
Are you using a form method="POST" or "GET"? The problem may be with the way you set your parameter.... try:

<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
If (Request("Title") <> "") Then
Recordset1__MMColParam = Request("Title")
End If
%>

paul377172
05-11-2009, 10:52 AM
yes yes yes, I think you have cracked it. Thanks so much for your patience.

Ray Borduin
05-11-2009, 10:56 AM
Probably most of the earlier examples would have worked too ;)

Cologne
06-03-2009, 12:12 PM
Ray I got it :-)

The trickm is to put the validation AFTER the Recodset :-)

Denis