3

I have problem delete data microsoft access database with C# I use this method to add data to microsoft access database

public static int autoIncrement(string kalimatSql)
{
    int lastIndex = -1;
    dbCon.Open();
    dbCmd.CommandText = kalimatSql;
    dbCmd.ExecuteNonQuery();
    dbCmd.CommandText = "Select @@Identity";
    lastIndex = Convert.ToInt32(dbCmd.ExecuteScalar());
    dbCon.Close();
    return lastIndex;
}

I call that method with

myQuiz.id = Global.autoIncrement("INSERT INTO Quizzes (Name) VALUES ('" + myQuiz.name + "')");
Global.quizzes.Add(myQuiz);

I wonder how to delete data from database? if I add with that way?
So far I already try this way

public static int deleteData(string kalimatSQL)
{
    int lastIndex = -1;
    dbCon.Open();
    dbCmd.CommandText = kalimatSQL;
    dbCmd.ExecuteNonQuery();
    dbCon.Close();
    return lastIndex;
}

And I call delete method with this way

if ( listBoxQuizzes.SelectedIndex != -1)  {
    Global.deleteData("DELETE FROM Quizzes WHERE name=" +listBoxQuizzes.SelectedItem.ToString()); 
}

But give error result

"C# Syntax error (missing operator) in query expression 'name = quiz002' " can someone help me fix it?

1 Answer 1

1

You are missing single quotes around name value ('')

It should be like this:

Global.deleteData("DELETE FROM Quizzes WHERE name = '" + listBoxQuizzes.SelectedItem.ToString() + "'");
Sign up to request clarification or add additional context in comments.

1 Comment

u're right thanks for helped...i will mark u answer as correct one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.