locked
Selecting dataGrid row and print row data RRS feed

  • Question

  • 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 image.

      <Image Source="{Binding Path=Picture}" Width="200" Height="150" HorizontalAlignment="Center" VerticalAlignment="Top" >
      </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");
                }
    		}



    • Moved by Sheldon _Xiao Friday, August 17, 2012 3:22 AM (From:Windows Presentation Foundation (WPF))
    Wednesday, August 15, 2012 12:42 AM

Answers

  •                 cmd.CommandText = "SELECT Picture FROM DataBase";

     

    Is that your actual CommandText? "SELECT Picture FROM DataBase" ?

    If so, aren't you missing a "where" clause?

    You said ExecuteScalar, which in the case of multiple rows "returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored." - Correct, but only the first if multiple rows are returned.

    It is therefore always picking the first image.

     

    So I guess you need to add a where clause?

    Something like...

    ...= "SELECT Picture FROM DataBase" + " where MyId=" + (MyClass(MyListBox.SelectedItem)).MyId

    Regards,
    Pete


    #PEJL



    • Edited by Pete LakerMVP Wednesday, August 15, 2012 12:59 AM
    • Proposed as answer by Srithar Thursday, August 16, 2012 4:44 AM
    • Marked as answer by mhshojaee Friday, August 17, 2012 6:02 PM
    Wednesday, August 15, 2012 12:56 AM
  • I used this :

    cmd.CommandText = "SELECT Picture FROM DataBase" + "Where Name=" + (???(grdPerson.SelectedItem)).???;
    I do not know what consider for ??? !

    I will try to break this down for you in one long ramble, I hope that you these and all the other contributer's replies at least helpful (hint hint)

    Each line of your listbox is a representation of a class from the collection that is bound to Listbox1.ItemsSource.

    That class has a bunch of properties, "Name" and "Picture" being two of them.

    The missing piece of information here is WHAT is the name of the CLASS that these properties belong to?

    public MyClass
    {
        public string Name {get; set;}
        public string Picture {get; set;}    
    }

    In this case MyClass is the class.

    In my example I use the ListBox.SelectedItem. This is of type "object", so we can't get at the item's properties.

    We therefore have to cast it to the class we know it as:

    (MyClass(grdPerson.SelectedItem))

    This is now the selected item converted into it's known class, so we can get the properties, like...

    (MyClass(grdPerson.SelectedItem)).Name

    However, looking again at your sql, it is still very wrong....

    cmd.CommandText = "Select Picture From Database Where Name='" + grdPersonnel1.SelectedIndex + "'";

     

    You do not want to use SelectedIndex, as this is an integer representing the selected row of a listbox, nothing to do with the selected item.

    Also, I'm sure your pictures database table isn't called "Database", it's probably called "Pictures" or something.

    Also, the database is usually connected to in the connection statement, so you just have to specify the table "from Pictures", not even "from Database.dbo.Pictures"

    Furthermore, database indexing on "picture name" would not be wise, I assume you have a PictureId column?

    That would presumably be another property of the class...

    public MyClass
    {
        public int PictureId {get; set;}
        public string Name {get; set;}
        public string Picture {get; set;}    
    }

    It may not be used in the control template, but it should be a populated property of the class, so when you get SelectedItem, you can use that property to index the correct image from the Pictures table.

    So now you should understand better my original reply, which was hopefully correct:

    cmd.CommandText = "SELECT Picture FROM Pictures Where PictureId=" + (MyClass(grdPerson.SelectedItem)).PictureId;

     

    In fact, looking back, everyone was right, to all your questions around this.
    Please give everyone some Friday love and "Vote as helpful/mark as answer", as they all are in my opinion ;)
     

    Best regards,
    Pete


    #PEJL


    • Edited by Pete LakerMVP Friday, August 17, 2012 4:40 PM
    • Marked as answer by mhshojaee Friday, August 17, 2012 6:01 PM
    Friday, August 17, 2012 4:39 PM

