Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2006, 03:55 PM
Registered User
 
Join Date: Sep 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Incrementing PK (int) before Insert record

Hello all,

I am currently building a Insert record feature, which will increment the PK (CustomerID) by one, and then add the fields. In classic asp it was simple, you just read in the value, store it, add 1 then pass it back. Is it still the same in ASP.NET?

I threw some code up and got this far:

Code:
Public Class InsertCustomer

    Private ReadOnly _conString As String

    Public Function GetCusID() As SqlDataReader
        'create command
        Dim con As New SqlConnection(_conString)
        Dim cmd As New SqlCommand()
        Dim CusId As Integer
        cmd.Connection = con
        cmd.CommandText = "SELECT MAX(CustomerId) From Customers"
        con.Open()
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        While reader.Read
            CusId = CType(reader("CustomerID"), Integer)
            CusId = CusId + 1
            'While GetCusID.Read

        End While
        'Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
    End Function
    Public Sub New()
        _conString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    End Sub



End Class
Is there any simpler way to do it? IE without writing a VB Class?
Much appericiated

 
Old September 19th, 2006, 04:42 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

The easiest way to do it would be to set the PK to an auto inc field so the SQL Server incremented the value for you.

Probably what i would do, as opposed to writing a class in code, I would write a stored procedure that would handle everything for me and all i would have to do is pass in the values of my insert statement.

My 2 cents anyway.

--Stole this from a moderator

I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert where record doesn't already exist Neal SQL Server 2000 3 February 4th, 2008 10:09 AM
Insert record disabled jking Access 9 January 10th, 2006 04:31 PM
Insert new Record priority05 Classic ASP Databases 2 September 6th, 2004 12:26 PM
Insert a record into a Database ersp Classic ASP Basics 3 May 9th, 2004 08:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.