data type mismatch error
-
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 -
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 CsandaHi 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, -
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 CsandaAlthough 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 useExecuteScalar
. However, this returns a type calledobject
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 whatt1
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. -
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, -
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 CsandaYou 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!");
}