Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: OleDb Sample Code for Insert and Update


Message #1 by rdalton@c... on Wed, 11 Sep 2002 23:32:49
Hi,

I am just starting with asp.net.  I have a number of existing asp pages I 
would like to convert.  These existing pages use generic code for adding 
and updating information in access databases.  Since the code is standard 
I can teach other people how to update it and make basic changes without 
their having to understand or work with SQL statements.

I have two WROX books on using asp.net with visual basic, and the examples 
there are very clear for when you want to use SQL statements, or when you 
are binding data to an element in a page.  I don't want to do either.  I 
would prefer to not build SQL statements each time, and while the update 
functions will work well with a binded element the input functions do not.

Can anyone please help by reviewing the asp code below and making some 
suggestions on how to complete the same type of standard code block using 
asp.net?

The following example would add the strNTitle variable that was set via 
strNTitle = request.form("strNTitle") as a field in a new row in the 
database.  The connection is already opened and called gobjConnection.

		SET rs = Server.CreateObject("ADODB.Recordset")
		rs.ActiveConnection = gobjConnection
		rs.CursorType = 1
		rs.LockType = 3
		rs.Source = "tblNews"
		rs.Open
		rs.AddNew
		rs("strNTitle") = strNTitle
		rs.Update
		rs.close
		set rs = nothing

The following example would do the same thing, but it would update an 
existing database row.

		Set cmdDC = Server.CreateObject("ADODB.Command")
		cmdDC.ActiveConnection = gobjConnection
		SQL_txt = "SELECT * FROM tblNews WHERE intNewsID=" & 
request.form("NewsID")
		cmdDC.CommandText = SQL_txt
		Set rs_update = Server.CreateObject("ADODB.Recordset")
		rs_update.Open cmdDC, , 0, 2
		rs_update.Fields("strNTitle") = strNTitle
		rs_update.update
		rs_update.close
		set rs_update=nothing
		set cmdDC = nothing


I have not been able to find any articles that explain this type of 
methodology simply.  If you have any articles that you would suggest 
please let me know.  Most articles that I have seen widen the topic with 
excitement over the validator capabilities or with discussions on the 
benefits of being able to retrieve and bind data to elements on the page.  
I just want to be able to simply add data to the database and then look 
into these other features later.

Thanks,

Raymond Dalton


  Return to Index