Vamsi Challa
Vamsi Challa

Reputation: 11109

Using auto increment column

I have 12 columns with the first column as primary key and autoincrement. So in my insert command i am passing 11 values, but i keep getting the error saying, table has 12 columns but 11 values supplied.

Here is the create table statement:

public String CREATE_TABLE = "CREATE TABLE coupons"
    + "(sno INTEGER PRIMARY KEY AUTOINCREMENT, "
                             + "title VARCHAR(20), " +
                             "description VARCHAR(20), " +
                             "imagepath VARCHAR(20), " +
                             "couponpath VARCHAR(20), " +
                             "barcodepath VARCHAR(20), " +
                             "downloadedon VARCHAR(20), "
                             + "redeemedon VARCHAR(20), " + "starttime VARCHAR(20), " + "endtime VARCHAR(20), "
                             + "status VARCHAR(20), statustext VARCHAR(20))";

Here is my insert statement:

    String insertString = "INSERT INTO coupons VALUES(@title, @description, @imagepath, @couponpath, @barcodepath, @downloadedon, @redeemedon, @starttime, @endtime, @status, @statustext)";

    SqliteCommand command = new SqliteCommand (insertString, m_dbConnection);
    command.Parameters.AddWithValue ("@title", title);
    command.Parameters.AddWithValue ("@description", description);
    command.Parameters.AddWithValue ("@imagepath", imagepath);
    command.Parameters.AddWithValue ("@couponpath", couponpath);
    command.Parameters.AddWithValue ("@barcodepath", barcodepath);
    command.Parameters.AddWithValue ("@downloadedon", downloadedon);
    command.Parameters.AddWithValue ("@redeemedon", redeemedon);
    command.Parameters.AddWithValue ("@starttime", starttime);
    command.Parameters.AddWithValue ("@endtime", endtime);
    command.Parameters.AddWithValue ("@status", status);
    command.Parameters.AddWithValue ("@statustext", statustext);

    command.ExecuteNonQuery ();

How to increment an auto-increment field with ADO.NET in C# seemed to help, but didn't work. What is that i am doing wrong?

What was I doing wrong?

You have to mention all other column names while using the insert statement, other than the Autoincrement column.

My Insert Statement now:

    String insertString = @"INSERT INTO coupons(title, description, imagepath, couponpath, barcodepath, downloadedon, redeemedon, starttime, endtime, status, statustext) VALUES(@title, @description, @imagepath, @couponpath, @barcodepath, @downloadedon, @redeemedon, @starttime, @endtime, @status, @statustext)";

Even this works :

String insertString = @"INSERT INTO coupons VALUES(NULL, @title, @description, @imagepath, @couponpath, @barcodepath, @downloadedon, @redeemedon, @starttime, @endtime, @status, @statustext)";

EDIT:

Now the error is gone, but I don't see any value when i retrieve sno from table.

Sno:    Title: Sprint Customer Feedback Campaign

As you can see Sno is empty. Why could this be?

Here is how I am reading data from db..

public void getDataFromTable ()
{

    string selectString = "select * from coupons";
    SqliteCommand command = new SqliteCommand (selectString, m_dbConnection);
    SqliteDataReader reader = command.ExecuteReader ();
    while (reader.Read ()) {
        Console.WriteLine ("Sno: " + reader ["sno"] +
        "\tTitle: " + reader ["title"] +
        "\tDescription: " + reader ["description"] +
        "\tImagePath: " + reader ["imagepath"] +
        "\tCouponPath: " + reader ["couponpath"] +
        "\tBarcodePath: " + reader ["barcodepath"] +
        "\tDownloadedOn: " + reader ["downloadedon"] +
        "\tRedeemedOn: " + reader ["redeemedon"] +
        "\tStartTime: " + reader ["starttime"] +
        "\tEndTime: " + reader ["endtime"] +
        "\tStatus: " + reader ["status"] +
        "\tStatusText: " + reader ["statustext"]
        );
    }
    Console.ReadLine ();
}

Upvotes: 0

Views: 1063

Answers (2)

laalto
laalto

Reputation: 152847

Hamlet Hakobyan has one solution but there's another way:

If you insert a NULL to the INTEGER PRIMARY KEY column, the row id autogeneration kicks in. So keep the code and add the NULL to the SQL here:

String insertString = "INSERT INTO coupons VALUES(NULL, @title, @description, @imagepath, @couponpath, @barcodepath, @downloadedon, @redeemedon, @starttime, @endtime, @status, @statustext)";

Upvotes: 1

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

The correct insert query in your case looks like this:

String insertString = @"INSERT INTO coupons(title, description, imagepath, couponpath, barcodepath, downloadedon, redeemedon, starttime, endtime, status, statustext)
VALUES(@title, @description, @imagepath, @couponpath, @barcodepath, @downloadedon, @redeemedon, @starttime, @endtime, @status, @statustext)";

You must enlist all columns explicitly if not all columns affected in insert. Omitting the columns list mean that you must insert all columns.

Upvotes: 2

Related Questions