Amit

Amit

  • 1.4k
  • 212
  • 33.5k

Error when i update image on sqlserver database.

Jan 5 2015 5:21 AM
hi,
 
i have create windows form in c# and add three field textbox for employee name ,picturebox for image and save btn for save data and modify btn for modify and btn for save data. 
Error:->No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type. 
  
 
public partial class Ex : Form
{
public string connectionString=@"";
SqlConnection con = new SqlConnection(@"");
SqlCommand cmd;
string photofilename;
SqlDataReader dr;
public Ex()
{
InitializeComponent();
}
private void Ex_Load(object sender, EventArgs e)
{
this.imageTableAdapter.Connection.ConnectionString = connectionString;
// TODO: This line of code loads data into the 'dataSet1.Image' table. You can move, or remove it, as needed.
this.imageTableAdapter.Fill(this.dataSet1.Image);
}
private void button2_Click(object sender, EventArgs e)
{
byte[] image = null;
FileStream fstream = new FileStream(photofilename,FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
image = br.ReadBytes((int)fstream.Length);
con.Open();
string str = "insert into image(emp_name,emp_image)values('"+textBox1.Text+"',@emp_image)";
cmd = new SqlCommand(str,con);
cmd.Parameters.AddWithValue("@emp_name",textBox1.Text);
cmd.Parameters.AddWithValue("@emp_image", image);
int n = cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("record inserted");
}
else
MessageBox.Show("insertion failed");
}
private void button1_Click(object sender, EventArgs e)
{
FileDialog fldlg = new OpenFileDialog();
photofilename = openFileDialog1.FileName;
fldlg.Filter = "Image File (*.jpg)|*.jpg";
if (fldlg.ShowDialog() == DialogResult.OK)
{
photofilename = fldlg.FileName.ToString();
pictureBox1.Image = Image.FromFile(photofilename);
}
fldlg = null;
}
private void button3_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("Update Image set emp_name=@emp_name,emp_image=@emp_image where emp_name=@emp_name",con);
cmd.Parameters.AddWithValue("@emp_image",textBox1.Text);
cmd.Parameters.AddWithValue("@emp_image",pictureBox1.Image);
try{
dr=cmd.ExecuteReader();
while(dr.Read())
{
byte[] imgg=(byte[])(dr["emp_image"]);
if(imgg==null)
{
pictureBox1.Image=null;
}else
{
MemoryStream mstream=new MemoryStream(imgg);
pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
}
}
}catch(Exception){
}
int i=cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
MessageBox.Show("Record has been Updated successfully");
}
else
{
MessageBox.Show("Record has not been updated due to some problem");
}
}
}
}
 
I have written this code for insert and update name and image .but