Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. data type mismatch error

data type mismatch error

Scheduled Pinned Locked Moved C#
csharphelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    csanda1
    wrote on last edited by
    #1

    Hi everyone, I've recentley started using C# and am doing an application to insert data using oledataadapter and dataset, and i have an error data type mismatch in this line 'int t1 = adapter.InsertCommand.ExecuteNonQuery();' can anyone tell mi what that means and what ive done wrong, because all my data types are text. Here's the code below: string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datacapt.mdb"; OleDbConnection conn = new OleDbConnection(strConnection); conn.Open(); string strCommand = "INSERT INTO compdata(ID,CompanyName,ContactName) Values ('" + txID.Text + "','" + txtName.Text + "','" + txtcontact.Text + "')"; OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.InsertCommand = new OleDbCommand(strCommand, conn); int t1 = adapter.InsertCommand.ExecuteNonQuery(); conn.Close(); if (t1 > 0) { MessageBox.Show("added successfully!"); } Thanks very much Csanda

    R J P 3 Replies Last reply
    0
    • C csanda1

      Hi everyone, I've recentley started using C# and am doing an application to insert data using oledataadapter and dataset, and i have an error data type mismatch in this line 'int t1 = adapter.InsertCommand.ExecuteNonQuery();' can anyone tell mi what that means and what ive done wrong, because all my data types are text. Here's the code below: string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datacapt.mdb"; OleDbConnection conn = new OleDbConnection(strConnection); conn.Open(); string strCommand = "INSERT INTO compdata(ID,CompanyName,ContactName) Values ('" + txID.Text + "','" + txtName.Text + "','" + txtcontact.Text + "')"; OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.InsertCommand = new OleDbCommand(strCommand, conn); int t1 = adapter.InsertCommand.ExecuteNonQuery(); conn.Close(); if (t1 > 0) { MessageBox.Show("added successfully!"); } Thanks very much Csanda

      R Offline
      R Offline
      Rick van Woudenberg
      wrote on last edited by
      #2

      Hi csanda, You're trying to assign an insert statement as the value for integer t1, while an insert statement should be executed by itself and an integer can only have numbers as a value. I would do it differently. Try the following code : void Whatever() { try { string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datacapt.mdb"; OleDbConnection conn = new OleDbConnection(strConnection); conn.Open(); string strCommand = "INSERT INTO compdata(ID,CompanyName,ContactName) Values ('" + txID.Text + "','" + txtName.Text + "','" + txtcontact.Text + "')"; OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.InsertCommand = new OleDbCommand(strCommand, conn); adapter.InsertCommand.ExecuteNonQuery(); conn.Close(); { MessageBox.Show("Added successfully!"); } } catch (Exception exc) { MessageBox.Show("An error occured :" + exc.ToString()); } } cheers,

      C 1 Reply Last reply
      0
      • C csanda1

        Hi everyone, I've recentley started using C# and am doing an application to insert data using oledataadapter and dataset, and i have an error data type mismatch in this line 'int t1 = adapter.InsertCommand.ExecuteNonQuery();' can anyone tell mi what that means and what ive done wrong, because all my data types are text. Here's the code below: string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datacapt.mdb"; OleDbConnection conn = new OleDbConnection(strConnection); conn.Open(); string strCommand = "INSERT INTO compdata(ID,CompanyName,ContactName) Values ('" + txID.Text + "','" + txtName.Text + "','" + txtcontact.Text + "')"; OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.InsertCommand = new OleDbCommand(strCommand, conn); int t1 = adapter.InsertCommand.ExecuteNonQuery(); conn.Close(); if (t1 > 0) { MessageBox.Show("added successfully!"); } Thanks very much Csanda

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Although you can return a value from a call to ExecuteNonQuery, its slightly harder to setup as you must use output or return parameters from your query. If you want to return a single value (say a 1 or 0 to indicate success/failure), you may find it easier to use ExecuteScalar. However, this returns a type called object which you must cast to an int (if that is what is being returned from the query) Now, it is not clear from your code what t1 was supposed to return, but the sensible thing would be to return the new key from the table row just inserted. You could return 0 if the insert did not succeed and the rest of your code should then work fine. I think @@IDENTITY works for Access for returning a newly inserted key, so your statement should change to: string strCommand = "INSERT INTO compdata(ID,CompanyName,ContactName) Values ('" + txID.Text + "','" + txtName.Text + "','" + txtcontact.Text + "');SELECT @@IDENTITY"; then change: int t1 = (int)adapter.InsertCommand.ExecuteScalar(); If @@IDENTITY does not work there will be loads of articles on the web about returning a newly inserted key.

        --- How to get answers to your questions[^]

        1 Reply Last reply
        0
        • R Rick van Woudenberg

          Hi csanda, You're trying to assign an insert statement as the value for integer t1, while an insert statement should be executed by itself and an integer can only have numbers as a value. I would do it differently. Try the following code : void Whatever() { try { string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datacapt.mdb"; OleDbConnection conn = new OleDbConnection(strConnection); conn.Open(); string strCommand = "INSERT INTO compdata(ID,CompanyName,ContactName) Values ('" + txID.Text + "','" + txtName.Text + "','" + txtcontact.Text + "')"; OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.InsertCommand = new OleDbCommand(strCommand, conn); adapter.InsertCommand.ExecuteNonQuery(); conn.Close(); { MessageBox.Show("Added successfully!"); } } catch (Exception exc) { MessageBox.Show("An error occured :" + exc.ToString()); } } cheers,

          C Offline
          C Offline
          csanda1
          wrote on last edited by
          #4

          Hi, Thanx for your reply, but it still returns the same error. Tell me is this code compatible with c# .net 2005. Csanda

          1 Reply Last reply
          0
          • C csanda1

            Hi everyone, I've recentley started using C# and am doing an application to insert data using oledataadapter and dataset, and i have an error data type mismatch in this line 'int t1 = adapter.InsertCommand.ExecuteNonQuery();' can anyone tell mi what that means and what ive done wrong, because all my data types are text. Here's the code below: string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datacapt.mdb"; OleDbConnection conn = new OleDbConnection(strConnection); conn.Open(); string strCommand = "INSERT INTO compdata(ID,CompanyName,ContactName) Values ('" + txID.Text + "','" + txtName.Text + "','" + txtcontact.Text + "')"; OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.InsertCommand = new OleDbCommand(strCommand, conn); int t1 = adapter.InsertCommand.ExecuteNonQuery(); conn.Close(); if (t1 > 0) { MessageBox.Show("added successfully!"); } Thanks very much Csanda

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            You might want to simplify it by not using the OleDbDataAdapter, but use an OleDbCommand directly.

            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datacapt.mdb";
            OleDbConnection conn = new OleDbConnection(strConnection);
            conn.Open();
            string strCommand = "INSERT INTO compdata(ID,CompanyName,ContactName) Values ('" + txID.Text + "','" + txtName.Text + "','" + txtcontact.Text + "')";

            OleDbCommand comm = new OleDbCommand(strCommand, conn);

            int t1 = command.ExecuteNonQuery();
            conn.Close();

            if (t1 > 0)
            {
            MessageBox.Show("added successfully!");
            }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups