By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,317 Members | 2,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,317 IT Pros & Developers. It's quick & easy.

Save and retrieve image in SQL Database using Data adapter commands with C#.

P: n/a
Hi all...

I searhed for a code to save and retrieve image from SQL database
using Data adapter but I didn't found anything.

Thanks,
Bassem.
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Bassem
if you get your data in a dataset using an Using an sqlDataAdapter , then
every thing will be returned as object . if you know that the field you
have in hand is an image then you can cast it to the appropriate type
These links give examples that would help
http://www.experts-exchange.com/Prog...ges/C_Sharp/Q_
20837398.html

http://forums.aspfree.com/t22808/s12...df909cb0d.html
http://www.csharpfr.com/gma/listbox+image

http://www.aspmessageboard.com/forum...p?M=666404&P=1
http://www.akadia.com/services/dotne...rite_blob.html
hope you find what you need in these examples

Nov 15 '05 #2

P: n/a


Thanks Mohamoss....
I read so much in this links and get more ideas.
I implement code to save photo and succeed in that.

The code I Implement in C#. [Using OleDbDataAdapter]

Global field to hold the path of the photo: GSPathImage
The function used to get the path of the photo:
private void EmployeePhoto_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Employee Photo";
ofd.Filter = "Bitmap (*.BMP;*.DIB;*.)|*.BMP;*.DIB|JPEG
(*.JPG;*.JPEG;*.JPE;*.JFIF)|*.JPG;*.JPEG;*.JPE;*.J FIF|All files
(*.*)|*.*" ;
ofd.ShowDialog();
pBEmpPhoto.Image = new Bitmap(ofd.FileName) ;
GSPathImage = ofd.FileName ;
}
GSPathImage = GSPathImage == null ? "" : GSPathImage ;
byte[] photo = GSPathImage == "" ? new byte[0]: GetPhoto(GSPathImage) ;

OleDbParameter oledbParam = new OleDbParameter("@ImagEmployee"
,OleDbType.VarBinary,photo.Length,ParameterDirecti on.Input,true,0,0,null
, DataRowVersion.Current ,photo);

oleDbConnection1.Open();

Example to add values for Employee and his photo:

oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO EmployeeSalary(EmployeeName , ImagEmployee )"+
"VALUES ('"+lsNewEmpName+"' , '@ImagEmployee' )";
oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();

oleDbConnection1.Close();

Till now everything going ok and no exception.

When retrieving the photo I get exception.
The code of the retrieving:

string EmployeeValue = txtBEmployeeName.Text.Trim();
string myOleDBstring = "SELECT EmployeeName , ImagEmployee FROM
EmployeeSalary WHERE EmployeeName = '"+ EmployeeValue +"'";
DataSet dsTest = new DataSet();
OleDbDataAdapter oledbTest = new OleDbDataAdapter(myOleDBstring
,oleDbConnection1);
oledbTest.Fill(dsTest ,"EmployeeSalary");
int testrec = dsTest.Tables["EmployeeSalary"].Rows.Count;

if(testrec > 0 )
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData =
(Byte[])(dsTest.Tables["EmployeeSalary"].Rows[testrec-1]["ImagEmployee"]
);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pBEmpPhoto.Image = Image.FromStream(stmBLOBData);
}
And the exception is:
System.ArpumentException: Invalid parameter user.

Thanks for the help.
Bassem

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.