TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
abdujalil chuliev
NA
400
41.6k
double image insert mysql
Dec 29 2015 5:03 AM
I use WPF and C# in this project.
There is only an Insert button and a datagrid.
First I choose an image from datagrid,
then I press Insert button.
In my case when I press insert once, two images are inserted to my DB at the same time.
Where is the problem in my code?:
<Window x:Class="PupilReg.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Image Insert" Height="674" Width="1009" WindowStartupLocation="CenterScreen" FontSize="15" FontWeight="Bold" ResizeMode="CanResizeWithGrip">
<Grid Margin="0,10,-8,-50">
<DataGrid Name="dataGrid1" Height="565" Width="435" Margin="474,53,100,65">
<DataGrid.ItemBindingGroup>
<BindingGroup/>
</DataGrid.ItemBindingGroup>
<DataGrid.Columns>
<DataGridTextColumn Header="Image_ID" Binding="{Binding Path=Image_ID}" Width="80"/>
<DataGridTemplateColumn Header="Image_BLOB" Width="200" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=Image_BLOB}" Width="160" Height="160" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button x:Name="btnInsert" Content="Insert" HorizontalAlignment="Left" Margin="93,117,0,0" VerticalAlignment="Top" Width="75" Click="btnInsert_Click"/>
</Grid>
</Window>
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Windows;
namespace PupilReg
{
public partial class MainWindow : Window
{
string constr = "Data Source=localhost;port=3306;Initial Catalog=test;User Id=root;password=2525";
public MainWindow()
{
InitializeComponent();
BindGrid1();
}
public void BindGrid1()
{
MySqlConnection con = new MySqlConnection(constr);
con.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM images", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
}
private void btnInsert_Click(object sender, RoutedEventArgs e)
{
DataRowView SelectedRowValue = (DataRowView)dataGrid1.SelectedValue;
byte[] ImageBytes = (byte[])SelectedRowValue.Row.ItemArray[1];
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand("INSERT INTO images (Image_BLOB) VALUES (@ImageSource)", con);
cmd.Parameters.Add("@ImageSource", MySqlDbType.Blob, ImageBytes.Length).Value = ImageBytes;
con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
cmd.ExecuteNonQuery();
MessageBox.Show("Image Inserted");
}
con.Close();
BindGrid1();
}
}
}
Reply
Answers (
2
)
Increment values in gridview column
Windows forms