DataReader to string and to integer

dualshck012

Active member
Joined
Jan 14, 2018
Messages
29
Location
Leyte, Philippines
Programming Experience
1-3
Good day!

I'm creating a function for incremented ID generation
I'm getting an error: {"Input string was not in a correct format."} under line Convert.ToIns32(maxId). I also tried Int32.TryParse with the same error. The Output would be something ICTS2021002

C#:
string maxId = mysqlconstring.reader3[0].ToString();
                    if (!string.IsNullOrWhiteSpace(maxId) && maxId.StartsWith(prefix))
                    {
                        int count = Convert.ToInt32(maxId);
                        newId = string.Format("ICTS{0}{1:000}", DateTime.Now.Year, count + 1);
                    }
 
The whole concept here is wrong from the start. Don't manually increment values like that. Define a sequence in your database and use that to generate the values for you.

As for the issue, what was the value of maxId when the exception was thrown? If it's like your desired output and starts with "ICTS" then why would you expect to be able to convert that to an int?
 
The whole concept here is wrong from the start. Don't manually increment values like that. Define a sequence in your database and use that to generate the values for you.

As for the issue, what was the value of maxId when the exception was thrown? If it's like your desired output and starts with "ICTS" then why would you expect to be able to convert that to an int?
This was the entire code though, I used it a while with added dash character using split method. The sequence works fine. I used this, int count = Convert.ToInt32(maxId.Split('-')[2]); Right now I want to remove the added dashes.


C#:
       private void GenerateTaskCatID()
        {
            mysqlconstring.conn2.Open();
            string query509A = "select Max(tskcatid) from ihsastaskcatgry";
            mysqlconstring.cmda = mysqlconstring.conn2.CreateCommand();
            mysqlconstring.cmda.CommandType = CommandType.Text;
            mysqlconstring.cmda.CommandText = query509A;
            mysqlconstring.adapta.SelectCommand = mysqlconstring.cmda;
            mysqlconstring.reader3 = mysqlconstring.cmda.ExecuteReader();

            string newId = string.Format("ICTS-{0}-001", DateTime.Now.Year);
            if (mysqlconstring.reader3.HasRows)
            {
                string prefix = string.Format("ICTS-{0}", DateTime.Now.Year);
                while (mysqlconstring.reader3.Read())
                {

                    string maxId = mysqlconstring.reader3[0].ToString();
                    if (!string.IsNullOrWhiteSpace(maxId) && maxId.StartsWith(prefix))
                    {
                        int count = Convert.ToInt32(maxId.Split('-')[2]);
                        newId = string.Format("ICTS-{0}-{1:000}", DateTime.Now.Year, count + 1);
                    }
                }

            }
            Sett_icts_tbox_taskcatkey.Text = newId;
            mysqlconstring.conn2.Close()
}
 
If all you're interested in is the last three digits of the ID text then get the last the last three characters of the string first, then convert that to an int.

Regardless of whether your sequence code works or not, it's still bad code. Try using that in a multi-user system and see how well it works. Maybe just do it the right way from the outset. In fact, you really shouldn't need any code at all to do this. If you had one column to populate from a proper sequence and then another column that constructed a value using its own expression, that formatted ID text would be created automatically.
 
If all you're interested in is the last three digits of the ID text then get the last the last three characters of the string first, then convert that to an int.

Regardless of whether your sequence code works or not, it's still bad code. Try using that in a multi-user system and see how well it works. Maybe just do it the right way from the outset. In fact, you really shouldn't need any code at all to do this. If you had one column to populate from a proper sequence and then another column that constructed a value using its own expression, that formatted ID text would be created automatically.
how to that in datareader?
 

Latest posts

Back
Top Bottom