Error when trying to insert a table using Novacode DocX nuget package - novacode-docx

I'm trying to create/manipulate Word .docx files using the DocX nuget package.
In the documentation they provide the following example:
// Place holder for a Table.
Table t;
// Load document a.
using (DocX documentA = DocX.Load(#"C:\Example\a.docx"))
{
// Get the first Table from this document.
t = documentA.Tables[0];
}
// Load document b.
using (DocX documentB = DocX.Load(#"C:\Example\b.docx"))
{
/*
* Insert the Table that was extracted from document a, into document b.
* This creates a new Table that is now associated with document b.
*/
Table newTable = documentB.InsertTable(t);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
When the code executes I receive an error when inserting the table:
Table newTable = documentB.InsertTable(t);
Error:
System.InvalidOperationException
{"Sequence contains no elements"}
I am at a loss as to why this is occurring. I have looked at the table "t" that is being inserted and it appears to have all of the properties populated. I am unclear as to what is causing the error.
Any help would be greatly appreciated.

I have switched over to using the lastest version of the DLL downloaded from docx.codeplex.com.
I was still having this problem. However I then compared the table that I was coping from my source document to a table that I created directly using the DocX.dll.
I found that for some reason the Design property on my source table was not set to anything, however the table that I created directly was set to Novacode.TableDesign.TableGrid.
I am now setting this property on my source table and everything works as expected.
mytable.Design = Novacode.TableDesign.TableGrid;
I suspect this is a problem in my source document that I am modifying. However since I am not the original author I have no control over this.
Manually setting this property is allowing me to clone the source table, and my solution is now working.

Related

External Content FTS4 Tables in an attached database

I need to add FTS to an existing database.
Started to test external content FTS tables, where the FTS indexes reside in the default (main) DB. Everything worked satisfactorily, except a few things (such as index rebuild) could take considerable amount of time.
Then I read about the possibility to put FTS index into attached DB. This seemed to promise several advantages, hence I decided to give it a try. However, all my trials failed. Here are a few examples:
Situation
We have a table 'account' with a text column 'code', and
Want to create FTS index for that column and place it into separate database file
Test1) ERROR: near ".": syntax error
ATTACH 'ZipFts.sdf' AS ZipFts; CREATE VIRTUAL TABLE ZipFts.account USING fts4(content=account, code);
INSERT INTO ZipFts.account(ZipFts.account) VALUES('rebuild');
Test 2) ERROR: Stack overflow (infinite recursion inside sqlite engine)
ATTACH 'ZipFts.sdf' AS ZipFts; CREATE VIRTUAL TABLE ZipFts.account USING fts4(content=account, code);
INSERT INTO ZipFts.account(account) VALUES('rebuild');
Test3) ERROR: no such table: ZipFts.account
ATTACH 'ZipFts.sdf' AS ZipFts; CREATE VIRTUAL TABLE ZipFts.ZipFts_account USING fts4(content="account", code);
INSERT INTO ZipFts_account(ZipFts_account) VALUES('rebuild');
Test4) ERROR: no such table: ZipFts.main.account
ATTACH 'ZipFts.sdf' AS ZipFts; CREATE VIRTUAL TABLE ZipFts.ZipFts_account USING fts4(content="main.account", code);
INSERT INTO ZipFts_account(ZipFts_account) VALUES('rebuild');
Does anybody know how these things work? Thanks in advance.
After some searching in sqlite3.c I found what might be the answer.
Look at the bottom of the function fts3ReadExprList(). The name of the content table is prefixed with the DB name here! This explains everything.
Moreover, this seems to be the only non-trivial use of zContentTbl (= the name of the content table). When I slightly modified fts3ReadExprList() function as shown in the code underneath, the problem disappeared.
// Code inserted by #JS-->
// Do not prefix zContentTbl with the database name. The table might reside in main database, for example.
if( p->zContentTbl){
fts3Appendf(pRc, &zRet, " FROM '%q' AS x", p->zContentTbl);
}
else
// <--#JS
fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
...
Note that I did not test the code sufficiently. (So far I only know that the FTS index was created.)
Anyway, for the time being, I consider this an SQLite bug and I'll try to go on with my fix.
I think this is as designed.
If it were otherwise, the underlying table for an external content table could change as databases are attached or detached.
You might be able to achieve this using a contentless FTS Table though.
Dan Kennedy.

Using Full text search on varbinary data type for searching file contents

I create a full-text search index on varbinary(max) column and insert file content into this column.(files like docx,xlsx,txt,...) I create an extension column for specify content type.but when I query this table with contains,
SELECT *
FROM tblFiles
WHERE CONTAINS(tblFiles.FileContent, 'web OR webserver');
no result were showing.why?
You may need to wait a bit after defining a full text index, because the index itself is created in the background; it's an async process, not synchronous. For a small table it may not be noticeable, but for a large one, it will be.
Also, make sure the types in your file type column start with a dot: .docx instead of just docx.
I found why my query dont work. because files that inserted into db was docx and in my database does not exist any filter for this format.I inserted a doc document and query it.It works. :)
Thank you.

