ASP.NET Webforms (insert, update, and delete records ) SQL Server and Visual Studio.net

  • Thread starter Thread starter kennethgrice
  • Start date Start date
K

kennethgrice

Hi,

I am new to ASP.net and visual studio.net.

I have created a new ASP.NET Web Application and have started with a
simple form.

I have created a simple test database that will store a person's first
and last name and their SSN. The primary key is an autonumbered field.

So I have made a connection in Visual Studio with the sql data adapter
and have dragged 3 text boxes on the web form.

For each text box, I have set the data source to be the field in the
database or (dataset)

So now I have dragged a button on to the form.

Do I put the insert, update, and delete code in the button properties?

Can visual studio.net generate the code to do this, or do I manually
have to write to code to do this. If it has to be done manually, could
someone please post sample code on how to do this?

My text box names are f_name, l_name, and ssn.
And the fields in the database are labeled the same way.


Thanks in advance for any help.
 
Hi Kenneth,

One way to do this is to create SQL Stored Procedures which will actually
perform the Insert/Update/Delete against the data, and then simply execute
them from your ASP.NET application.

If you're not familiar with SQL Stored Procedures, here's an example of what
one would look like that updates the f_name field of your database...
-------------------------------------------------------
CREATE PROCEDURE dbo.spf_nameUpdate
(
@ID varchar(50),
@f_name varchar(4)
)
AS
SET NOCOUNT OFF;

UPDATE TableName
SET f_name = @f_name
WHERE (ID = @ID)
GO
-------------------------------------------------------

After you've created the Stored Procedure, you can then add a SQLCommand
object to your webform (available in the toolbox, under the data tab.) Upon
adding the SQLCommand to your webform, you can simply follow the wizard to
specify the database and stored procedure it should connect to.

HTH,
Mike
 
If you eventually want/need to write your own code for database tasks,
you really ought to check out Microsoft's Data Access Application
Block. The latest version is currently part of the Enterprise Library
but I'm using an Oracle-specific adaptation of the original, standalone
version and it works great. Definitely helpful when you're trying to
follow common coding standards. You can find details about it here...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/daab.asp
 
Back
Top