Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I am using C# .net where I have to retrieve serial number from SQL database which is like A23 but I need only 23 below I am give code, here GenerateKey() is the function which hold the value

C#
public string GenerateKey()
   {
       int data = 0;
       string id = "";
       string str = "select billno from tblEditBill"; //table name tblEditBill
       OdbcCommand cmd;
       cmd = new OdbcCommand(str, conn);
       OdbcDataReader dr;
       dr = cmd.ExecuteReader();
       while (dr.Read() != false)
       {
           data = Convert.ToInt32(dr.GetString(0));

       }
       try
       {

           int newid = Convert.ToInt32(data);
           //Int32.Parse(data);
           id = Convert.ToString(newid + 1);

       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message + ex.Source);
       }

       return id.ToString();
   }

If it is possible please help.Thanks to All.

Thanks & Regards
Indrajit


Update
Hello Friend You Can also manually remove your character from your string like this and use the remaining character to convert it into integer to use them

C#
protected string GetInteger(string s)
{
      string str = s;
      string newStr = "";// string newStr = string.Empty;
      for (int i = 0; i < str.Length; i++)
      {
         // here i will check for the ascii value to match with 0123456789 then it will add to new string
          if (Convert.ToInt32(str[i]) > 47 && Convert.ToInt32(str[i]) < 58)
          {
             newStr += str[i];
          }
      }
     // now new gentrated string will be returned
     return newStr;
}
Posted
Updated 11-Sep-11 19:35pm
v3
Comments
Prerak Patel 12-Sep-11 0:57am    
Use code block for code segment.
Tejas Vaishnav 12-Sep-11 5:56am    
Who the hell this Simon Bang Terkildsen..

Deleted my ans and update the question...???

Use Regex to get only numbers
C#
data = Convert.ToInt32(Regex.Match(dr.GetString(0), "\d+").Value);
 
Share this answer
 
Comments
IndrajitDasgupat 12-Sep-11 1:15am    
What is the use of "\d+" in this method? I did not understand
Prerak Patel 12-Sep-11 1:27am    
\d is regular expresion of a digit. \d+ means one or more digits.
AditSheth 12-Sep-11 1:35am    
You can also use "[0-9]+" instead of "\d+"
Pravin Patil, Mumbai 12-Sep-11 1:48am    
You can surely use [0-9]+, but \d is shorthand notation for the same.
Not clear what you want to achieve but the code shows numerous problems; the code is formally correct, but coding style show a number of misunderstandings:


  • string id = ""; must be: string id = string.Empty;.
  • string str = "select...";; use of immediate string constant hard-coded in the method.
  • OdbcDataReader dr; dr = cmd.ExecuteReader(); is correct but looks strange;
    why not OdbcDataReader dr = cmd.ExecuteReader();?
  • while (dr.Read() != false) shows misunderstanding of Boolean types and conditions;
    should be while (dr.Read())...


—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900