i have a c# project with a table "Pics". it has two columns R_no(int) Picto(OLE). How i can store images if different formats in this table and retrieve it to show in a picture box. plz help me to solve this
my code is as below

string q2 = "select picto from pics where R_no=" +Convert.ToInt16(cmb_rno.SelectedItem) + "";
pictureBox1.Image = obj.toreader(q2);
public Image toreader(string qury)
        {
            Image img=null;
            try
            {
                OleDbCommand cmd = new OleDbCommand(qury, mycon);
                mycon.Open();
                OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                    if (rdr.Read())
                    {
                        MessageBox.Show("READ SUCCESFULLY");
                        byte[] b = (byte[])rdr.GetValue(0);
                        img = new Bitmap(new MemoryStream(b, true));
                    }
                mycon.Close();
                return img;
            }
            catch (Exception ex)
            {
                mycon.Close();
                MessageBox.Show("Eror While reading by reader\n"+ex.Message);
                return img;
            }

it gives error like this "parameter is not valid"

Recommended Answers

All 7 Replies

thanx for ur kind solution.
but this does not solve my problem completely. please change or modify or tick out the mistakes in my code so the problem may solved.
thnx

Here is another example. You should modify it to fit your needs. It is unclear why you are using the approach you posted if you're only obtaining a single image.

using System;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmImageUpDown : Form
  {
    /*
    IF OBJECT_ID('IMG', 'U') IS NOT NULL DROP TABLE IMG
    CREATE TABLE IMG
    (
      RecordId int identity(1000, 1) PRIMARY KEY,
      IMG varbinary(max)
    )
     */
 
    const string connStr = @"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Scott;Data Source=apex2006sql";
    
    public frmImageUpDown()
    {
      InitializeComponent();
    }

    private void buttonUpload_Click(object sender, EventArgs e)
    {
      using (OleDbConnection conn = new OleDbConnection(connStr))
      {
        conn.Open();
        using (OleDbCommand cmd = new OleDbCommand("Insert Into IMG (IMG) Values (?)", conn))
        {
          cmd.Parameters.Add(new OleDbParameter("@IMG", OleDbType.Binary)).Value = System.IO.File.ReadAllBytes(@"C:\picture2.bmp");
          cmd.ExecuteNonQuery();
        }
        conn.Close();
      }
    }

    private void buttonDownload_Click(object sender, EventArgs e)
    {
      using (OleDbConnection conn = new OleDbConnection(connStr))
      {
        conn.Open();
        using (OleDbCommand cmd = new OleDbCommand("Select Top 1 IMG From IMG", conn))
        {
          byte[] buffer = (byte[])cmd.ExecuteScalar();
          using (MemoryStream ms = new MemoryStream(buffer))
          {
            ms.Position = 0;
            ms.Seek(0, SeekOrigin.Begin);
            Bitmap bmp = new Bitmap(ms);
            pictureBox1.Image = bmp;
          }
        }
        conn.Close();
      }
    }

    private void buttonDownload2_Click(object sender, EventArgs e)
    {
      using (OleDbConnection conn = new OleDbConnection(connStr))
      {
        conn.Open();
        using (OleDbCommand cmd = new OleDbCommand("Select Top 1 * From IMG", conn))
        {
          using (OleDbDataReader dr = cmd.ExecuteReader())
          {
            using (DataTable dt = new DataTable())
            {
              dt.Load(dr);
              DataRow row = dt.Rows[0];
              MessageBox.Show(string.Format("Displaying image with record id {0:F0}", (int)row["RecordId"]));
              using (MemoryStream ms = new MemoryStream((byte[])row["IMG"]))
              {
                ms.Position = 0;
                ms.Seek(0, SeekOrigin.Begin);
                Bitmap bmp = new Bitmap(ms);
                pictureBox1.Image = bmp;
              }
            }
          }

        }
        conn.Close();
      }
    }
  }
}

This code give an error "Parameter is not valid" please give me the solution

You broke it?! The code I posted works as-is. Post the code you are using and indicated which line is causing the exception.

This is my code This gives the error "out of Memory"**

string constr = "Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\\ISO.mdb";
            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand cmd = new OleDbCommand("select pito from pictos where id="+cmbrno.SelectedItem+"", con);
            
            try
            {
                con.Open();
                byte[] picbyte = (byte[])cmd.ExecuteScalar();
                FileStream fs = new FileStream("img", FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(picbyte, 0, picbyte.Length);
                fs.Flush();
                fs.Close();
                pictureBox1.Image = Image.FromFile("img");**
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

What is the problem then? Your machine is obviously running out of memory. Is that image huge or is your query returning a lot of results? If not then consider upgrading your hardware or killing applications that you don't need running. You can also increase the amount of virtual memory on your machine inside of the windows control panel somewhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.