homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

    
Insert New Record
stevelibby




msg:954529
 4:27 pm on Feb 25, 2005 (gmt 0)

Hi All
This answer will probably be a doddle for you clever lot.
I have created a table on ms sql server and i am trying to follow the w3schools below.

[w3schools.com...]

So Far so good, but i cant seem to create new records?
There are 3 fields 1 being id that is an "int" and i have set it as table indentity column in the manage indexes.
Have i done all right, what have i done wrontg.

 

martyt




msg:954530
 4:43 pm on Feb 25, 2005 (gmt 0)

Leave the identity column out of the insert statement. It'll get set automatically.

stevelibby




msg:954531
 4:51 pm on Feb 25, 2005 (gmt 0)

Hi
This is what i have done
sql="INSERT INTO Users (Fname,Lname"
sql=sql & "Fname,Lname)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("Fname") & "',"
sql=sql & "'" & Request.Form("Lname") & "',"
on error resume next
objconn.Execute sql,recaffected
if err<>0 then
Response.Write("No update permissions!")
else
Response.Write("<h3>" & recaffected & " record added</h3>")
end if
objconn.close
%>

Which in the info on the form, why doesnt it go into my db?

martyt




msg:954532
 5:22 pm on Feb 25, 2005 (gmt 0)

The sql string you're building has all sorts of syntax errors - column names duplicated, commas in the wrong place, missing closing paren, etc.

Print out the string you've built and see if you can execute the query directly in SQL Query Analyzer or Access or whatever you're using.

Or, better yet, figure out what the SQL query needs to look like using Query Analyzer or Access, then change the code so that it builds that string for you.

stevelibby




msg:954533
 5:34 pm on Feb 25, 2005 (gmt 0)

i have copied the query exactly as the web site has shown, so how can it be wrong?

martyt




msg:954534
 9:40 pm on Feb 25, 2005 (gmt 0)

sql="INSERT INTO Users (Fname,Lname) "
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("Fname") & "',"
sql=sql & "'" & Request.Form("Lname") & "')"

Easy_Coder




msg:954535
 2:24 pm on Feb 27, 2005 (gmt 0)

Just a suggestion...

This technique

sql=sql & "('" & Request.Form("Fname") & "',"

could potentially get you into trouble.

I'd suggest isolating the request items in variables, checking them for illegal characters and then processing them into your sql command accordingly.

aspdaddy




msg:954536
 9:29 pm on Feb 28, 2005 (gmt 0)

If you are using SQL server, you can avoid that technique of creating SQL altogether and just use stored procedures.

They are much simpler to write & debug, execute faster and more secure.

Create dbo.User_SAVE
@Fname varchar(25),
@Lname varchar(25)
AS
INSERT INTO Users (Fname,LName)
VALUES(@Fname,@LName)
GO

Then in the ASP:

objConn.Execute ("exec dbo.User_SAVE " & Request.Form("Fname") & " " & Request.Form("Lname") )

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved