11 Replies - 324 Views - Last Post: 23 August 2012 - 12:27 AM Rate Topic: ***** 1 Votes

#1 ryang112  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-August 12

Input String was in Incorrect Format thrown from datatable

Posted 22 August 2012 - 03:09 AM

Hi there, I keep getting a FormatException in the following code block
  OrderLine  ordLine = new OrderLine();

                //Load all the 'finalized' dummyLines into a datatable (this will be used to loop through and set parameters later)

                dTab = DAL.GetAllDummyLinesForOrder(setRealOrderFromDummy);

                foreach (DataRow row in dTab.Rows)
                {
                        
                        
                       //'Get' values from the datable and use them to SET the values in the OrderLineClass
                        ordLine.OrderLineID = SqlInt32.Parse(dTab.Columns["OrderLineID"].ToString()); //Format Exception Being Thrown Here... WHY??
                        ordLine.OrderID = SqlInt32.Parse(dTab.Columns["OrderID"].ToString());
                        ordLine.ProductID = SqlInt32.Parse(dTab.Columns["ProductID"].ToString());
                        ordLine.Quantity = SqlInt32.Parse(dTab.Columns["Quantity"].ToString());
                        ordLine.LineValue = SqlMoney.Parse(dTab.Columns["LineValue"].ToString());
                        ordLine.VatID = SqlInt32.Parse(dTab.Columns["VatID"].ToString());
                        //Note IsActive is simply hardcoded
                        ordLine.IsActive = "Y";
                        //Add OrderLine With The Above Parameters
                        int x = DAL.AddOrderLine(ordLine);             
                        
                   
                }





Apologies for comments being messy, my ordLine is an instance class held in an external DLL. I have tried using:

ordLine.OrderLineID = SqlInt32.Parse(dTab.Rows[0].ToString());



as well with no success. I have checked the value of the datable dTab, and it does infact have rows and columns, so no error there right? The column names used in the foreach loop are the same as those in the SQL database, and thier datatypes match those of the class properties I am attempting to set. Even if i put in a try....catch the end result is these rows are not commited to my database.
Below is the code for the method DAL.AddOrderLine:

 //Add Order Line
        public int AddOrderLine(OrderLine orderLine)
        {
            using (SqlConnection dbConnection = new SqlConnection(DBConn))
            {
                int x;
                SqlCommand dbCmd = new SqlCommand("uspAddOrderLine", dbConnection);
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.AddWithValue("@orderLineID", orderLine.OrderLineID);
                dbCmd.Parameters.AddWithValue("@orderID", orderLine.OrderID);
                dbCmd.Parameters.AddWithValue("@productID", orderLine.ProductID);
                dbCmd.Parameters.AddWithValue("@quantity", orderLine.Quantity);
                dbCmd.Parameters.AddWithValue("@orderLineValue", orderLine.LineValue);
                dbCmd.Parameters.AddWithValue("@vatID", orderLine.VatID);
                dbCmd.Parameters.AddWithValue("@isActive", orderLine.IsActive);

                try
                {

                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                    x = dbCmd.ExecuteNonQuery();
                }

                catch (SqlException e)
                {
                    exc = new Exceptions();
                    exc.Exception = e;
                    logExc = new LogException();
                    logExc.LogExceptionToFile();
                    throw new ApplicationException("Error occured in the database and / or connection");
                }

                //Realease Resources
                dbConnection.Close();
                dbConnection.Dispose();
                dbCmd.Dispose();
                return x;
            }

        }



this is contained in a class in another (seperate) DLL.

Any assistance will be appreciated, I hope I have given enough information
:)

Is This A Good Question/Topic? 0
  • +

Replies To: Input String was in Incorrect Format thrown from datatable

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5677
  • View blog
  • Posts: 22,539
  • Joined: 23-August 08

Re: Input String was in Incorrect Format thrown from datatable

Posted 22 August 2012 - 03:35 AM

Are you stepping through the code in the debugger? What is the value of dTab.Columns["OrderLineID"] that's failing?
Was This Post Helpful? 0
  • +
  • -

#3 ryang112  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-August 12

Re: Input String was in Incorrect Format thrown from datatable

Posted 22 August 2012 - 03:42 AM

