Ha, fixed it!

Firstly, since I found it worked if I did..

NSData data = NSdata.FromString("some text");

... I tried loading 'data' from various text and binary files on the disk.
Text ones worked, binary didn't which suggested something about the binding.

After eating a bit of humble pie and paying closer attention to the
exception details...

{System.ArgumentNullException: Argument cannot be null. Parameter name: s  
at System.Text.Encoding.GetByteCount (System.String s) [0x00006] in
/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Text/Encoding.cs:192   
 

at System.Text.UTF8Encoding.GetByteCount (System.String chars) [0x00000] in
/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Text/UTF8Encoding.cs:912
    

at Mono.Data.Sqlite.SqliteConvert.ToUTF8 (System.String sourceText)
[0x00000] in
/Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteConvert.cs:76
    

at Mono.Data.Sqlite.SQLite3.Bind_Text (Mono.Data.Sqlite.SqliteStatement
stmt, Int32 index, System.String value) [0x00000] in
/Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLite3.cs:385
    

at Mono.Data.Sqlite.SqliteStatement.BindParameter (Int32 index,
Mono.Data.Sqlite.SqliteParameter param) [0x001b6] in
/Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteStatement.cs:217
    

at Mono.Data.Sqlite.SqliteStatement.BindParameters () [0x0001c] in
/Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteStatement.cs:151
   

at Mono.Data.Sqlite.SqliteCommand.BuildNextCommand () [0x000ac] in
/Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteCommand.cs:240
}

... this kind of back up that supposition.

Another look at the SqliteCommand.Parameter object shows it has a 'DBType'
parameter, by default this was 'DBType.Object', I changed it to
'DBType.Binary' (not actually required), which then threw an exception when
setting the parameter value, unable to cast data asbyte[], so by including
System.Linq to provide an NSData.ToArray() (thanks Google) method and it
worked!

So my final code.....

using System.Linq;

UIImage img =...... 
NSData data = img.AsPng(); 
using (var command = new SqliteCommand()) 
{ 
  command.Connection = .... 
  command.CommandText = "insert into imagetable (iref, myimage) values (1,
@imagedata)";

  command.Parameters.Add("@imagedata"); 
  command.Parameters[0].Size = (int)data.Length; 
  command.Parameters[0].Value = data.ToArray();

  command.ExecuteScalar(); 
} 


At the end of the day, it was just the way I was assigning data to the
parameter value that needed attention.

Thanks for your help, we got there in the end!

Chris




--
View this message in context: 
http://monotouch.2284126.n4.nabble.com/Inserting-image-into-SQLite-tp4565016p4570230.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to