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

    Error i autogenerating id in

    Error in autogenerating id ..
    error..."Input string was not in a correct format"
    int r;
    string str = "select MAX(empcode) from tbl_organization1";
    MySqlCommand cmd = new MySqlCommand(str, cn);
    MySqlDataReader dr = cmd.ExecuteReader();
    if (dr.Read())

    string a = dr[0].ToString();

    if (a == "")
    lblempcode.Text = "K00" + 1;



    string v = a.TrimStart('K');
    r = Convert.ToInt16(v);
    r = r + 1;
    lblempcode.Text = "K" + string.Format("{0:000}",r); ;


  • #756502

    As per error details "Input string was not in a correct format", you may pass invalid format while convert it into some object. Better to put a break point and check line by line.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #756504
    Hai Ancy,
    It seems, you are getting the error at the line:

    r = Convert.ToInt16(v);

    This is because the format is string and you are trying to convert to int.
    The first things is that your empcode should be int rather than string and then only you will get the max of it.
    Once you find the line of issue, you can fix it.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #756511
    Which line gives you error ? I think 'string.Format' gives you error, please check what value you have stored in variable 'r' ? please elaborate so that we can help you to resolve the issue
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #756543
    Please check the value where you are converting the value from string to integer.

    The error usually comes for a value which is not convertible in a integer.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #760931

    your working good for me

    Input string was not in a correct format

    Conversion issue check them or may you try to int data type assign string value

    see this output image given below

    Name : Dotnet Developer-2015
    Email Id :

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

    Delete Attachment

  • Sign In to post your comments