Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!

I have a problem, and for several a hours i try to found what i'm doing wrong.
So, i want to delete a record from a table(i use acces). I create a combobox where i put all id's cars. But i receive this error : "Unable to find TableMapping["cars"] or DataTable "cars""
Bellow is the code, please help with a solution.



private void button1_Click(object sender, EventArgs e)
{
string str = comboBox1.Text;
OleDbConnection myConex = new OleDbConnection(sirConex);

string deletecar = "Delete from cars where id_car=@str";
OleDbCommand cmd = new OleDbCommand(deletecar, myConex);

cmd.Parameters.Add("@str", OleDbType.Char).Value = str;


mda.InsertCommand = cmd;


try
{
mda.Update(mds,"cars");


}

catch (Exception excpt)
{
MessageBox.Show("Error:" + excpt.Message);

}
}
Posted

I assume you have a table called cars ? It looks a bit backwards to me that you'd set an 'insertcommand' to delete. It's also obviously hideous that you'd doing this in the presentation layer, but I assume it's just learning code. What is mda ? Why don't you just run the SQL without using it ?
 
Share this answer
 
Where is your Connection.Open ?
 
Share this answer
 
Yes, i have a table cars.
mda is declared like this

OleDbDataAdapter mda=new OleDbDataAdapter();
 
Share this answer
 
OK - I've never used that, I just write straight SQL commands through a straight connection. I think the other guy is right, I didn't know what you were using, so I didn't look for an open command, but it's not there. Here is the MSDN sample for this class:

OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection);

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the Insert, Update and Delete commands.
    adapter.InsertCommand = new OleDbCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (?, ?)");

    adapter.UpdateCommand = new OleDbCommand(
        "UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
        "WHERE CustomerID = ?");

    adapter.DeleteCommand = new OleDbCommand(
        "DELETE FROM Customers WHERE CustomerID = ?");

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID", 
        OleDbType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName", 
        OleDbType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID", 
        OleDbType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName", 
        OleDbType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID", 
        OleDbType.Char, 5, "CustomerID").SourceVersion = 
        DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID", 
        OleDbType.Char, 5, "CustomerID").SourceVersion = 
        DataRowVersion.Original;

    return adapter;



Two differences I note.

1 - they use a ? to denote the location of a parameter
2 - they use the deletecommand to delete stuff ( makes sense to me )
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900