se_brandon
se_brandon

Reputation: 228

OracleCommand.ExecuteNonQuery() throwing exception

Thanks for taking the time to look and help me out. I am driving myself nuts with what seems to be a very easy insert operation but I can't seem to get passed this part. Here is what I have for code:

 protected void InsertIntoMaterialDB()
    {
        ToroGeneral toro = new ToroGeneral();

        // Grab connection string.
        string conString = toro.GetOracle1ConnectionString();
        string insertQuery = "INSERT INTO MATERIALMOVEREQUEST (ORIGINATOR, REQUESTDATE, PARTNUMBER, REQUESTQTY, MOVEFROM, MOVETO, COMPLETEDBY, COMPLETION_DATE, COMMENTS, RESPONSETIME) "
                               + "values (:Originator, :RequestDate, :PartNumber, :RequestQty, :MoveFrom, :MoveTo, :CompletedBy, :CompletionDate, :ResponseTime, :Comments)";

        using(OracleConnection conn1 = new OracleConnection(conString))
        {    
             conn1.Open();
             OracleCommand cmd = conn1.CreateCommand();
             OracleTransaction myTrans;
             cmd.CommandText = insertQuery;
             myTrans = conn1.BeginTransaction(IsolationLevel.ReadCommitted);
             cmd.Transaction = myTrans;

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

             DateTime rDate = DateTime.Parse(RequestDateTB.Text);
             DateTime cDate = DateTime.Parse(CompDateTB.Text);

             cmd.Parameters.AddWithValue("Originator", OracleType.VarChar).Value = OriginatorTB.Text;
             cmd.Parameters.AddWithValue("RequestDate", OracleType.DateTime).Value = rDate;
             cmd.Parameters.AddWithValue("PartNumber", OracleType.VarChar).Value = PartNumber.Text;
             cmd.Parameters.AddWithValue("RequestQty", OracleType.Number).Value = Convert.ToInt32(RequestQuantity.Text);
             cmd.Parameters.AddWithValue("MoveFrom", OracleType.VarChar).Value = MoveFromTB.Text;
             cmd.Parameters.AddWithValue("MoveTo", OracleType.VarChar).Value = MoveToTB.Text;
             cmd.Parameters.AddWithValue("CompletedBy", OracleType.VarChar).Value = CompletedByTB.Text;
             cmd.Parameters.AddWithValue("CompletionDate", OracleType.DateTime).Value = cDate;
             cmd.Parameters.AddWithValue("ResponseTime", OracleType.Number).Value = Convert.ToInt32(RespTimeTB.Text);
             cmd.Parameters.AddWithValue("Comments", OracleType.VarChar).Value = CommentsTB.Text;

             cmd.ExecuteNonQuery(); // THIS THROWS AN EXCEPTION.
             cmd.Transaction.Commit();
        }

    }

I was able to get everything working except for the two 'DATE' type fields REQUESTDATE and COMPLETION_DATE. I don't know what I am doing wrong except for that when it gets to the ExecuteNonQuery() call it throws an exception.

If anyone has any suggestions it would be really appreciated.

Upvotes: 1

Views: 4757

Answers (3)

bwalk2895
bwalk2895

Reputation: 564

I've run into a similar issue in the past. Use the To_Date function to convert the string to a date oracle will recognize.

INSERT INTO MATERIALMOVEREQUEST (ORIGINATOR, REQUESTDATE, PARTNUMBER, REQUESTQTY, MOVEFROM, MOVETO, COMPLETEDBY, COMPLETION_DATE, COMMENTS, RESPONSETIME) "
                           + "values (:Originator, To_Date(:RequestDate, 'YYYY-MM-DD-HH24:MI:SS'), :PartNumber, :RequestQty, :MoveFrom, :MoveTo, :CompletedBy, To_Date(:CompletionDate, 'YYYY-MM-DD-HH24:MI:SS'), :ResponseTime, :Comments)";

Then in your parameters set the value to a string.

cmd.Parameters.AddWithValue("RequestDate", OracleType.VarChar).Value = rDate.ToString("yyyy-MM-dd-hh:mm:ss");

Upvotes: 2

Ashish Gupta
Ashish Gupta

Reputation: 15139

In addition to what Joe said above, I noticed a couple of things :-

a) You are opening a transaction but never rollbacking it in case of any exception. You may want to enclose this with try catch statement and rollback the transaction in case of exception and commit otherwise.

b) You should always use the using block around the connection objects (I just edited your code in the question). That way you don't have to close and dispose connection explicitly. This is because all connection provider types (including OracleConnection) implement IDisposable interface via their base class "DbConnection".

Upvotes: 0

to StackOverflow
to StackOverflow

Reputation: 124726

For the Date fields you probably want to use OracleType.DateTime and pass a DateTime value that you generate by parsing the input string, e.g.:

AddWithValue("CompletedBy", OracleType.DateTime).Value = 
              DateTime.ParseExact(...CompletedByTB.Text...); 

Upvotes: 1

Related Questions