Retrieve images in C# WPF Application from SQL Server Database

maqskywalker
maqskywalker used Ask the Experts™
on
Hi Experts,
I'm working through this tutorial

How to Save and Retrieve images in C# WPF Application from SQL Server Database
http://www.redmondpie.com/how-to-save-and-retrieve-images-in-c-wpf-application-from-sql-server-database/

This is a WPF application using C#.  So i followed the tutorial step by step. Basically there are only 2 windows in this applicaiton.

Window1.xaml and Window1.xaml.cs
Window2.xaml and Window2.xaml.cs

So my problem is in the Window2.xaml.cs

When I compile I get 2 errors

1) the first error is with my dataset declaration
ds = new DataSet();  

2) the second error is with System.Drawing
I get the errors shown on the image below.

I have attached my code, am I missing something? maybe i have been staring at the screen too long.   Has anyone ever got these 2 errors? do you know where I went wrong?

Thanks for any help.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Configuration;

namespace WpfSqlServerImages1
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            // open a database connection
            string connstr = @"Data Source=MyServer\SQLEXPRESS;Initial Catalog=TestImage;Integrated Security=True";
            SqlConnection sqlCon = new SqlConnection(connstr);

            // open the connection
            sqlCon.Open();

            // set up a data adapter object                    
            // Create a SqlDataAdapter
            SqlDataAdapter sqa = new SqlDataAdapter("select name from picture", sqlCon);

            // load a data set
            ds = new DataSet();

            sqa.Fill(ds);

            sqlCon.Close();

            foreach (DataRow dataRow in ds.Tables[0].Rows)
            {
                ListBoxItem lbItem = new ListBoxItem();
                lbItem.Content = dataRow[0].ToString();
                lbpics.Items.Add(lbItem);
            }

            InitializeComponent();
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBoxItem lb = (lbpics.SelectedItem as ListBoxItem);
            string connstr = @"Data Source=MyServer\SQLEXPRESS;Initial Catalog=TestImage;Integrated Security=True";
            SqlConnection sqlCon = new SqlConnection(connstr);
            sqlCon.Open(); 
            ds = new DataSet(); 
            SqlDataAdapter sqa = new SqlDataAdapter("Select pic from picture where name='" + lb.Content.ToString() + "'", sqlCon); 
            sqa.Fill(ds); 
            
            sqlCon.Close();


            byte[] data = (byte[])ds.Tables[0].Rows[0][0]; 
            MemoryStream strm = new MemoryStream(); 
            strm.Write(data, 0, data.Length); 
            strm.Position = 0; 
            System.Drawing.Image img = System.Drawing.Image.FromStream(strm); 
            BitmapImage bi = new BitmapImage(); 
            bi.BeginInit(); MemoryStream ms = new MemoryStream(); 
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); 
            ms.Seek(0, SeekOrigin.Begin); 
            bi.StreamSource = ms; 
            bi.EndInit(); 
            imagebox.Source = bi;

        }
    }
}

Open in new window

dserror.gif
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AshokSr. Software Engineer

Commented:
DataSet  ds = new DataSet();

For Systems.Drawing.Imaging,
try
System.Windows.Media.Imaging....

HTH
Ashok

AshokSr. Software Engineer

Commented:
DataSet  ds = new DataSet();

For Systems.Drawing.Imaging,

Goto Solution Explorer, in References, Right-Click Add References, from Dialog (default tab is .NET), select System.Drawing
and ADD it to References list


HTH
Ashok

Author

Commented:
Ashok111,
I added the System.Drawing reference like you said and that fixed that problem

So now basically i have only 1 error when i compile.

in the part of my code where i declare my dataset i used to have it like this:

 ds = new DataSet();

but per your suggestion i changed it to this:

Dataset ds = new DataSet();

but now, word Datatset is underlined in red and the error it gives it says
"The type or namespace 'Dataset' could not be found ( are you missing a using directive or an assembly reference)"

the error is shown in the image below and also below is my latest code after i made the changes you suggested
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Configuration;

namespace WpfSqlServerImages1
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            // open a database connection
            string connstr = @"Data Source=A69380ISB\SQLEXPRESS;Initial Catalog=TestImage;Integrated Security=True";
            SqlConnection sqlCon = new SqlConnection(connstr);

            // open the connection
            sqlCon.Open();

            // set up a data adapter object                    
            // Create a SqlDataAdapter
            SqlDataAdapter sqa = new SqlDataAdapter("select name from picture", sqlCon);

            // load a data set
            Dataset ds = new DataSet();

            sqa.Fill(ds);

            sqlCon.Close();

            foreach (DataRow dataRow in ds.Tables[0].Rows)
            {
                ListBoxItem lbItem = new ListBoxItem();
                lbItem.Content = dataRow[0].ToString();
                lbpics.Items.Add(lbItem);
            }

            InitializeComponent();
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBoxItem lb = (lbpics.SelectedItem as ListBoxItem);
            string connstr = @"Data Source=MyServer\SQLEXPRESS;Initial Catalog=TestImage;Integrated Security=True";
            SqlConnection sqlCon = new SqlConnection(connstr);
            sqlCon.Open(); 
            Dataset ds = new DataSet(); 
            SqlDataAdapter sqa = new SqlDataAdapter("Select pic from picture where name='" + lb.Content.ToString() + "'", sqlCon); 
            sqa.Fill(ds); 
            
            sqlCon.Close();

            byte[] data = (byte[])ds.Tables[0].Rows[0][0]; 
            MemoryStream strm = new MemoryStream(); 
            strm.Write(data, 0, data.Length); 
            strm.Position = 0; 
            System.Drawing.Image img = System.Drawing.Image.FromStream(strm); 
            BitmapImage bi = new BitmapImage(); 
            bi.BeginInit(); MemoryStream ms = new MemoryStream(); 
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); 
            ms.Seek(0, SeekOrigin.Begin); 
            bi.StreamSource = ms; 
            bi.EndInit(); 
            imagebox.Source = bi;

        }
    }
}

Open in new window

error1.gif

Author

Commented:
Dataset ds = new DataSet();

I tried to fix the error that was showing on the above dataset declaration by

adding

using System.Data.Dataset;

that fixes the error on the Dataset declaration but now it show an error on this  line.

Any ideas of this dilemna or how i can fix it?

Thanks.


dataseterror.gif
Sr. Software Engineer
Commented:
BTW, you are catching up!

You have to do two things

1)  In References, Add "System.Data"

2) At the top of the file, add "using System.Data;"

Now
DataSet d = new DataSet();
will be recognized.

HTH
Ashok

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial