Hi.....
I need a help again...........
I have a one TextBox and two Buttons in my application. One is for Browse and other is for Upload.
I want to browse an image file using Browse button (then the path file will appear in the text box) and upload that file into database.
Then i want to load this stored button into PictureBox using another button called View.
I found below code for browsing.....please support me to do the rest.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Open File";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fdlg.FileName;
}
}
If u could please give me a hand to do this.
Thanks,
CharithMax
Loading
Kirtan PatelPosted Aug 14, 2009, 2:26 PM
Here i Have defined two Columns in Database "ID int and "photo" with data type var-binary in database
// Procedure How to Insert Image into Database
using System.Data.SqlClient;
using System.IO;
private void btnInsertImage_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
byte[] photo = File.ReadAllBytes(textBox1.Text);
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("insert into ImageData(photo) values(@photo)", con);
comm.Parameters.AddWithValue("@photo", photo);
comm.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data inserted");
}
}
// Read Image into PictureBox From database
private void btnReadImageData_Click(object sender, EventArgs e)
{
byte[] photo;
MemoryStream m = null;
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("select * from ImageData where id=1", con);
SqlDataReader reader = comm.ExecuteReader();
while ( reader.Read())
{
photo= (byte[])(reader["photo"]);
m = new MemoryStream(photo);
}
pictureBox1.Image = Image.FromStream(m);
}
Happy Coding :)
Charith LiyanagamagePosted Aug 23, 2009, 1:27 PM
please send the same solution for MS Access 2007 db. And please mention what is the data type that i should select when creating access data table.
Thanks.
Kirtan PatelPosted Aug 15, 2009, 7:12 AM
By using Index or using Column name
i n my code i called it with column name "photo" that is in my database
you can use both :)
Charith LiyanagamagePosted Aug 15, 2009, 7:03 AM
Ur code works well. But I got some Indexexception @ this point(Bold green coloured line) when retrieving the image.
while ( reader.Read())
{
// photo= (byte[])(reader["photo"]);
m = new MemoryStream(photo);
}
so I modified the code line as below
while (reader.Read())
{
//photo = (byte[])(reader["photo"]);
photo = (byte[])(reader[1]); -------Modified line
m = new MemoryStream(photo);
}
Now it works properly....
Anyway Thank you for ur big help.............
Charith LiyanagamagePosted Aug 15, 2009, 6:31 AM
Charith LiyanagamagePosted Aug 15, 2009, 6:29 AM
Master BillaPosted Aug 14, 2009, 11:38 PM
I think some more information i need provide here
1. your image store column database as "Image" DataType.
2. after you need read your image as byte, then you need send to db.
Save C# code
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection connection = null;
try
{
Byte[] imgByte = null;
if ( textBox1.Text!="")
{
byte[] imgByte = File.ReadAllBytes(textBox1.Text);
}
// Insert the employee name and image into db
string conn = ConfigurationManager.ConnectionStrings ["EmployeeConnString"].ConnectionString;
connection = new SqlConnection(conn);
connection.Open();
string sql = "INSERT INTO EmpDetails(empimg) VALUES(@eimg) SELECT @@IDENTITY";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@eimg", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
}
catch
{
lblResult.Text = "There was an error";
}
finally
{
connection.Close();
}
}
private void btnview_Click(object sender, EventArgs e){
byte[] imgeData;
MemoryStream datastream = null;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
con.Open();
using(SqlCommand comm = new SqlCommand("select ImageColumn from EmpDetails where ID='"+YourID+"'", con)
{
using(SqlDataReader reader = comm.ExecuteReader())
{
while ( reader.Read())
{
imgeData = (byte[])(reader["ImageColumn"]);
datastream = new MemoryStream(imgeData);
}
pictureBox1.Image = Image.FromStream(datastream );
}
}
}
}
if you have a y question ,please post here
Thank you