Sqlite: Create a table and fill with information only once

I have an appcelerator titanium project (you don't need to be familiar with the platform to help me) that I am using to create an iOS app. I want to have a database with a table that is filled with several rows for the first time, and then left alone after the first time.
I know you can create a table if it doesn't exist. Is there something similar for Inserting data?
Thanks!
Expanding on MPelletier's response with some Titanium-specific code, you could do the following in your project:
var my_db = Ti.Database.open('nameofdb');
var my_result_set = my_db.execute('SELECT * FROM MyTable');
var records = my_result_set.rowCount;
The records variable will indicate whether or not you have data in your table and then you can act accordingly.
There are a couple of nice ORM-ish utilities out there for Titanium: TiStore and Joli are the two I've used. Both are inspired by ActiveRecord and can be helpful in reducing your DB-related code. They're on Github if you want to know more about them!
There's INSERT OR REPLACE if you want, but you might as well just check against the number of rows on the table:
SELECT COUNT(*) FROM MyTable;
Then decide to enter them on that.
You can create your DB and insert data in it via SQLLiteManager or whatever tool you want and then dump it.
Take the file you dumped, put it in your Titanium project folder (somewhere in the Resources folder).
Then this line of code will take the content of the .sqlite file and insert it in the iOS db:
var db = Ti.Database.install('../your-db.sqlite', 'your-db-name');
Just don't forget the CREATE IF NOT EXISTS statement in your SQL file.
Or use REPLACE instead of INSERT as MPelletier said.

The Ghost of a Renamed Table

I have renamed a table in a SQL Server 2008 database, from eL_CourseStepUserNotes to StepUserNotes. I renamed the table using the SSMA.
The table is used in a ASP.NET 4.0 app. I use LINQ to SQL for all CRUD. The problem is that the following code:
dbDataContext db = new dbDataContext();
var k = (from c in db.StepUserNotes
where ((c.CourseStepFK == q.CourseStepFK) && (c.UserFK == q.UserFK))
select c).FirstOrDefault();
try
{
db.StepUserNotes.InsertOnSubmit(q);
db.SubmitChanges();
}
catch
{
}
Fails on the db.SubmitChanges line, saying:
SqlException was caught. Invalid object name 'eL_CourseStepUserNotes'.
Ie, the old name for the table has come back to haunt me.
I have deleted the old LINQ to SQL dbml file and created a new one. I have searched through all the source code for strings that contain the old table name. Nothing. The code compiles...
Where else can I look?
The error is coming back from SQL Server, and using the utility for listing all foreign keys in a sql server database shown in SO question:
sql:need to change constraint on rename table?
reveals no sign of the old table name in FKs either.
I am at a complete loss as to where to look or what to try next. Any suggestions?
Answer:
The problem, as stated by Stu and Stark was a trigger. Stu gave me the SQL to run that nailed the problem. It is documented here in case anyone else runs into this:
Select Object_Name(ID) From SysComments
Where Text Like '%el_CourseStepUserNotes%'
This revealed a trigger with the following name:
tr_eL_CourseStepUserNotes
The trigger referenced the old name as follows:
SET DateAmended = CURRENT_TIMESTAMP
FROM eL_CourseStepUserNotes PP
INNER JOIN inserted i ON PP.UserNoteId = i.UserNoteId
Everything is now working again.
Silly me, I should have realised that a trigger was the problem as the first error I got was related to the DateAmended field.
I have no idea why a trigger would update when a table name changed. I had checked all Keys and relationships, but forgot this trigger.
Live and learn.
When you recreated your linq to sql file, did you remember to refresh the server browser to reflect the table name change first?
Edit - I mean the visual studio server browser.
you need to rebind your LINQ To SQL class, drop all the tables, then add them again from the Server Explorer
open your project in visual studio.
open your dbml file.
find the table you just have renamed.
select and delete it.
Save.
Open server Explorer.
Connect to your database.
Find the table which you have renamed.
Drag and drop on the designer of your dbml file.
Save the file again.
Compile your project.
It is not necessary to drop the tables from your DBML file. You can simply open the designer, click on the class diagram representing your table and type the new name of the table.
However, if you're recreating from scratch it is important to refresh in Server Explorer otherwise it will still pull the old name from the schema.

ADD COLUMN to sqlite db IF NOT EXISTS - flex/air sqlite?

I've got a flex/air app I've been working on, it uses a local sqlite database that is created on the initial application start.
I've added some features to the application and in the process I had to add a new field to one of the database tables. My questions is how to I go about getting the application to create one new field that is located in a table that already exists?
this is a the line that creates the table
stmt.text = "CREATE TABLE IF NOT EXISTS tbl_status ("+"status_id INTEGER PRIMARY KEY AUTOINCREMENT,"+" status_status TEXT)";
And now I'd like to add a status_default field.
thanks!
Thanks - MPelletier
I've add the code you provided and it does add the field, but now the next time I restart my app I get an error - 'status_default' already exists'.
So how can I go about adding some sort of a IF NOT EXISTS statement to the line you provided?
ALTER TABLE tbl_status ADD COLUMN status_default TEXT;
http://www.sqlite.org/lang_altertable.html
That being said, adding columns in SQLite is limited. You cannot add a column anywhere but after the last column in your table.
As for checking if the column already exists, PRAGMA table_info(tbl_status); will return a table listing the various columns of your table.
ADD ON:
I've been using a strategy in database design that allows me to distinguish which modifications are required. For this, you will need a new table (call it DBInfo), with one field (Integer, call it SchemaVersion). Alternately, there is also an internal value in SQLite called user_version, which can be set with a PRAGMA command. Your code can, on program startup, check for schema version number and apply changes accordingly, one version at a time.
Suppose a function named UpdateDBSchema(). This function will check for your database schema version, handle DBInfo not being there, and determine that the database is in version 0. The rest of this function could be just a large switch with different versions, nested in a loop (or other structure available to your platform of choice).
So for this first version, have an UpgradeDBVersion0To1() function, which will create this new table (DBInfo), add your status_default field, and set SchemaVersion to 1. In your code, add a constant that indicates the latest schema version, say LATEST_DB_VERSION, and set it to 1. In that way, your code and your database have a schema version, and you know you need to synch them if they are not equal.
When you need to make another change to your schema, set the LATEST_DB_VERSION constant to 2 and make a new UpgradeDBVersion1To2() function that will perform the required changes.
That way, your program can be ported easily, can connect to and upgrade an old database, etc.
I know this is an old question... however.
I've hit this precise problem in the SQLite implementation in Adobe AIR. I thought it would be possible to use the PRAGMA command to resolve, but since adobe air's implementation does not support the PRAGMA command, we need an alternative.
What I did, that I thought would be worth while sharing here, is this:
var sql:SQLStatement = new SQLStatement();
sql.sqlConnection = pp_db.dbConn;
sql.text = "SELECT NewField FROM TheTable";
sql.addEventListener(SQLEvent.RESULT, function(evt:SQLEvent):void {
});
sql.addEventListener(SQLErrorEvent.ERROR, function(err:SQLErrorEvent):void {
var sql:SQLStatement = new SQLStatement();
sql.sqlConnection = pp_db.dbConn;
sql.text = "ALTER TABLE TheTable ADD COLUMN NewField NUMERIC;";
sql.execute();
sql.addEventListener(SQLEvent.RESULT, function (evt:SQLEvent):void {
});
});
sql.execute();
Hope it helps someone.
I solved a similar problem using the answer from this question:
ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite
Use built in user_version parameter to keep track of your updates. You set it using:
PRAGMA user_version = 1
and you retrieve it using
PRAGMA user_version
So basically retrieve user_version (it's 0 by default), check if it's 0. If yes, perform your updates and set it to 1. If you have more updates in the future, check if it's 1, perform updates and set it to 0. And so on...
In some cases I execute the command and get the exception for "duplicate column". Just a quick solution, not the perfect.

Resources