Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET SQL-Server WPF printing , +
There are some rows in my dataGrid, when i select one of them and print it's data, only first image of database will print while i only need to print selected row image.
 
<Image VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Fill" Name="PictureBox"
                       Source="{Binding Picture}" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" Opacity="2">
	</Image>
 
private void Print_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                
                DrawingVisual dv = new DrawingVisual();
                var dc = dv.RenderOpen();
              
                SqlConnection con = new SqlConnection();
                con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Database\Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                SqlCommand cmd = new SqlCommand();
                BitmapImage bmp = new BitmapImage();
                cmd.CommandText = "SELECT Picture FROM DataBase";
                cmd.Connection = con;
                con.Open();
                bmp.CacheOption = BitmapCacheOption.OnLoad;
                bmp.BeginInit();
                bmp.StreamSource = new System.IO.MemoryStream((Byte[])cmd.ExecuteScalar());
                bmp.EndInit();
                dc.DrawImage(bmp, new Rect(140, 170, 150, 150));
-
 
                dc.DrawText(new FormattedText("Name:", CultureInfo.GetCultureInfo("en-us"), FlowDirection,
                     new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Bold,
                         FontStretches.Normal), 12, System.Windows.Media.Brushes.Black), new System.Windows.Point(700, 180));
                dc.DrawText(new FormattedText(txtName.Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection,
                      new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Normal,
                          FontStretches.Normal), 11, System.Windows.Media.Brushes.Black), new System.Windows.Point(550, 180));
 
                dc.Close();             
                printDialog.PrintVisual(dv, "Print");
            }
		}
Posted 15-Aug-12 7:00am
Edited 15-Aug-12 7:01am
v2
Comments
Wes Aday at 15-Aug-12 12:04pm
   
You are not selecting your image based on any kind of identifier.
Mohammad Hossein Shojaei at 15-Aug-12 12:07pm
   
What type of identifier?
Wes Aday at 15-Aug-12 12:12pm
   
Any kind. Name, number, index. Whatever you are using in your database.
Mohammad Hossein Shojaei at 15-Aug-12 12:15pm
   
If I use Name or ID, have you guidance or link?
Wes Aday at 15-Aug-12 12:17pm
   
http://www.w3schools.com/sql/sql_where.asp
Mohammad Hossein Shojaei at 15-Aug-12 12:28pm
   
I used this:
cmd.CommandText = "SELECT Picture FROM Database Where Name=@Name";
 
But error: Must declare the scalar variable "@Name".
Wes Aday at 15-Aug-12 12:32pm
   
"SELECT Picture FROM Database Where Name='" + whereeveritisthatNameiscomingfrom + "'" and you might want to do some research on parameterized queries.
Mohammad Hossein Shojaei at 15-Aug-12 12:44pm
   
I found this:
...= "SELECT Picture FROM DataBase" + " where Name=" + (MyClass(grdPersonnel.SelectedItem)).Name;
 
but it's don't work!
Wes Aday at 15-Aug-12 12:45pm
   
You have to use single quotes for text items in SQL
Kenneth Haugland at 15-Aug-12 12:18pm
   
When I read the title it though ok, but when I saw the code I was lost... What are you trying to achive?
Mohammad Hossein Shojaei at 15-Aug-12 13:49pm
   
As i said: When i select one of DataGrid rows and print it's data, only first image of database will print while i only need to print selected DataGrid row image.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I solved it.
 
private void Print_Click(object sender, RoutedEventArgs e)
{
    System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
    if (printDialog.ShowDialog() == true)
    {                
        DrawingVisual dv = new DrawingVisual();
        var dc = dv.RenderOpen();
 
         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Database\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            SqlCommand cmd = new SqlCommand();
 
            BitmapImage bmp = new BitmapImage();
            cmd.CommandText = "Select Picture from Database where Code=@Code";
            cmd.Parameters.Add("@Code", SqlDbType.NVarChar, 30);
            cmd.Parameters["@Code"].Value = this.txtCode.Text;
            cmd.Connection = con;
            con.Open();
 
            bmp.CacheOption = BitmapCacheOption.OnLoad;
            bmp.BeginInit();
            bmp.StreamSource = new System.IO.MemoryStream((Byte[])cmd.ExecuteScalar());
            bmp.EndInit();
            dc.DrawImage(bmp, new Rect(140, 170, 150, 150));
 
        dc.DrawText(new FormattedText("Name:", CultureInfo.GetCultureInfo("en-us"), FlowDirection,
             new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Bold,
                 FontStretches.Normal), 12, System.Windows.Media.Brushes.Black), new System.Windows.Point(700, 180));
        dc.DrawText(new FormattedText(txtName.Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection,
              new Typeface(new System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Normal,
                  FontStretches.Normal), 11, System.Windows.Media.Brushes.Black), new System.Windows.Point(550, 180));
 

        dc.Close();
 
        printDialog.PrintVisual(dv, "Print");
    }
  Permalink  
Comments
ArianDiba at 19-Aug-12 11:16am
   
This was good snippet for me,
thanks

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 Maciej Los 359
2 Kornfeld Eliyahu Peter 309
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100