All replies

  •                 cmd.CommandText = "SELECT Picture FROM DataBase";

     

    Is that your actual CommandText? "SELECT Picture FROM DataBase" ?

    If so, aren't you missing a "where" clause?

    You said ExecuteScalar, which in the case of multiple rows "returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored." - Correct, but only the first if multiple rows are returned.

    It is therefore always picking the first image.

     

    So I guess you need to add a where clause?

    Something like...

    ...= "SELECT Picture FROM DataBase" + " where MyId=" + (MyClass(MyListBox.SelectedItem)).MyId

    Regards,
    Pete


    #PEJL



    • Edited by Pete LakerMVP Wednesday, August 15, 2012 12:59 AM
    • Proposed as answer by Srithar Thursday, August 16, 2012 4:44 AM
    • Marked as answer by mhshojaee Friday, August 17, 2012 6:02 PM
    Wednesday, August 15, 2012 12:56 AM
  • I used this :

    cmd.CommandText = "SELECT Picture FROM DataBase" + "Where Name=" + (???(grdPerson.SelectedItem)).???;
    I do not know what consider for ??? !

    Wednesday, August 15, 2012 7:13 PM
  • Hi mhshojaee,

    Pete was right. I saw in you code that you have mentioned the cmd.commandText as "select picture from database" which retrieves all the images from your database table. you can't display all the images in a single Image Control.

    So what you need to change was you need to include a where (condition) in your cmd.commandText. 

    For example let us say there are 10 images in your database and you have 10 items in your listbox. then you could use the selected index value of Listbox in your where condition provided you have a separate column in your sql table for imageid. Like this

    ImageID    Picture 

      1             (BinaryData)

      2             (BinaryData)

      3             (BinaryData)

    If you had the Table structure like this you can simply use in the where condition like this,

    "SELECT Picture FROM DataBase" + " where ImageId=" + Listbox1.SelectedIndex;
    Happy Programming!!!

    Thursday, August 16, 2012 4:43 AM
  • Thanks, I did it:

    cmd.CommandText = "Select Picture from Database" + " where Name=" + grdPersonnel1.SelectedIndex;

    but there is error: Conversion failed when converting the nvarchar value '' to data type int.

    or,:

    cmd.CommandText = "Select Picture From Database Where Name='" + grdPersonnel1.SelectedIndex + "'";

    this error: Buffer cannot be null. Parameter name: buffer

    I'm new in C#, it's some ambiguous.

    Thursday, August 16, 2012 4:14 PM
  • Hi mhshojaee,

    What's the table structure?

    What's the type of the Name column?

    Are you sure that the value in Name column will equal to the SelectedIndex?

    I suggest you using SqlCommand.Parameters Property in you command and set the right SqlDbType in the SqlParameter object.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, August 17, 2012 7:15 AM
  • I used this :

    cmd.CommandText = "SELECT Picture FROM DataBase" + "Where Name=" + (???(grdPerson.SelectedItem)).???;
    I do not know what consider for ??? !

    I will try to break this down for you in one long ramble, I hope that you these and all the other contributer's replies at least helpful (hint hint)

    Each line of your listbox is a representation of a class from the collection that is bound to Listbox1.ItemsSource.

    That class has a bunch of properties, "Name" and "Picture" being two of them.

    The missing piece of information here is WHAT is the name of the CLASS that these properties belong to?

    public MyClass
    {
        public string Name {get; set;}
        public string Picture {get; set;}    
    }

    In this case MyClass is the class.

    In my example I use the ListBox.SelectedItem. This is of type "object", so we can't get at the item's properties.

    We therefore have to cast it to the class we know it as:

    (MyClass(grdPerson.SelectedItem))

    This is now the selected item converted into it's known class, so we can get the properties, like...

    (MyClass(grdPerson.SelectedItem)).Name

    However, looking again at your sql, it is still very wrong....

    cmd.CommandText = "Select Picture From Database Where Name='" + grdPersonnel1.SelectedIndex + "'";

     

    You do not want to use SelectedIndex, as this is an integer representing the selected row of a listbox, nothing to do with the selected item.

    Also, I'm sure your pictures database table isn't called "Database", it's probably called "Pictures" or something.

    Also, the database is usually connected to in the connection statement, so you just have to specify the table "from Pictures", not even "from Database.dbo.Pictures"

    Furthermore, database indexing on "picture name" would not be wise, I assume you have a PictureId column?

    That would presumably be another property of the class...

    public MyClass
    {
        public int PictureId {get; set;}
        public string Name {get; set;}
        public string Picture {get; set;}    
    }

    It may not be used in the control template, but it should be a populated property of the class, so when you get SelectedItem, you can use that property to index the correct image from the Pictures table.

    So now you should understand better my original reply, which was hopefully correct:

    cmd.CommandText = "SELECT Picture FROM Pictures Where PictureId=" + (MyClass(grdPerson.SelectedItem)).PictureId;

     

    In fact, looking back, everyone was right, to all your questions around this.
    Please give everyone some Friday love and "Vote as helpful/mark as answer", as they all are in my opinion ;)
     

    Best regards,
    Pete


    #PEJL


    • Edited by Pete LakerMVP Friday, August 17, 2012 4:40 PM
    • Marked as answer by mhshojaee Friday, August 17, 2012 6:01 PM
    Friday, August 17, 2012 4:39 PM