EDN Admin
Well-known member
i m creating image save in sql2005 with C# and my image column in image format in sql.
when i click display image button then i got a error....
<br/>
<br/>
<br/>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
<br/>
namespace TowerTroops
{
public partial class Form1 : Form
{
Image curImage;
string curFileName;
string connectionString = "Data Source=sonumonu; Initial Catalog=tt; user id=sa; password=123";
<br/>
string savedImageName="";
public Form1()
{
InitializeComponent();
}
<br/>
private void panel1_Paint(object sender, PaintEventArgs e)
{
<br/>
}
<br/>
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the ttDataSet.EmpDetails table. You can move, or remove it, as needed.
this.empDetailsTableAdapter.Fill(this.ttDataSet.EmpDetails);
}
<br/>
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=sonumonu; Initial Catalog=tt; User ID=sa; Password=123";
con.Open();
try
{
FileStream file = new FileStream(curFileName,FileMode.OpenOrCreate,FileAccess.Read);
byte[] rawdata = new byte[file.Length];
file.Read(rawdata, 0, System.Convert.ToInt32(file.Length));
file.Close();
string str = txtName.Text;
string str1 = txtJobStartDate.Text;
string str2 = txtmobNo.Text;
string str3 = txtPerAdd.Text;
string str4 = txtSalary.Text;
string str5 = txtState.Text;
string str6 = txtPerMob.Text;
string str7 = txtOtherInfo.Text;
string str8 = txtEmail.Text;
//string str9 = dateTimePicker1.Text;
<br/>
string str9 = txtBirthDate.Text;
string str10 = txtAddress.Text;
string str11 = comboBoxEducation.SelectedItem.ToString();
string str12 = comboBoxGender.SelectedItem.ToString();
string str13 = comboBoxMaritalStatus.SelectedItem.ToString();
SqlCommand cmd = new SqlCommand("Insert into EmpDetails ([Name],[Address],[Alternet Phone],[E-mail Address],[Birth Date],State,Salary,[Job Start Date],Gender,[Emergency Address],[Other Info],[Emergency
Phone],[Marital Status],Education,[Employee Picture]) values (" + str + "," + str10 + "," + str2 + "," + str8 + "," + str9 + "," + str5 + "," + str4 + "," + str1 + "," + str12 + "," + str3 + "," + str7 + "," + str6 + "," + str13 +
"," + str11 + "," + rawdata + ")", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Data added Sucesfully");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + "Please Insert Values");
}
<br/>
}
<br/>
private void comboBoxGender_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBoxGender.SelectedItem == "Male")
{
EmployeepictureBox1.Image = Properties.Resources.avatar_male;
}
else
{
EmployeepictureBox1.Image = Properties.Resources.avatar_female;
}
}
<br/>
private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=sonumonu; Initial Catalog=tt; User ID=sa; Password=123";
con.Open();
try
{
string str0 = txtEmpID.Text;
string str = txtName.Text;
string str1 = txtJobStartDate.Text;
string str2 = txtmobNo.Text;
string str3 = txtPerAdd.Text;
string str4 = txtSalary.Text;
string str5 = txtState.Text;
string str6 = txtPerMob.Text;
string str7 = txtOtherInfo.Text;
string str8 = txtEmail.Text;
//string str9 = dateTimePicker1.Text;
<br/>
string str9 = txtBirthDate.Text;
string str10 = txtAddress.Text;
string str11 = comboBoxEducation.SelectedText;
string str12 = comboBoxGender.SelectedText;
string str13 = comboBoxMaritalStatus.SelectedText;
SqlCommand cmd = new SqlCommand("delete from EmpDetails where EmpID=" + str0 + "", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Delete Sucesfully");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
<br/>
private void linklblPhoto_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
if (openDlg.ShowDialog() == DialogResult.OK)
{
curFileName = openDlg.FileName;
txtOtherInfo.Text = curFileName;
}
}
<br/>
private void btnDisplayImg_Click(object sender, EventArgs e)
{
//string sql = "select [Employee Picture] from EmpDetails where EmpID=11018";
<br/>
string sql = "select [Employee Picture] from EmpDetails where EmpID=" + txtEmpID.Text + "";
SqlConnection con = new SqlConnection();
con.ConnectionString = connectionString;
con.Open();
FileStream file;
BinaryWriter bw;
int bufferSize = 100;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;
savedImageName = txtOtherInfo.Text;
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while (myReader.Read())
{
file = new FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(file);
startIndex = 0;
retval = myReader.GetBytes(0, startIndex, outbyte,0,bufferSize);
while (retval == bufferSize)
{
bw.Write(outbyte);
bw.Flush();
startIndex += bufferSize;
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
}
bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
file.Close();
}
//con.Close();
<span style="text-decoration:underline curImage = Image.FromFile(savedImageName); ----here is error
EmployeepictureBox1.Image = curImage;
EmployeepictureBox1.Invalidate();
con.Close();
<br/>
}
<br/>
}
}
View the full article
when i click display image button then i got a error....
<br/>
<br/>
<br/>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
<br/>
namespace TowerTroops
{
public partial class Form1 : Form
{
Image curImage;
string curFileName;
string connectionString = "Data Source=sonumonu; Initial Catalog=tt; user id=sa; password=123";
<br/>
string savedImageName="";
public Form1()
{
InitializeComponent();
}
<br/>
private void panel1_Paint(object sender, PaintEventArgs e)
{
<br/>
}
<br/>
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the ttDataSet.EmpDetails table. You can move, or remove it, as needed.
this.empDetailsTableAdapter.Fill(this.ttDataSet.EmpDetails);
}
<br/>
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=sonumonu; Initial Catalog=tt; User ID=sa; Password=123";
con.Open();
try
{
FileStream file = new FileStream(curFileName,FileMode.OpenOrCreate,FileAccess.Read);
byte[] rawdata = new byte[file.Length];
file.Read(rawdata, 0, System.Convert.ToInt32(file.Length));
file.Close();
string str = txtName.Text;
string str1 = txtJobStartDate.Text;
string str2 = txtmobNo.Text;
string str3 = txtPerAdd.Text;
string str4 = txtSalary.Text;
string str5 = txtState.Text;
string str6 = txtPerMob.Text;
string str7 = txtOtherInfo.Text;
string str8 = txtEmail.Text;
//string str9 = dateTimePicker1.Text;
<br/>
string str9 = txtBirthDate.Text;
string str10 = txtAddress.Text;
string str11 = comboBoxEducation.SelectedItem.ToString();
string str12 = comboBoxGender.SelectedItem.ToString();
string str13 = comboBoxMaritalStatus.SelectedItem.ToString();
SqlCommand cmd = new SqlCommand("Insert into EmpDetails ([Name],[Address],[Alternet Phone],[E-mail Address],[Birth Date],State,Salary,[Job Start Date],Gender,[Emergency Address],[Other Info],[Emergency
Phone],[Marital Status],Education,[Employee Picture]) values (" + str + "," + str10 + "," + str2 + "," + str8 + "," + str9 + "," + str5 + "," + str4 + "," + str1 + "," + str12 + "," + str3 + "," + str7 + "," + str6 + "," + str13 +
"," + str11 + "," + rawdata + ")", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Data added Sucesfully");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + "Please Insert Values");
}
<br/>
}
<br/>
private void comboBoxGender_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBoxGender.SelectedItem == "Male")
{
EmployeepictureBox1.Image = Properties.Resources.avatar_male;
}
else
{
EmployeepictureBox1.Image = Properties.Resources.avatar_female;
}
}
<br/>
private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=sonumonu; Initial Catalog=tt; User ID=sa; Password=123";
con.Open();
try
{
string str0 = txtEmpID.Text;
string str = txtName.Text;
string str1 = txtJobStartDate.Text;
string str2 = txtmobNo.Text;
string str3 = txtPerAdd.Text;
string str4 = txtSalary.Text;
string str5 = txtState.Text;
string str6 = txtPerMob.Text;
string str7 = txtOtherInfo.Text;
string str8 = txtEmail.Text;
//string str9 = dateTimePicker1.Text;
<br/>
string str9 = txtBirthDate.Text;
string str10 = txtAddress.Text;
string str11 = comboBoxEducation.SelectedText;
string str12 = comboBoxGender.SelectedText;
string str13 = comboBoxMaritalStatus.SelectedText;
SqlCommand cmd = new SqlCommand("delete from EmpDetails where EmpID=" + str0 + "", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Delete Sucesfully");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
<br/>
private void linklblPhoto_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
if (openDlg.ShowDialog() == DialogResult.OK)
{
curFileName = openDlg.FileName;
txtOtherInfo.Text = curFileName;
}
}
<br/>
private void btnDisplayImg_Click(object sender, EventArgs e)
{
//string sql = "select [Employee Picture] from EmpDetails where EmpID=11018";
<br/>
string sql = "select [Employee Picture] from EmpDetails where EmpID=" + txtEmpID.Text + "";
SqlConnection con = new SqlConnection();
con.ConnectionString = connectionString;
con.Open();
FileStream file;
BinaryWriter bw;
int bufferSize = 100;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;
savedImageName = txtOtherInfo.Text;
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while (myReader.Read())
{
file = new FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(file);
startIndex = 0;
retval = myReader.GetBytes(0, startIndex, outbyte,0,bufferSize);
while (retval == bufferSize)
{
bw.Write(outbyte);
bw.Flush();
startIndex += bufferSize;
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
}
bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
file.Close();
}
//con.Close();
<span style="text-decoration:underline curImage = Image.FromFile(savedImageName); ----here is error
EmployeepictureBox1.Image = curImage;
EmployeepictureBox1.Invalidate();
con.Close();
<br/>
}
<br/>
}
}
View the full article