Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There is a DataGrid with bunch of images in my wpf application.
I have to select an image from my DataGrid and insert that image into mysql DB.
My code is not working properly. I need help. My code is below.
C#
private void btnImage_Click(object sender, RoutedEventArgs e)
        {            
            {
		string constr = "Data Source=localhost;port=3306;Initial Catalog=MyCollege;User Id=root;password=2525";
                MySqlConnection con = new MySqlConnection(constr);
                MySqlCommand cmd = new MySqlCommand("INSERT INTO images(Image) VALUES ('" + dataGrid1.SelectedValue + "')", con);
                con.Open();
                int s1 = cmd.ExecuteNonQuery();
                if (s1 > 0)
                {
                    lbl1.Content = "Image inserted succesfully";
                }
                con.Close();
            }
        }
Posted
Updated 18-Oct-15 20:11pm
v3
Comments
VR Karthikeyan 19-Oct-15 0:43am    
Cast the SelectedValue to BitmapImage and Convert it into bytes array and save it in database.
abdujalilc 19-Oct-15 1:45am    
Example code would be much appreciated if possible. As I am new on wpf. Thanks.
VR Karthikeyan 19-Oct-15 2:28am    
How you are assign Images to DataGrid? by using DataTable or? Show the code pls.
abdujalilc 19-Oct-15 4:13am    
private void BindGrid1()
{
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand("SELECT Image FROM images)", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
}
VR Karthikeyan 19-Oct-15 5:54am    
What is the DataType you have used for Image column in images Table?

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