Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a code example to work properly (adding rec to database) 1

Status
Not open for further replies.

JoeCool32

Programmer
Sep 26, 2002
50
US
I'm trying to use an code example from ASPFree.com for use in .NET, but I get an error during the build. It involves adding records to a database. It can also be found here and here .

At any rate, the error I get in the code-behind is this: 'Execute' is not a System.Data.SqlClient.SqlCommand' for the line in red.

With this in mind, what is the proper way to phrase it so the program will run and a record will be added?

I believe this example was orignally done on the Beta 1 version so that might explain why it doesn't work in .NET 1.0

Here's the whole of the code (I transfered the code for the .htm pg to .aspx, and the code for the original .aspx pg to .vb)

ASPX code:
<html>
<body>
<form method=&quot;POST&quot;>
<p>Name: <input type=&quot;text&quot; name=&quot;name&quot; size=&quot;20&quot;></p>
<p><input type=&quot;submit&quot; value=&quot;ADD&quot; name=&quot;B1&quot;>
<input type=&quot;reset&quot; value=&quot;RESET&quot; name=&quot;B2&quot;></p>
</form>
</body>
</html>


VB Code-Behind [Sean is the theoretical DB, SQLAdd is the theoretical table and NAME is the theoretical column]:

Sub Page_Load(Src as Object, E as EventArgs )
Dim myConnection as SQLConnection
myConnection = new SQLConnection(&quot;SERVER=; UID=; PWD=; DATABASE=Sean&quot;)

Dim myCommand as SQLCommand
myCommand = new SQLCommand(&quot;INSERT into SQLAdd (NAME) VALUES ('&quot; & Request.Form(&quot;name&quot;) & &quot;')&quot;, myConnection)

myConnection.Open()
myCommand.Execute()
myConnection.Close()
Response.Write(&quot;Records written to database&quot;)
End Sub JJ [peace]

&quot;Ignorance and prejudice and fear walk hand in hand&quot; - Witch Hunt, by Rush
 
Change Execute to ExecuteScalar() That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Oops you probably want ExecuteNonQuery actually. If you want a brief description of them check the tool tip in VS using Intellisense to bring up the list of execute methods for a command object. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hey, that works. Thanks yet again, Mark. :-D JJ [peace]

&quot;Ignorance and prejudice and fear walk hand in hand&quot; - Witch Hunt, by Rush
 
You are welcome Joe! That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top