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

Welcome to UtterAccess! Please ( Login   or   Register )

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



Posts: 497
Joined: 19-August 08
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:
ommand.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: 22,245
Joined: 8-January 07
From: St. Catharines, 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 proble
Go to the top of the page
 
rlsdata
post Aug 10 2011, 11:31 AM
Post#3



Posts: 497
Joined: 19-August 08
From: Michgan, USA


Thanks Doug
Omoved it and it still returns the same error.
It has to be something simple iconfused.gif
Go to the top of the page
 
Doug Steele
post Aug 10 2011, 11:51 AM
Post#4


UtterAccess VIP
Posts: 22,245
Joined: 8-January 07
From: St. Catharines, 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,301
Joined: 1-January 07
From: Whittier, California, USA


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



Posts: 497
Joined: 19-August 08
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.
Oarranged the parameters into ordinal position and it works.
yan
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    20th February 2020 - 09:43 PM