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

You are currently viewing the ASP.NET 3.5 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 August 6th, 2009, 11:13 AM
Authorized User
 
Join Date: Jun 2009
Posts: 30
Thanks: 13
Thanked 0 Times in 0 Posts
Default Display employer id

hi....how to display the last data(Employer ID) from employer table?

selectStr = "Select Employer_Id from Employer";
cmdSelect =
newSqlCommand(selectStr, con);
dtrEmp = cmdSelect.ExecuteReader();
if (dtrEmp.HasRows){
while (dtrEmp.Read())
{
Label2.Text =
Convert.ToString(dtrEmp["Employer_Id"]);

}
}
dtrEmp.Close();
i have tried....but still could get the last Employer_Id...can anybody correct it?

Last edited by Banishah; August 6th, 2009 at 11:28 AM..
 
Old August 6th, 2009, 03:02 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You should be able to do that by using the Read method as you have.

This should work:

Code:
 
using (SqlConnection connection = new SqlConnection("your_connection_string"))
{
   string cmdText = "SELECT Employer_Id FROM Employer";
   SqlCommand command = new SqlCommand(cmdText, connection);
   connection.Open();
   SqlDataReader reader = command.ExecuteReader();
   if (reader != null)
      while (reader.Read())
      {
         Label2.Text = reader["Employer_Id"].ToString();
      }
}
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
Banishah (August 8th, 2009)
 
Old August 6th, 2009, 03:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Sorry Lee, But I see no difference between you code and OP's code. Maybe he need to clarify what is he looking for??? I think he is looking for a max function or a min one or something like httat. Anyway he will still show always the last ID of the table, with no particular order.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old August 6th, 2009, 03:45 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
Originally Posted by gbianchi View Post
Sorry Lee, But I see no difference between you code and OP's code.
Maybe you should look closer.

His code never calls the Open method to open the connection.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old August 6th, 2009, 03:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Yep that right, but he doesn't instanciate the connection either, so he could easily has open it before all this...

Anyway, I was just asking because it's not clear what the porpuose of his code.. Maybe Banishah can tell us what he is looking for to do???
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old August 7th, 2009, 11:52 PM
Authorized User
 
Join Date: Jun 2009
Posts: 30
Thanks: 13
Thanked 0 Times in 0 Posts
Default

Actually i need the last employerID(e.g EM000003) to add 1 to it and become EM000004 because i want it to be autogenerated once a new employer is added....
 
Old August 8th, 2009, 02:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

So in your ID field you have a combo of letters and numbers??

well this just complicate things. Several things you can do is split the field, get the max of the numbers and add one (all in one query).
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old August 9th, 2009, 01:05 AM
Authorized User
 
Join Date: Jun 2009
Posts: 30
Thanks: 13
Thanked 0 Times in 0 Posts
Default

But how to split it?
 
Old August 9th, 2009, 04:23 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Isn't this simply a matter of presentation and formatting?

I would create an identity field that automatically increases on each new record. Then either in your presentation layer or in a calculated field you combine the EM0000* stuff with the employee id...

Just my 2 cents.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Banishah (August 9th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Form Data Will Not Display When I Click On Display Button sprdave BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 12 February 13th, 2011 12:09 AM
Employer ID Banishah ASP.NET 3.5 Basics 0 August 4th, 2009 11:58 PM
'this.ID = id;' in class construction holf BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 October 6th, 2006 10:58 AM
ID crmpicco HTML Code Clinic 5 February 10th, 2005 11:50 AM
why not index.asp?id=1 can be www.myweb.com/?id=1 BurhanKhan Classic ASP Professional 11 September 6th, 2004 02:06 PM





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