UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Type Mismatch Error In Oledb Command    
 
   
rlsdata
post Aug 10 2011, 11:16 AM
Post #1

UtterAccess Veteran
Posts: 477
From: Michgan, USA



I am working with a method that inserts data with parameters as values. I am getting a data type mismatch error when the method gets to the line:

command.ExecuteNonQuery();

All of the parameters are int with the exception of @_InspItem and @_ObservDef and @_Category. Those parameters are string.

CODE
private void AddTemplateItemToInspection()
      {
      try
        {
        //verify there is a building
        _InspectionID = Convert.ToInt32(this.inspectionIDTextBox.Text);

        if (string.IsNullOrEmpty(comboBoxBuilding.SelectedValue.ToString()))
          {
          MessageBox.Show("You must select a building for this inspection item.");
          return;
          }
        string sql;

        //see of unit id is null and set sql string accordingly
        if (string.IsNullOrEmpty(comboBoxUnit.SelectedValue.ToString()))
          {
          sql = "INSERT INTO tblInspectionChecklist(InspectionID, BuildingID, Category, InspectableItem, ItemWeight, ObservableDeficiency, Criticality) " +
                               "Values(@_InspectionID, @_BuildingID, @_Category, @_InspItem, @_ItemWeight, @_ObservDef, @_Criticality)";
          }
        else
          {
          sql = "INSERT INTO tblInspectionChecklist(InspectionID, BuildingID, UnitID, Category, InspectableItem, ItemWeight, ObservableDeficiency, Criticality) " +
                               "Values(@_InspectionID, @_BuildingID, @_UnitID, @_Category, @_InspItem, @_ItemWeight, @_ObservDef, @_Criticality)";
          }

        //set up oledb actions
        string s = ConfigurationManager.AppSettings["connString"];
        OleDbConnection c = new OleDbConnection(s);
        OleDbCommand command = new OleDbCommand(sql, c);
        command.Parameters.AddWithValue(@"_InspectionID", _InspectionID);
        if (comboBoxUnit.SelectedText != null)
          {
          command.Parameters.AddWithValue(@"_UnitID", Convert.ToInt32(comboBoxUnit.SelectedValue));
          }
        command.Parameters.AddWithValue(@"_BuildingID", Convert.ToInt32(comboBoxBuilding.SelectedValue));
        command.Parameters.AddWithValue(@"_InspItem", _InspItem);
        command.Parameters.AddWithValue(@"_ObservDef", _ObservDef);
        command.Parameters.AddWithValue(@"_Category", this.comboBoxInspectionArea.SelectedValue);
        command.Parameters.AddWithValue(@"_Criticality", _Criticality);
        command.Parameters.AddWithValue(@"_ItemWeight", _ItemWeight);
        c.Open();
        command.ExecuteNonQuery();
        c.Close();

        }
      catch (OleDbException ex)
        {
        MessageBox.Show(ex.Message + " " + ex.Source.ToString());
        }
      }

Any help of suggestions are appreciated!
Go to the top of the page
 
+
Doug Steele
post Aug 10 2011, 11:22 AM
Post #2

UtterAccess VIP
Posts: 17,636
From: Don Mills, ON (Canada)



Don't know whether it matters, but the example in OleDbCommand.ExecuteNonQuery Method shows opening the connection before creating the OleDbCommand object. (of course, I'd expect a different error message if that was the problem...)
Go to the top of the page
 
+
rlsdata
post Aug 10 2011, 11:31 AM
Post #3

UtterAccess Veteran
Posts: 477
From: Michgan, USA



Thanks Doug
I moved it and it still returns the same error.
It has to be something simple (IMG:style_emoticons/default/iconfused.gif)
Go to the top of the page
 
+
Doug Steele
post Aug 10 2011, 11:51 AM
Post #4

UtterAccess VIP
Posts: 17,636
From: Don Mills, ON (Canada)



Sorry, no other ideas. Hopefully someone else will see something.

Good luck!
Go to the top of the page
 
+
rabroersma
post Aug 10 2011, 12:04 PM
Post #5

UtterAccess VIP
Posts: 1,215
From: Arcadia, California, USA



QUOTE (rlsdata @ Aug 10 2011, 09:16 AM) *
I am working with a method that inserts data with parameters as values. I am getting a data type mismatch error when the method gets to the line:
command.ExecuteNonQuery();


To help solve this problem, we'll need to know which layer of the application is throwing the data type mismatch error. First off, we should rule out whether the error is being issued by the database during the insertion attempt. If you have database has the ability to log sql statements, see if the insert is attempted but rejected by the database. If it is, try to identify which field is typed incorrectly.
Go to the top of the page
 
+
rlsdata
post Aug 10 2011, 03:07 PM
Post #6

UtterAccess Veteran
Posts: 477
From: Michgan, USA



Got the answer! Hopefully it will be useful to others.
If you are using OLEDB then the number parameters in the SQL statement should match the number defined in the Parameters collection. In addition, they are evaluated based upon their ordinal position in the SQL statement and not by name.
I arranged the parameters into ordinal position and it works.

Ryan
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 22nd May 2013 - 09:05 AM