next primary key in label control

N

nasirmajor

please any urgent response.
Dear alls; (database relevent quetion)
i want to display the next availible primery key {e.g 1,2,3
(autonumbered) next 4} on the user entry form in "label" control.Thanks
 
R

Ray Booysen

please any urgent response.
Dear alls; (database relevent quetion)
i want to display the next availible primery key {e.g 1,2,3
(autonumbered) next 4} on the user entry form in "label" control.Thanks
On another point, primary keys are never supposed to be used for
anything other than indexing in the database. If you're wanting to show
this kind of information, the "best" solution is to have another column
to show this kind of data.
 
N

nasirmajor

Ray said:
On another point, primary keys are never supposed to be used for
anything other than indexing in the database. If you're wanting to show
this kind of information, the "best" solution is to have another column
to show this kind of data.

i don't want to show this info to the user (i will set its visible prop
to false). what
i want is to use this value to concatenate with the user picture. so
that this pic can be retrived using picname_primarykeyvalue
 
R

Ray Booysen

i don't want to show this info to the user (i will set its visible prop
to false). what
i want is to use this value to concatenate with the user picture. so
that this pic can be retrived using picname_primarykeyvalue
In a multi user system, you can't guarentee what the next primary key
will be. If two users open the site at once and open the form, there
will be problems if they use the "next" primary key.
 
B

bruce barker \(sqlwork.com\)

in this senerio you can not use an identity as primary key. you create a new
table

create table NextID (lastID int not null)
insert NextID values (0)

then you get the next number with a simple sql statement

declare @id int
update set @id = lastID = lastID + 1
select @id

you could also switch to a GUID as the key value type, then you can generate
them on the client when needed.

-- bruce (sqlwork.com)
 
J

Jared

My method for doing this is to fire an append query then straight away
get the max ID. So long as you haven't got another transaction in the
same millisecond it should work.

e.g.

//Insert New Records into Table
strsql = "Insert Into tblMyTable (intData) Values (1)";

conn = new SqlConnection(csConnections.strConnMyConnection);
command = new SqlCommand(strsql, conn);
conn.Open();

try
{
command.ExecuteNonQuery();
}

catch
{
return "Failure";
}

finally
{
conn.Close();
}

//Get New ID
int intNewID = 0
strsql = "SELECT max(ID) FROM tblMyTable";
command = new SqlCommand(strsql, conn);
conn.Open();

reader = command.ExecuteReader();

while (reader.Read())
{
intNewID = reader.GetInt32(0);
}
conn.Close();



You can now use intNewID for part of your picture name.

Regards

Jared
 
N

nasirmajor

Jared said:
My method for doing this is to fire an append query then straight away
get the max ID. So long as you haven't got another transaction in the
same millisecond it should work.

e.g.

//Insert New Records into Table
strsql = "Insert Into tblMyTable (intData) Values (1)";

conn = new SqlConnection(csConnections.strConnMyConnection);
command = new SqlCommand(strsql, conn);
conn.Open();

try
{
command.ExecuteNonQuery();
}

catch
{
return "Failure";
}

finally
{
conn.Close();
}

//Get New ID
int intNewID = 0
strsql = "SELECT max(ID) FROM tblMyTable";
command = new SqlCommand(strsql, conn);
conn.Open();

reader = command.ExecuteReader();

while (reader.Read())
{
intNewID = reader.GetInt32(0);
}
conn.Close();



You can now use intNewID for part of your picture name.

Regards

Jared

Thanks to all responses. it will solve my problem
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top