1

I keep getting this error every time I try to add something into the database. I am trying to add something into a database that's already in the executable.

This is how I am trying to add it:

public class DBAccess
{
    public static void AddTransaction(Transaction transaction)
    {
        using IDbConnection dbConnection = new SQLiteConnection(LoadConnection());
        dbConnection.Execute("INSERT INTO Transaction (Category, Name, Type, Date, Amount, Method, Description, Recurring" +
            ") VALUES (@Category, @Name, @Type, @Date, @Amount, @Method, @Description, @Recurring)", transaction);
    }

    public static string LoadConnection(string conn = "Connection")
    {
        return ConfigurationManager.ConnectionStrings[conn].ConnectionString;
    }
}

This is the model:

public class Transaction
{
    public int TransactionID { get; set; }
    public string Category { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string Date { get; set; }
    public int Amount { get; set; }
    public string Method { get; set; }
    public string Description { get; set; }
    public string Recurring { get; set; }
}

This is what I am trying to add:

class Program
{
    static void Main(string[] args)
    {
        Transaction transaction = new Transaction();

        Console.WriteLine("Category: ");
        transaction.Category = Console.ReadLine();

        Console.WriteLine("Name: ");
        transaction.Name = Console.ReadLine();

        Console.WriteLine("Type: ");
        transaction.Type = Console.ReadLine();

        Console.WriteLine("Date: ");
        transaction.Date = Console.ReadLine();

        Console.WriteLine("Amount: ");
        bool isAmountConverted = int.TryParse(Console.ReadLine(), out int amount);

        if (isAmountConverted)
        {
            transaction.Amount = amount;
        }

        Console.WriteLine("Method: ");
        transaction.Method = Console.ReadLine();

        Console.WriteLine("Description: ");
        transaction.Description = Console.ReadLine();

        Console.WriteLine("Recurring: ");
        transaction.Recurring = Console.ReadLine();

        DBAccess.AddTransaction(transaction);
    }
}

This is the database:

This is the database

1 Answer 1

1

Transaction is a Keyword. you should replace Transaction to [Transaction] in your query

public static void AddTransaction(Transaction transaction)
{
    using IDbConnection dbConnection = new SQLiteConnection(LoadConnection());
    dbConnection.Execute("INSERT INTO [Transaction] (Category, Name, Type, Date, Amount, Method, Description, Recurring" +
            ") VALUES (@Category, @Name, @Type, @Date, @Amount, @Method, @Description, @Recurring)", transaction);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Farhad Zamani thanks dude you’ve corrected, I was pulling my hair out trying to deal with this.
@David Glad i helped you ;). if my answer is correct please accept my answer as correct answer.
@FarhadZamani thanks for your solution, i had same problem but with a field so also you we can do "INSERT INTO Action ([Order], Name) ...."

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.