[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

Creating a Unique Id for Database Logon in Webservice App

Hello Again,

I am trying to convert a .NET Web Service project from VB to C# and I am having some problems with the conversion. The following code has been partially converted, via C-Sharpener for VB. I am having trouble getting it the rest of the way.  It's obvious that the code is intended to create a unique ID, which is used later to sign on to a database. I just can't quite get the C# equivalent quite right.

[ WebMethod() ]
        public string UniqueID() {
            string uniqueIDReturn = null;
            Object sID = null; Object sChar = null; Object iCtr = null;
            VBMath.Randomize();
                  string sChar;
                  
            for ( iCtr=1; iCtr<=10; iCtr++ ) {
                sChar = Strings.Chr( System.Convert.ToInt32( Conversion.Int( ( 90 - 65 + 1 ) * VBMath.Rnd() ) ) + 65 );
                sID = System.Convert.ToString( sID ) + sChar;
            }
                  
            sID = System.Convert.ToString( sID ) + DateAndTime.Month( DateAndTime.Now ) + DateAndTime.Day( DateAndTime.Now ) + DateAndTime.Year( DateAndTime.Now ) + DateAndTime.Hour( DateAndTime.Now ) + DateAndTime.Minute( DateAndTime.Now ) + DateAndTime.Second( DateAndTime.Now );
            uniqueIDReturn = System.Convert.ToString( sID );
           
            return uniqueIDReturn;
        }

As always, help is much appreciated!!!

0
nh_capricorn
Asked:
nh_capricorn
  • 4
1 Solution
 
ptmcompCommented:
The work is already done in the .net Framework:

Guid myGlobalUniqueId = Guid.NewGuid();

Just use ToString() to get a string represantation. You can use myGlobalUniqueId.ToString("N"); to get it as one big hex-number.
0
 
ptmcompCommented:
Or if you really want to use the translated code:

             public static string UniqueID() {
            string uniqueIDReturn;
           
            string sID = "";
           
            Random random = new Random((int)DateTime.Now.Ticks);
           
            char sChar;
               
            for (int iCtr=1; iCtr<=10; iCtr++ ) {
                  sChar = (Char)( System.Convert.ToInt32(  ( 90 - 65 + 1 ) * random.Next() )  + 65 );
                sID += sChar;
            }
               
            sID = sID + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
            uniqueIDReturn = sID;
           
            return uniqueIDReturn;
        }
0
 
ptmcompCommented:
Sorry, guess this is better:

             public static string UniqueID() {
            string uniqueIDReturn;
           
            string sID = "";
           
            Random random = new Random((int)DateTime.Now.Ticks);
           
            char sChar;
               
            for (int iCtr=1; iCtr<=10; iCtr++ ) {
                  sChar = (Char)random.Next(65, 90);
                sID += sChar;
            }
               
            sID = sID + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Year + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
            uniqueIDReturn = sID;
           
            return uniqueIDReturn;
        }
0
 
ptmcompCommented:
And a bit shorter:

using System.Text;

        public static string UniqueID() {

            StringBuilder sbUID = new StringBuilder(24);
           
            Random random = new Random((int)DateTime.Now.Ticks);
           
            for (int iCtr = 1; iCtr <= 10; iCtr++) {
                  char ch = (Char)random.Next(65, 90);
                  sbUID.Append(ch);
            }
           
            sbUID.AppendFormat("{0:MMddyyyyHHmmss}", DateTime.Now);
           
            return sbUID.ToString();
        }
0
 
nh_capricornAuthor Commented:
WOW!

Thanks - all of your solutions are good ones. I originally thought that using the GUID would create a much longer ID, but now, taking another look at the code, I'm not so sure.

Still, you answered my question, and I'm grateful.

Thanks Again!

~ nhc
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now