• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2169
  • Last Modified:

How do I cast ADODB.Recordset.Fields values to integers?

I would like to associate the ID's of tableMoose with a range of values from tableBear. The following code gives an "Input string was not in a correct format" error in the If statement. I am having trouble casting the cell values to integers in order to make comparisons in the if statement. The code is ported from VB6 which allowed a cast to integer in the If statement. I am using DotNet 2.0.

Thanks.
private void fill_ID()
            {
                ADODB.ConnectionClass conn = new ADODB.ConnectionClass();
                ADODB.Recordset rs = new ADODB.Recordset();
                ADODB.Recordset rs2 = new ADODB.Recordset();
 
                conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:" + "\\" + "Foo.mdb", null, null, 0);
                rs.Open("Select ID, columnX FROM tableMoose", conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, 0);
                rs2.Open("Select ID, columnA, columnB FROM tableBear", conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, 0);
 
                rs.MoveFirst();
                while (rs.EOF == false)
                {
                    rs2.MoveFirst();
                    while (rs2.EOF == false)
                    {
                        if ((Int32.Parse((rs.Fields["columnX"].ToString())) >= Int32.Parse((rs2.Fields["columnA"].ToString()))) && (Int32.Parse((rs.Fields["columnX"].ToString())) <= Int32.Parse((rs2.Fields["columnB"].ToString()))))
                        {
                            rs.Update("ID", rs2.Fields["ID"]);
                        }
                        rs2.MoveNext();
                    }
                    rs.MoveNext();
                }
            }

Open in new window

0
tman43
Asked:
tman43
  • 3
1 Solution
 
vbturboCommented:
Hello

try assign the values into vars first and see which one is giving you the exception

Double arg1;
Double arg2;
Double arg3;
                    while (rs2.EOF == false)
                    {
                       arg1 = Int32.Parse((rs.Fields["columnX"].Value;
                       arg2 = Int32.Parse((rs2.Fields["columnA"].Value;
                       arg3 = Int32.Parse((rs2.Fields["columnB"].Value;

                        rs2.MoveNext();
                    }
                    rs.MoveNext();
0
 
tman43Author Commented:
Assigning the column values to variables and then using the variables to make comparisons in the If statement has removed the "Input string was not in a correct format" error in the If statement. The method now performs correctly.

Thanks much!
            private void fill_ID()
            {
                ADODB.ConnectionClass conn = new ADODB.ConnectionClass();
                ADODB.Recordset rs = new ADODB.Recordset();
                ADODB.Recordset rs2 = new ADODB.Recordset();
 
                conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:" + "\\" + "Foo.mdb", null, null, 0);
                rs.Open("Select ID, columnX FROM tableMoose", conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, 0);
                rs2.Open("Select ID, columnA, columnB FROM tableBear", conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, 0);
 
                Double arg1;
                Double arg2;
                Double arg3;
 
                rs.MoveFirst();
                while (rs.EOF == false)
                {
                    rs2.MoveFirst();
                    while (rs2.EOF == false)
                    {
                        arg1 = Int32.Parse((rs.Fields["columnX"].Value.ToString()));
                        arg2 = Int32.Parse((rs2.Fields["columnA"].Value.ToString()));
                        arg3 = Int32.Parse((rs2.Fields["column"].Value.ToString()));
 
                        if ((arg1 >= arg2) && (arg1 <= arg3))
                        {
                            rs.Update("ID", rs2.Fields["ID"]);
                        }
                        rs2.MoveNext();
                    }
                    rs.MoveNext();
                }
            }

Open in new window

0
 
tman43Author Commented:
Thanks for your prompt response and clear solution.
0
 
tman43Author Commented:
Note:

Typo on Line 23.

Should be: ...rs2.Fields["columnB"]...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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