0

I don't know why this code gives me an error. I am trying to put the sql commands into a transaction. This code gives me this error. I can't fill int anything else at the source than this. This is the error I get

Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

using (SQLiteConnection cn = new SQLiteConnection(string.Format("Data Source={0};")))
                    {
                        cn.Open();
                        using (SQLiteTransaction tr = cn.BeginTransaction())
                        {

                            sqlTarget = sqlInsert1 + columnList + ") VALUES (";
                            object[] sourceVal = new object[nCol];
                            rdrSourceTable.GetValues(sourceVal);
                            string strMsg = string.Empty;
                            int iCol = 0;
                            foreach (object col in sourceVal)
                            {
                                string columnName = rdrSourceTable.GetName(iCol++);
                                sqlTarget += objDbTarget.ObjectForSql(col, ref strMsg, false, columnName) +
                                             comma;
                            }
                            if (strMsg.Length > 0)
                            {
                                msg = string.Format(
                                    "The input values are wrong, strMsg = {0}\r\nThe composed sql = {1}",
                                    strMsg, sqlTarget);
                                if (m_interactive)
                                {
                                    DialogResult res = MessageBox.Show(msg, GetType().ToString(),
                                        MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                                    if (res == DialogResult.Cancel)
                                    {
                                        throw new CopyDbContentsException(msg);
                                    }
                                }
                                if (errorCount++ < 5)
                                {
                                    RadFile.WriteLogMsg("FillTableWithInsertCommands. " + msg +
                                                        "\r\n\r\nContinue?");
                                }
                                //Skip the insert action because of the error and go to next row.
                                continue;
                            }
                            sqlTarget = sqlTarget.Substring(0, sqlTarget.Length - comma.Length) + ")";
                            objDbTarget.ExecuteActionQuery(sqlTarget);
                            iRow++;
                            int remainder = iRow%250;
                            if (remainder == 0)
                            {
                                WriteStatusLabel(string.Format(
                                    "Copy the rows of table {0}. {1:N0} written.", Name,
                                    iRow));
                            }
                            remainder = iRow%nMsgMod;
                            if (remainder == 0)
                            {
                                msg = string.Format("{0:N0} rows of table {1} copied.",
                                    iRow, Name);
                                RadFile.WriteLogMsg(msg, withHeader: false);
                                if (nMsgMod < 100000 && iRow == 10*nMsgMod)
                                {
                                    nMsgMod *= 10;
                                }
                            }
                            tr.Commit();
                        }
                        cn.Close();
                    }
                }
                msg = string.Format("Table {0} is copied, {1:N0} rows. ", Name, iRow);
                        if (errorCount > 0)
                        {
                            msg += errorCount;
                            msg += (errorCount == 1) ? " row is" : " rows are";
                            msg += " skipped because of errors in the input";
                        }
                        RadFile.WriteLogMsg(msg, withHeader: false);
            }
        }
Petah
  • 127
  • 1
  • 7

3 Answers3

2

you expect this to work?

SQLiteConnection cn = new SQLiteConnection(string.Format("Data Source={0};"))

here is a good explaination about SQLite ConnectionStrings

https://www.connectionstrings.com/sqlite/

i assume you want to do something like

var cn = new SQLiteConnection(string.Format("Data Source={0};Version=3;", @"c:\mydb.db"))

And the Error

Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

says that string.Format("Data Source={0};") want's to access the first item at index 0, which is not provided by you

fubo
  • 44,811
  • 17
  • 103
  • 137
  • I am trying to use the transaction to increase the write speed of sqlite by putting it in a transaction. I now get an error saying database locked – Petah Sep 03 '15 at 11:53
  • 1
    there are a lot of existing answers about that http://stackoverflow.com/questions/17592671/sqlite-database-locked-exception – fubo Sep 03 '15 at 12:09
1

Its at the 1st line:

string.Format("Data Source={0};"

you must provide argument for string format , for example :

string.Format("Data Source={0};","my data source")

where "my data source" will be your database data source name.

Alex
  • 768
  • 1
  • 5
  • 13
1

Man, you should refactor all this code. But for you problem, this is the solution:

using (SQLiteConnection cn = new SQLiteConnection("put your entire connection string here"))

see this for more information on how to use string.Format method.

Bruno Casarotti
  • 623
  • 8
  • 23