Attached ImageI have stepped through it yes, I suspect its that value but I cant find it in locals? I have attached a screen snip on my debugger and how I am attempting to find the value of it in locals.

Thanks for the speedy Reply :)
Was This Post Helpful? 0
  • +
  • -

#4 ryang112  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-August 12

Re: Input String was in Incorrect Format thrown from datatable

Posted 22 August 2012 - 03:53 AM

Attached ImageI have stepped through it yes, I suspect its that value but I cant find it in locals? I have attached a screen snip on my debugger and how I am attempting to find the value of it in locals.

Thanks for the speedy Reply :)
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5677
  • View blog
  • Posts: 22,539
  • Joined: 23-August 08

Re: Input String was in Incorrect Format thrown from datatable

Posted 22 August 2012 - 04:04 AM

Set a watch for the variable and step through the code. When the variable gets a value, the watch will be set with that value and you can see what it is.

Alternately, you can likely find the values under the Non-Public members. Don't have VS available, so can't be more specific than that at this point.
Was This Post Helpful? 1
  • +
  • -

#6 ryang112  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-August 12

Re: Input String was in Incorrect Format thrown from datatable

Posted 22 August 2012 - 04:10 AM

Thanks very much :) I will try that.
Was This Post Helpful? 0
  • +
  • -

#7 ryang112  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-August 12

Re: Input String was in Incorrect Format thrown from datatable

Posted 22 August 2012 - 04:16 AM

Its defintely the
SqlInt32.Parse(dTab.Columns["OrderLineID"].ToString());


part of the statement but i still cant see its value.. as its value is set to "It threw an exception etc".. so i have no idea if it reverted to null or something...
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4929
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Input String was in Incorrect Format thrown from datatable

Posted 22 August 2012 - 06:38 AM

This is a good example of how complex lines might seem easier to read but make debugging harder

break down the line so you can step through it.

ordLine.OrderID = SqlInt32.Parse(dTab.Columns["OrderID"].ToString());
//breaks down to
var testing = dTab.Columns["OrderID"];// At this point you can see the actual value regardless what it is
var testingString = testing.ToString();'
var testingSql = SqlInt32.Parse(testingString);
ordLine.OrderID = testingSql;


This post has been edited by tlhIn`toq: 22 August 2012 - 09:13 AM

Was This Post Helpful? 3
  • +
  • -

#9 ryang112  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-August 12

Re: Input String was in Incorrect Format thrown from datatable

Posted 22 August 2012 - 09:03 AM

Ahaaah very very clever :) I will try that.. Thanks very much :)
Was This Post Helpful? 0
  • +
  • -

#10 ryang112  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-August 12

Re: Input String was in Incorrect Format thrown from datatable

Posted 23 August 2012 - 12:13 AM

hey guys, saw the strangest thing, its passing the column name! Please see the picture i've included.. any ideasAttached Image
Was This Post Helpful? 0
  • +
  • -

#11 ryang112  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-August 12

Re: Input String was in Incorrect Format thrown from datatable

Posted 23 August 2012 - 12:19 AM

P.S i tried using the columns index, same result
Was This Post Helpful? 0
  • +
  • -

#12 ryang112  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-August 12

Re: Input String was in Incorrect Format thrown from datatable

Posted 23 August 2012 - 12:27 AM

:bananaman: I found the solution! Below is the corrected code
   foreach (DataRow row in dTab.Rows)
                {
                       //'Get' values from the datable and use them to SET the values in the OrderLineClass
                        ordLine.OrderLineID = SqlInt32.Parse(row[0].ToString());
                        ordLine.OrderID = SqlInt32.Parse(row[1].ToString());
                        ordLine.ProductID = SqlInt32.Parse(row[2].ToString());
                        ordLine.Quantity = SqlInt32.Parse(row[3].ToString()); 
                        ordLine.LineValue = SqlMoney.Parse(row[5].ToString());
                        ordLine.VatID = SqlInt32.Parse(row[6].ToString());
                        //Note IsActive is simply hardcoded
                        ordLine.IsActive = "Y";
                        //Add OrderLine With The Above Parameters
                        int x = DAL.AddOrderLine(ordLine);             
                }


Thanks everyone for your input :) : ) it helped greatly
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1