CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> .NET

 Increment the primary key by one using C#

Print topic Send  topic

Author Message
mike

Posts: 7
Posted: 11/01/2004, 3:32 AM

I have a primary key varchar(7) in SQL Server , I know that I can use identity filed in SQL Server to increment the primary key , but my database is designed like this , What I need is How to increment the primary key in the record component when I add new record , in other way What is the code that I must but in What Event to get the maximum value of the primary key and convert it from string to int increment it by 1 then convert it to String and finally set the value of the primary key filed in the record form component when I request a new record .
I am Using ASP.NET C#
Please Help
View profile  Send private message
Benjamin Krajmalnik
Posted: 11/01/2004, 6:08 PM

I would use a custom insert statement, and prime the primary key field to
max(FIeldName)+1

"mike" <mike@forum.codecharge> wrote in message
news:841861ed6a1b95@news.codecharge.com...
> I have a primary key varchar(7) in SQL Server , I know that I can use
identity
> filed in SQL Server to increment the primary key , but my database is
designed
> like this , What I need is How to increment the primary key in the record
> component when I add new record , in other way What is the code that I
must
> but in What Event to get the maximum value of the primary key and convert
it
> from string to int increment it by 1 then convert it to String and finally
set
> the value of the primary key filed in the record form component when I
request
> a new record .
> I am Using ASP.NET C#
> Please Help
>
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>

Stan
Posted: 11/01/2004, 11:58 PM

Mike

You can try following method

1. Create the hidden control into the Record with Data Type Integer and Control Source = PK Field Name
2. Add to the Before Insert event of the record DLookup Action with following settings

Expression = "MAX(CONVERT(int,<PK FIELD NAME>)) + 1"
Domain = "<TABLE NAME>"
Criteria = empty
Convert result to = Integer
Type of Target = Control
Target = Hidden name

Or you can create trigger into the database for increment this field during insert
DayMatrix

Posts: 4
Posted: 12/07/2004, 8:53 AM

This is a really, really bad idea in a web application, especially if you have multiple users. The only safe way to do something like this is create the record with the key then add the other data.

Unless you have some compelling reason to avoid the identity key, it is much better to let SQL handle your auto-incrementing keys.

Otherwise, I would use a trigger on the insert event but why?
_________________
Day Matrix
Member
Netfishbowl, LLC.
http://www.netfishbowl.com
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright 2003-2004 by UltraApps.com  and YesSoftware, Inc.