Quantcast
Channel: SharpDevelop Community
Viewing all articles
Browse latest Browse all 1764

c# sqlite datareader getbytes error

$
0
0

I have start app by i need include a rtf file or image to deal with image, but when try to access the field BLOB type i got error 

System.InvalidCastException: Specified cast is not valid.

   at System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
   at System.Data.SQLite.SQLiteDataReader.GetBytes(Int32 i, Int64 fieldOffset, Byte[ buffer, Int32 bufferoffset, Int32 length)
   at MultiControl.FormQcMes.FormQcMesLoad(Object sender, EventArgs e) in c:\Users\manuel sandoval\Documents\SharpDevelop Projects\MControl\MultiControl\FormQcMes.cs:line 96
   at System.Windows.Forms.Form.OnLoad(EventArgs e)

this the line:

retval=Reader.GetBytes(Reader.GetOrdinal("mesurements"), 0, buffer, 0, length);

 

here is my code:

/*
 * Created by SharpDevelop.
 * User: manuel sandoval
 * Date: 6/28/2015
 * Time: 12:02 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
using System.Data.SQLite;
using System.Data;
using System.IO;
using System.Text;

namespace MultiControl
{
    ///<summary>
    /// Description of FormQcMes.
    ///</summary>
    publicpartialclassFormQcMes : Form
    {
        publicSQLiteConnection_MCon;
        publicSQLiteDataAdapter_sqad;
        publicSQLiteCommand_sqlc;
        publicDataTableTQCMesu;
        privateFormctrlprod_Fprod;
        publicSQLiteDataReaderReader;
        publicFormQcMes(Formctrlprod MForm, SQLiteConnection sqlcon)
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            _MCon = sqlcon;
            _Fprod = MForm;
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
        voidFormQcMesLoad(object sender, EventArgs e)
        {
            int bufferSize = 100;                   // Size of the BLOB buffer.
            byte[ outbyte = newbyte[bufferSize];  // The BLOB byte[ buffer to be filled by GetBytes.
            long startIndex = 0;                    // The starting position in the BLOB output.
            long retval;                            // The bytes returned from GetBytes.  
            string desc=string.Empty;
            int length=256; //the width of the blob
            
            DataTable TQcForm = newDataTable("Mesurements");
            string stm = "SELECT * FROM qcformmesu where id_product = '"+ textBox1.Text+"' order by group_view, row_view ;";
            
            using (SQLiteCommand SqlCmd = newSQLiteCommand( stm,_MCon ))
            {
    
//              using ( SQLiteDataAdapter SqlAd = new SQLiteDataAdapter( SqlCmd))
//              {                   
//                 SqlAd.Fill(TQcForm);
//                 bindingSourceMsu.DataSource= TQcForm;
//                 dataGridVMesu.DataSource= TQcForm; 
        
                Reader = SqlCmd.ExecuteReader();
                TQcForm = Reader.GetSchemaTable();
//                    TQcForm.Load(Reader);
//                    bindingSourceMsu.DataSource= TQcForm;
                      dataGridVMesu.DataSource= TQcForm; 
//                      dataGridVMesu.DataB;
                      Byte[ value = newByte[10];
                    while(Reader.Read())
//                      while( Reader.NextResult())
                    { if (Reader.HasRows)
                     {
                       if (!Reader.IsDBNull(0))
                       {
                           
                           string ID = Reader.GetString(0);
                           string gpo = Convert.ToString(Reader.GetInt32(1));
                        if ( !Reader.IsDBNull(2) )
                        {                           
                             string row = Convert.ToString(Reader.GetInt32(2));
                        }
                        if ( !Reader.IsDBNull(3) )
                        {      
                            desc = Reader.GetString(3);    
                        }
//                        long desc = Reader.GetBytes(4,0,outbyte,0,100);    
                        MessageBox.Show(ID +' '+gpo+' '+desc);
 //                             Reader.GetBytes(4, 0, value, 0, 10);
                        if ( !Reader.IsDBNull(4) )
                        {
                            
                            byte[ buffer = newbyte[length];
                            retval=Reader.GetBytes(Reader.GetOrdinal("mesurements"), 0, buffer, 0, length);
//                            retvalue=Reader.GetByte(4);
//                            retval=Reader.GetBytes(4, 0, value, 0, 10);
//                            Byte[ rtf = new Byte[Convert.ToInt32((Reader.GetBytes(4, 0,
//                                                 null , 0, Int32.MaxValue)))];

//                         long bytesReceived = Reader.GetBytes(4, 0, rtf, 0, rtf.Length); 
//                         ASCIIEncoding encoding = new ASCIIEncoding();
                        }
// Reset the starting byte for the new BLOB.
//                startIndex = 0;
                  
                // Read the bytes into outbyte[ and retain the number of bytes returned.
//                retval = Reader.GetBytes(4, startIndex, outbyte, 0, bufferSize);                         
                        
//                    byte[ rtf = new byte[Reader.GetBytes(3,0,null,0,int.MaxValue)-1];
//                    Reader.GetBytes(3, 0, rtf, 0, rtf.Length); 
//                    MemoryStream stmBLOB = new MemoryStream(rtf);
//                    ptbLogo.Image = Image.FromStream(stmBLOB);

    //                     dataGridVMesu.Rows[0].Cells[3].Value = encoding.GetString(rtf, 0, Convert.ToInt32(bytesReceived));
                           
                       }
                     }
                    }
                

//                 using( SQLiteDataReader    Reader = SqlCmd.ExecuteReader())
//                 
//                       TQcForm.Load(Reader);
//                      bindingSourceMsu.DataSource= TQcForm;
//                     dataGridVMesu.DataSource= TQcForm;
            
//                   while (Reader.Read())
 //                  {
                       
//                   byte[ bytBLOB = new byte[Reader.GetBytes(7,3,null,0,int.MaxValue)-1];
//                    Reader.GetBytes(7, 3, bytBLOB, 0, bytBLOB.Length);

//                    MemoryStream stmBLOB = new MemoryStream(bytBLOB);
//                    dataGridVMesu.Rows[0].Cells[3].Value = bytBLOB;
//                    MemoryStream stmBLOB = new MemoryStream(bytBLOB); 
//                   }
//                 }
                 
              }
              
        }
            
  //  static byte[ GetBytes(SQLiteDataReader reader)
  //  {
  //      const int CHUNK_SIZE = 2 * 1024;
//       byte[ buffer = new byte[CHUNK_SIZE];
//        long bytesRead;
//        int fieldOffset =3;
//        using (MemoryStream stream = new MemoryStream())
//        {
//            if(reader.IsDBNull(fieldOffset)) return null;
//            while ((bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length)) > 0)
//            {
//                stream.Write(buffer, 0, (int)bytesRead);
////                fieldOffset ++;
//            }
//            return stream.ToArray();
 //       }
  //  }
  
        voidDataGridVMesuCellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }            
        
 
    }
}

help pls.


Viewing all articles
Browse latest Browse all 1764

Trending Articles