You must Sign In to post a response.
  • Category: .NET

    Valiadate EmployeeNumber dynamically using RegEx

    In my scenorio need to skip the known error while writing in log file in catch block.
    For example "The Employee Number 1256987 was not found"
    but the employee number is dynamic (1256987).
    I have assigned the string variable like
    string knownError ="The Employee Number [+EmpNo+] was not found";

    In catch block , i have to compare like this
    if (knownError.contains(ex.message());
    {
    //some logic
    }
    else
    {
    log.writeline(ex.message());
    }

    How can i check [+EmpNo+] using RegEx.
  • #766427
    Hi
    if you need numeric only means try this code



    Regex regex = new Regex(@"^\d$");

    OR

    if (!System.Text.RegularExpressions.Regex.IsMatch(textbox.Text, "^[0-9]*$"))

    Or

    <cc1:FilteredTextBoxExtender ID="Filteredtextboxextender1" runat="server" FilterType="Numbers"
    TargetControlID="txtRequestId">
    </cc1:FilteredTextBoxExtender>


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766434
    If you want to check message in exception block using exception.Message property, why you need RegEx here ? just check if database contains the employee record if not then directly throw exception
    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766551
    Hi,

    Kindly try with the following Logic for your requirement. It May resolve your Problem.

    class Program
    {
    private static Regex regex = new Regex("^[0-9]+$", RegexOptions.Compiled);

    static void Main(string[] args)
    {
    Stopwatch watch = new Stopwatch();
    string test = int.MaxValue.ToString();
    int value;

    watch.Start();
    for(int i=0; i< 1000000; i++)
    {
    int.TryParse(test, out value);
    }
    watch.Stop();
    Console.WriteLine("TryParse: "+watch.ElapsedTicks);

    watch.Reset();
    watch.Start();
    for (int i = 0; i < 1000000; i++)
    {
    IsDigitsOnly(test);
    }
    watch.Stop();
    Console.WriteLine("IsDigitsOnly: " + watch.ElapsedTicks);

    watch.Reset();
    watch.Start();
    for (int i = 0; i < 1000000; i++)
    {
    regex.IsMatch(test);
    }
    watch.Stop();
    Console.WriteLine("Regex: " + watch.ElapsedTicks);

    Console.ReadLine();
    }

    static bool IsDigitsOnly(string str)
    {
    foreach (char c in str)
    {
    if (c < '0' || c > '9')
    return false;
    }

    return true;
    }
    }

  • #766588
    This is customize the complete error. So all are depending on your messages. You have to handle it each and every part of your error messages. You can try the as follows. It is sample but you have to try your own logic based on the error message

    string knownError ="The Employee Number [+EmpNo+] was not found";
    In catch block , i have to compare like this
    if (ex.message() // matches first part "The Employee Number");
    {
    if (ex.message() // matches second part " was not found");
    {
    // get the Emp number from the string and show the exception
    }
    }
    else
    {
    log.writeline(ex.message());
    }

    By Nathan
    Direction is important than speed


Sign In to post your comments