Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I want to add photo to sql by wpf and every time I select an employee name it's photo retrieve from sql and see it on image control. I can load photo to image control. I need a code to save photo to sql database along with Name and Family,
and when Name or Family was selected, photo retrieve from sql.

XML
<Datagrid>
	<DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=No}" Header="No" Width="Auto"></DataGridTextColumn>
        <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="Auto"></DataGridTextColumn>
        <DataGridTextColumn Binding="{Binding Path=Family}" Header="Family" Width="Auto"></DataGridTextColumn>
        <DataGridTextColumn Binding="{Binding Path=Photo}" Header="Photo" Width="Auto"></DataGridTextColumn>
	</DataGrid.Columns>
</DataGrid>
			
<TextBox Grid.Column="1" Grid.Row="0" Height="25" Name="txtNo" VerticalAlignment="Center" Text="{Binding Path=No}" Margin="3" removed="#FFC8EAF0" FontWeight="Bold" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" FontFamily="Arial" FontSize="11" Padding="0" VerticalContentAlignment="Center"></TextBox>
			
<TextBox Grid.Column="1" Grid.Row="1" Height="25" Name="txtName" VerticalAlignment="Center" Text="{Binding Path=Name}" Margin="3" removed="#FFC8EAF0" FontWeight="Bold" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" FontFamily="Arial" FontSize="11" Padding="0" VerticalContentAlignment="Center"></TextBox>
				
<TextBox Grid.Column="1" Grid.Row="2" Height="25" Name="txtFamily" VerticalAlignment="Center" Text="{Binding Path=Family}" Margin="3" removed="#FFC8EAF0" FontWeight="Bold" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}" FontFamily="Arial" FontSize="11" Padding="0" VerticalContentAlignment="Center"></TextBox>
				
<Image Grid.Column="6" Margin="49,6,44,0" Grid.RowSpan="7" Name="Photo" DataContext="{Binding Path=SelectedItem, ElementName=grdPersonnel1}"></Image>
<Button Grid.Column="6" Margin="88,4,80,26" Grid.Row="7" Grid.RowSpan="2" Name="btnAddphoto" Click="btnAddphoto_Click">Add photo</Button>


C#
private void btnAddphoto_Click(object sender, RoutedEventArgs e)
       {

           Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
           dlg.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.* ";
           if (dlg.ShowDialog() == true)
           {
               System.IO.Stream stream = System.IO.File.Open(dlg.FileName, System.IO.FileMode.Open);

               BitmapImage imgsrc = new BitmapImage();
               imgsrc.BeginInit();
               imgsrc.StreamSource = stream;
               imgsrc.EndInit();
               this.Photo.Source = imgsrc;

       }


SQL
([No] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](40) NULL,
	[Family] [nvarchar](50) NULL,
	[Photo] [varbinary] (max) NULL)
Posted
Updated 16-Jun-12 4:43am
v2
Comments
db7uk 15-Jun-12 14:19pm    
Hi. I am not following the question. What is the problem? Your title stated you wanted to retrieve an image from sql and be used in a wpf application. I don't quite follow what you are trying to achieve with the code and question you have asked. Please re-phrase by "improve question".

1 solution

Here is a link that shows you how to save and retrieve images from Microsoft SQL:

http://www.redmondpie.com/how-to-save-and-retrieve-images-in-c-wpf-application-from-sql-server-database/[^]

Following this article should solve your issues. You might also want to consider saving your images to a central location and then just saving the image path to the database. That will reduce how large your database gets and yet it will still work well. Just a thought.
 
Share this answer
 
Comments
M.H. Shojaei 19-Jun-12 7:16am    
By selecting DataGrid row, person photo must be retrieve from sql and see it on image control.
this is my question. I do not need to ListBox.

Thanks
Tim Corey 19-Jun-12 8:16am    
Understood. However, the code in the example I gave shows you how to save and retrieve images in SQL. You will obviously need to change it for your environment and needs. That isn't something I can do for you. Instead, I'm pointing you in the right direction. You have all the pieces you need. Now you just need to put them together. Set up an event on your DataGrid so that when a row is clicked on, it uses that data to look up that row in the database and retrieve the image.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900