Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspdotnet_website_programming thread: Insert into database table problems


Message #1 by "erik" <w_erik@h...> on Tue, 24 Sep 2002 19:58:11
I wants to create a webpages that could put data from asp:textbox into 
database. I Believe my code is correct, but I kept on recieve an 
error "Syntax error in INSERT INTO statement." 

can anyone help me find out whats wrong with my code? 

following is the part of the code that produce that error messages:

<%@ Page Language="VB" debug="true"%>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.oledb" %>
<script runat="server">
        	
	Sub registerMe_click(sender as object, e as eventargs)
        dim objconnection as oledbconnection
        dim objcommand as oledbcommand
  	dim strconnect as string
  	dim strcommand as string
  	'dim dataset1 as new dataset

	dim sqlusername as string
	dim sqlemail as string
	dim sqlpassword as string

	sqlusername = txtusername.text
        sqlemail = txtemail.text
        sqlpassword = txtpassword.text

 strconnect = "provider=Microsoft.jet.oledb.4.0;" & _
 "data source=" & Server.mapPath("\wijayaer\db\coolfellowdb.mdb") & ";" & _
 "persist security info=false"
        
        'create connection object
        'if wants to use objcommand you need to open connection manualy
        objconnection = new oledbconnection(strconnect)
        objconnection.open()
	
strcommand = "INSERT INTO userinfo(username,email,password) values " & _
        "('" & sqlusername & "','" & sqlemail & "','" & sqlpassword & "')"

        'create a command and set its properties
        objcommand = new oledbcommand(strcommand, objconnection)
        
        'execute the command
        objcommand.ExecuteNonquery()
        lblusername.text = txtUsername.text
        lblemail.text = txtEmail.text
        lblpassword.text = txtpassword.text
        lblsqlusername.text = sqlusername
	end sub
</script>

<html>
<body>
<form runat="server">
<h1>Welcome to CoolFellows</h1>
<h2>Please fill the following fiels and click register to be register into 
CoolFellows </h2>


Username:
<asp:textbox ID="txtUsername" 
MaxLength="50" 
runat="server" 
ToolTip="Enter your user name" 
Width="10%" />
<br/><br/>


Email:
<asp:textbox ID="txtEmail" 
MaxLength="50" 
runat="server" 
ToolTip="Enter your email address" 
Width="10%" />
<br/><br/>

Password:
<asp:textbox ID="txtPassword" 
MaxLength="50" 
runat="server" 
ToolTip="Enter your password" 
Width="10%" />

<asp:button 
ID="btnRegisterMe" 
runat="server" 
Text="register me" 
ToolTip="click here to post the book name" 
OnClick="registerMe_click" />
<br/>
<br/>

you entered <br/><br/>

<asp:label
id= "lbluserName"
Font="courier"
fon-bold="true"
font-size="large"
runat="server" />

<br/> <br/>
<asp:label
id= "lblemail"
Font="courier"
fon-bold="true"
font-size="large"
runat="server" />
<br/><br/>

<asp:label
id= "lblpassword"
Font="courier"
fon-bold="true"
font-size="large"
runat="server" />

-- see of sqlusername has a value
<asp:label
id= "lblsqlusername"
Font="courier"
fon-bold="true"
font-size="large"
runat="server" />

</form>
</body>
</html>

I need help badly ^_^. thank you guys.
Message #2 by "Mike Gale" <info@d...> on Wed, 25 Sep 2002 07:58:35 +1200
erik wrote:
> I wants to create a webpages that could put data from asp:textbox into
> database. I Believe my code is correct, but I kept on recieve an
> error "Syntax error in INSERT INTO statement."
> 
> can anyone help me find out whats wrong with my code?
> 
...
> 
> I need help badly ^_^. thank you guys.

First I'd suggest emulating the technique used in the book.  You are
going direct from ASPX to database.  The book tends to go via business
tier, data tier and stored procedure.  There are good reasons for that
design.  If this is not clear follow the code through and make notes
until you are ready to stand up in front of a bunch of programmers and
describe it to them!

You can also capture the text of your SQL query and test that directly
against the database (with suitable editing to ensure no key clashes).

Mike Gale, Decision Engineering (NZ) Ltd.

Message #3 by "erik" <w_erik@h...> on Tue, 24 Sep 2002 21:27:51
> erik wrote:
> I wants to create a webpages that could put data from asp:textbox into
> database. I Believe my code is correct, but I kept on recieve an
> error "Syntax error in INSERT INTO statement."
> 
> can anyone help me find out whats wrong with my code?
> 
...
> 
> I need help badly ^_^. thank you guys.

First I'd suggest emulating the technique used in the book.  You are
going direct from ASPX to database.  The book tends to go via business
tier, data tier and stored procedure.  There are good reasons for that
design.  If this is not clear follow the code through and make notes
until you are ready to stand up in front of a bunch of programmers and
describe it to them!

You can also capture the text of your SQL query and test that directly
against the database (with suitable editing to ensure no key clashes).

Mike Gale, Decision Engineering (NZ) Ltd.

Message #4 by paul.tanswell@e... on Wed, 25 Sep 2002 09:31:20
Try putting square brackets around passwword in the SQL, sometimes this is 
a reserved word. ie "INSERT INTO userinfo(username,email,[password]) 
values " ....

Paul


  Return to Index