Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: validation, insertion


Message #1 by jay48202@y... on Sat, 5 Oct 2002 21:13:10
Hello everybody, 

 I am a newbie in ASP, and i've got a problem 


Here's the situation.

  I have a page (page1.asp) where the user is required to key in all 
their details for registration. I will later send all the details entered 
to another page (page2.asp) where I will then save it to a database.I am 
perfectly able to achieve this.

My problem is I am able to validate the text box to make sure that it is 
not empty and yelling at user that it is empty. but after pressing OK of 
the MsgBox, a blank record is getting inserted in the database. I made 
sure in the database table that particular field to be not null and set 
it as Primary key. Next time, when user visits the page1.asp and clicks 
submit button without filling txtbox,MsgBox appears and then the error 
message saying Primary key and about duplication. This duplication is all 
regarding to that blank entry. Help me.

page1.asp
---------

<HTML>
<HEAD>
<TITLE> Select the Part Type</TITLE>

<SCRIPT LANGUAGE="VBSCRIPT">

Dim Validation
Function frm1_OnSubmit()
	Validation=True
	If(document.frm1.txtPType.Value)="" Then
		MsgBox "Please fill in the Part Type"
		Validation=False
	End If
	If Validation=True Then
		return Validation
	Else
		return Validation
	End If
End Function
</SCRIPT>
</HEAD>
<BODY>
<P>
<form  name="frm1" onSubmit="frm1_OnSubmit()" action="page2.asp" 
method="POST">

<%	
Set Conn = Server.CreateObject("ADODB.Connection") 
Conn.Open ("FILEDSN=" + Server.MapPath("Ppk.dsn") + ";")
	

Set rs = Server.CreateObject("ADODB.Recordset") 
 
   rs.ActiveConnection=Conn
   rs.Open "Select * from tbLbls"
   %>
	<h4>Enter the name of the Part Type here</h4>
	<input type="text" name="txtPType" size=30><br>
	<h4>Click the "Add Part Type" button to add a part type</h4>
	<input type="Submit" value="Add Part Type"><br>
	<h4>Clear the contents</h4>
	<input type="reset" value="Clear"><br>
</form> 

<%
rs.close
rs.ActiveConnection=Nothing
set rs=Nothing
set Conn=Nothing
%></P>


</BODY>
</HTML>

page2.asp
----------

<html>
<head></head>
<body>

  
<Form>
<%
   Dim txtPartType   
   txtPartType = Request.Form("txtPType")
   
   
   Set Conn = Server.CreateObject("ADODB.Connection") 
   Conn.Open ("FILEDSN=" + Server.MapPath("Ppk.dsn") + ";")
   
sql_insert = "insert into tbLbls(PartType) values ('"&txtPartType&"')"
            

   Conn.Execute sql_insert
  
	set Conn=Nothing
	'Response.Redirect "numLabels.asp"
 %>
</Form>
</body>
</html>



Can anybody please help me solve this problem?


 Thanx in advance
Message #2 by "Peter Foti (PeterF)" <PeterF@S...> on Mon, 7 Oct 2002 09:37:27 -0400
First, let me say that you should be using Javascript here instead of
VBScript, since this is a client side script.  VBScript is not supported by
browsers other than Internet Explorer.  

Also, if the user has scripting disabled, then you will still have problems
with invalid data being sent.  You would be better off performing the
validation at the server, and if the user enters invalid or incomplete data,
you redirect them back to this page.  That way, you don't need to worry what
the client is using.

Regards,
Peter




> -----Original Message-----
> From: jay48202@y... [mailto:jay48202@y...]
> Sent: Saturday, October 05, 2002 9:13 PM
> To: ASP Databases
> Subject: [asp_databases] validation, insertion
> 
> 
> Hello everybody, 
> 
>  I am a newbie in ASP, and i've got a problem 
> 
> 
> Here's the situation.
> 
>   I have a page (page1.asp) where the user is required to key in all 
> their details for registration. I will later send all the 
> details entered 
> to another page (page2.asp) where I will then save it to a 
> database.I am 
> perfectly able to achieve this.
> 
> My problem is I am able to validate the text box to make sure 
> that it is 
> not empty and yelling at user that it is empty. but after 
> pressing OK of 
> the MsgBox, a blank record is getting inserted in the 
> database. I made 
> sure in the database table that particular field to be not 
> null and set 
> it as Primary key. Next time, when user visits the page1.asp 
> and clicks 
> submit button without filling txtbox,MsgBox appears and then 
> the error 
> message saying Primary key and about duplication. This 
> duplication is all 
> regarding to that blank entry. Help me.
> 
> page1.asp
> ---------
> 
> <HTML>
> <HEAD>
> <TITLE> Select the Part Type</TITLE>
> 
> <SCRIPT LANGUAGE="VBSCRIPT">
> 
> Dim Validation
> Function frm1_OnSubmit()
> 	Validation=True
> 	If(document.frm1.txtPType.Value)="" Then
> 		MsgBox "Please fill in the Part Type"
> 		Validation=False
> 	End If
> 	If Validation=True Then
> 		return Validation
> 	Else
> 		return Validation
> 	End If
> End Function
> </SCRIPT>
> </HEAD>
> <BODY>
> <P>
> <form  name="frm1" onSubmit="frm1_OnSubmit()" action="page2.asp" 
> method="POST">
> 
> <%	
> Set Conn = Server.CreateObject("ADODB.Connection") 
> Conn.Open ("FILEDSN=" + Server.MapPath("Ppk.dsn") + ";")
> 	
> 
> Set rs = Server.CreateObject("ADODB.Recordset") 
>  
>    rs.ActiveConnection=Conn
>    rs.Open "Select * from tbLbls"
>    %>
> 	<h4>Enter the name of the Part Type here</h4>
> 	<input type="text" name="txtPType" size=30><br>
> 	<h4>Click the "Add Part Type" button to add a part type</h4>
> 	<input type="Submit" value="Add Part Type"><br>
> 	<h4>Clear the contents</h4>
> 	<input type="reset" value="Clear"><br>
> </form> 
> 
> <%
> rs.close
> rs.ActiveConnection=Nothing
> set rs=Nothing
> set Conn=Nothing
> %></P>
> 
> 
> </BODY>
> </HTML>
> 
> page2.asp
> ----------
> 
> <html>
> <head></head>
> <body>
> 
>   
> <Form>
> <%
>    Dim txtPartType   
>    txtPartType = Request.Form("txtPType")
>    
>    
>    Set Conn = Server.CreateObject("ADODB.Connection") 
>    Conn.Open ("FILEDSN=" + Server.MapPath("Ppk.dsn") + ";")
>    
> sql_insert = "insert into tbLbls(PartType) values ('"&txtPartType&"')"
>             
> 
>    Conn.Execute sql_insert
>   
> 	set Conn=Nothing
> 	'Response.Redirect "numLabels.asp"
>  %>
> </Form>
> </body>
> </html>
> 
> 
> 
> Can anybody please help me solve this problem?
> 
> 
>  Thanx in advance
> 

  Return to Index