Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to add images to my database From winForms application to SQL Server 2008 using disconnected mode in ADO.NET, but I have a problem in this code, I can't add my images to my database

I always get a null value.



this is my code :




C#
string Query;
        SqlDataAdapter dataadapter;
        DataSet dataset = new DataSet();
        OpenFileDialog dialog = new OpenFileDialog();
        BindingSource tblname = new BindingSource();


        private void la_Load(object sender, EventArgs e)
        {

     Query = "select * from Imagee ";
            dataadapter = new SqlDataAdapter(Query, Connexion.cnx);
            dataadapter.Fill(dataset,"Timage");



          textBox1.DataBindings.Add(new Binding("Text", dataset, "Timage.Id"));

          Binding b = new Binding("Image", dataset, "Timage.imagee");

          b.Format += new ConvertEventHandler(b_Format);
          pictureBox1.DataBindings.Add(b);

            dataGridView1.DataMember = "Timage";
            dataGridView1.DataSource = dataset;

        }
        private void b_Format(object sender, ConvertEventArgs e)
        {

                byte[] b = (byte[])e.Value;
                System.IO.MemoryStream ms = new System.IO.MemoryStream(b);

                Bitmap bmp = new Bitmap(ms);

                ms.Close();

                e.Value = bmp;

        }
        private void Browse_Click(object sender, EventArgs e)
        {

            dialog.Filter = "JPEG|*.jpg";
            if (dialog.ShowDialog() == DialogResult.OK)
            {

                Image image = Image.FromFile(dialog.FileName);

                pictureBox1.Image = image;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            this.BindingContext[dataset, "Timage"].AddNew();

            //SqlCommandBuilder sql = new SqlCommandBuilder(dataadapter);
            //dataadapter.Update(dataset, "Timage");



        }



the error is when i click to add button for adding a new row to my database i have null value inserted in field of image and i receive this message

Unable to cast an object of type 'System.DBNull' to type 'System.Byte []'. in this line


byte[] b = (byte[])e.Value;
Posted
Comments
PIEBALDconsult 4-Apr-14 21:02pm    
That looks like reading code, not adding code. As far as the statement you point out, check it for DBNull.Value before trying to cast it.
But if the problem is that none of the images are stored, then you'll need to show the code that does the storing.
Member 10476498 5-Apr-14 4:41am    
when i use this code with only textbox and other control but not picturebox it works very good i can do my adding and deleting and updating without any probleme the advantage of this Mode disconect that i can add or delete or navigate on only one line like that
this code for adding an element to a dataset

this.BindingContext[dataset, "Timage"].AddNew();
and I can add my textbox the probleme is only in picturebox
and instead using another button to save data in my database i use in the same button :

SqlCommandBuilder sql = new SqlCommandBuilder(dataadapter);
dataadapter.Update(dataset, "Timage");

so i use disconect mode for minimizing my code because i will work in another project that i will have more and more fields


I hope now that I have clarified my problem !

C#
Query = "select * from Imagee ";
       dataadapter = new SqlDataAdapter(Query, Connexion.cnx);
       dataadapter.Fill(dataset, "Timage");

       Connexion.cnx.Close();//First dispose your connection object
       //Then give a condition to next of work
       if (dataset != null && dataset.Tables.Count > 0)
       {
           textBox1.DataBindings.Add(new Binding("Text", dataset, "Timage.Id"));

           Binding b = new Binding("Image", dataset, "Timage.imagee");

           b.Format += new ConvertEventHandler(b_Format);
           pictureBox1.DataBindings.Add(b);

           dataGridView1.DataMember = "Timage";
           dataGridView1.DataSource = dataset;
       }
 
Share this answer
 
Comments
Member 10476498 5-Apr-14 4:39am    
have you test your code ? is not working in my home
the error is the same error like before
i think false
C#
dialog.Filter = "JPEG|*.jpg";
            if (dialog.ShowDialog() == DialogResult.OK)



Xem phim online
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Apr-14 0:33am    
Have some shame. How can it be an answer?
—SA
Member 10476498 5-Apr-14 4:40am    
why you think is false ?
i show my image in the picturebox after clic in browse button for bind it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900