Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8481329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:32:07+00:00 2026-06-10T19:32:07+00:00

There are some rows in my dataGrid, when i select one of them and

  • 0

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 image of selected row, this snippet has error on cmd.CommandText …,
How can i improve 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();
            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.Connection = con;
            con.Open();

            cmd.CommandText = "Select Picture from Personnels where Name= " + grdPersonnel1.SelectedItem;

            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");
        }

<Image VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Fill" Name="PictureBox"
                   Source="{Binding Picture}" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" Opacity="2">
</Image>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:32:08+00:00Added an answer on June 10, 2026 at 7:32 pm

    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");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I select some data rows from mssql2005 database and bind them to datagrid,
Is there a way in spark datagrid to disable some rows programmatically, in flex
There is a datagrid with n number of rows, the first column in the
Comment rows are counted in the NR. Is there some flag to ignore comments?
Is there some regex that can be passed to re-seq such that it will
I have a flex datagrid with 3 columns. The first column contains the image
This one is driving me nuts. The issue only shows up when the datagrid
I have a DataGrid with hundreds of rows and two columns of data. I
I have a datagrid to which I bind some items. I only allow selecting
I want to protect some rows of a DataGrid to be protected from deletion

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.