473,508 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 11305
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
34035
by: Clay Beatty | last post by:
When you create database diagrams in Enterprise Manager, the details for constructing those diagrams is saved into the dtproperties table. This table includes an image field which contains most of...
2
1294
by: Roy | last post by:
Hi all, I've filled a dataset from a select dataadapter. Dataset has been modified and I need to send changes back to the database. Since dataadapter has been disposed, what is the code to send...
1
4478
by: Sam Martin | last post by:
Hi all, got a problem when converting a PNG to a JPG using the Image.Save(stream, format) method. When I save the original PNG to a file and open it, it's fine. I then save it to a database,...
4
4511
by: Jae | last post by:
I'm writing a web application that exports and imports excel files. The application gets a list of users and their info and displays it in a datagrid .The user then selects to save the file as a...
3
1616
by: Simon | last post by:
Hi everyone, I have a small problem regarding a wizard that I'm making on my website. The wizard is obviously a series of pages that take values from the user. My question is: - Should I...
9
2132
by: DapDap | last post by:
I have manage to open a image into a picture box. But I have problem in Saving it into database SQL Server. How to save the image ??
9
1449
by: Martin Arvidsson | last post by:
Hi! I have two computed datafields that i am displaying in my windowsform. When i try to update the dataset and commit the transaction. I get an error message telling me that i cant update...
3
9309
by: =?Utf-8?B?TXJrIEJsYWNrYWxs?= | last post by:
Hi all, This is my first attempt to update an access database in VB.Net 2005. It goes broadly as follows (I have condensed it): Dim MyConnection As New Data.OleDb.OleDbConnection...
6
3785
by: Suresh | last post by:
Hi All, I am fetching a dataset from the database under some condition. After this I create a data table. Traverse in the original dataset & add each row to created data table as it is through...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.