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;
}
}
}
Goto Solution Explorer, in References, Right-Click Add References, from Dialog (default tab is .NET), select System.Drawing
and ADD it to References list
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;
}
}
}
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.