ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

C# program that interfaces with MS Access: Store data (text, image) in an Access database file

Updated on December 3, 2009

1.      Open a new Visual C# .NET windows application. Name the project CreateDatabase.

2.      Design a form. Add three Textboxes and two Buttons

3.      Set Name property of Textboxes to “txtDatabase”, “txtStudentName” and “txtStudentPicture”.

4.      Set Name property of Buttons to “btnCreateTable” with text property “Create Table” and “btnSave” with text property “Save”.

5.      In the code file add namespace:

using System.Text;

using System.Data.OleDb;

6.      Double click on the Button “Create Table” and pest the following code into the “btnCreateTable_Click” event.


OleDbCommand Cmd; 
string SQL = "";  
OleDbCommand objCmd = new OleDbCommand(); 
 
OleDbConnection Con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data
source=" + txtDatabase.Text + "");
 
SQL = "CREATE
TABLE tblStudentInfo ([StudentID] COUNTER, [StudentName] TEXT(50), [Picture]
OLEObject)";
Cmd = new
OleDbCommand(SQL, Con);
 
      Con.Open();
      objCmd = new OleDbCommand(SQL,
Con);
     
objCmd.ExecuteNonQuery();
   Con.Close();

7.      Double click on the Button “btnSave” and pest the following code into the “btnSave_Click” event.

StoreData(ImageToStream(txtStudentPicture.Text));

8.      Add the following modules to the code file.

private byte[] ImageToStream(string fileName)

     {
        Bitmap image = new Bitmap(fileName);
        MemoryStream stream = new
MemoryStream();
       
image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        return stream.ToArray();
     }
 
    private void
StoreData(byte[] content)
     {
 
OleDbConnection
conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data source="
+ txtDatabase.Text + "");
    
conn.Open();
 
     if (conn.State.Equals(ConnectionState.Closed))
        
conn.Open();
         try
         {
OleDbCommand insert = new OleDbCommand("Insert into tblStudentInfo (StudentName,Picture)
values(@StudentName,@image)", conn);
 
OleDbParameter
picParameter = insert.Parameters.Add("@StudentName",
OleDbType.VarChar );
              
picParameter.Value = txtStudentName.Text;
              
picParameter.Size = 50;
OleDbParameter
imageParameter =  insert.Parameters.Add("@image", SqlDbType.Binary);
              
imageParameter.Value = content;
              
imageParameter.Size = content.Length;
              
insert.ExecuteNonQuery();
               MessageBox.Show("Data
Stored successfully"); 
         }
         catch (Exception ex)
            {
               MessageBox.Show(ex.Message.ToString());
               MessageBox.Show(ex.StackTrace.ToString());
            }
         finally
            {
              
conn.Close();
            }
  }

9.      Press F5 to build and run the project.

Put your database name with location like c:\MyAccessDB in the text box “txtDatabase”, put student name in the text box “txtStudentName” and put student picture path in the text box “txtStudentPicture” then press the “Create Table” Button and after that press “Save” button to save the student information to the database table.

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Narendra 7 years ago

      Thanx

      Its Working fine

    • profile image

      Khalid Elboushy 7 years ago

      It is V.Good And very Faster in saving data

      but we need to add Namespace System.IO

      using System.IO;

    • mbip profile image
      Author

      mbip 7 years ago

      Yes, u r right. I'll update it. Thanks again

    • profile image

      shweta 6 years ago

      it works well...can i get the code for retrieving data from Access using Generics

    • mbip profile image
      Author

      mbip 6 years ago

      Give me some time.

    • profile image

      Palani 6 years ago

      Very Useful Solution! Breaking my head for the past 4 hrs.

    • sambaran08 profile image

      sambaran08 6 years ago from India

      Nice blog,for any help about C# visit bloging,visit my blog https://hubpages.com/technology/Cnet

      and follow me

    • profile image

      Tester 6 years ago

      Don't work!

      Error: Cannot find/open "c:\MyAccessDB"!!!

      So, if the file MyAccessDB not exist, it must be create first, and then open and put the StudentName and picture! Thanx anyway! [sorry for my english :)]!

    • profile image

      suborno 5 years ago

      thanks a lot.can u send me.

    • profile image

      chandan 5 years ago

      public void StoreData(byte[] content)

      //Compilation Error in this line....

      Error 1 'StoreData': member names cannot be the same as their enclosing type E:\Chandan\MSAccessStoreImage\WindowsFormsApplication1\WindowsFormsApplication1\StoreData.cs 68 21 WindowsFormsApplication1

    • profile image

      sagar maiyad 5 years ago

      i cant open connection..

      error:

      Server Error in '/database' Application.

      Could not find installable ISAM.

      Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

      Exception Details: System.Data.OleDb.OleDbException: Could not find installable ISAM.

      Source Error:

      Line 46:

      Line 47:

      Line 48: Con.Open();

      Line 49:

      Line 50: objCmd = new OleDbCommand(SQL,

    • profile image

      amrit 5 years ago

      abe chalta nahi hai!!!!!!!!!!!!

    • profile image

      Ramchandra jha 5 years ago

      Sorry.......This programe is not run

      So plz help me

      Error:-

      Error 1 Newline in constant F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 27 43 CreateDatabase

      Error 2 Newline in constant F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 28 32 CreateDatabase

      Error 3 Newline in constant F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 30 7 CreateDatabase

      Error 4 Newline in constant F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 32 11 CreateDatabase

      Error 5 Newline in constant F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 75 40 CreateDatabase

      Error 6 Newline in constant F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 76 28 CreateDatabase

      Error 7 Newline in constant F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 93 32 CreateDatabase

      Error 8 Newline in constant F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 94 20 CreateDatabase

      Error 9 Expected class, delegate, enum, interface, or struct F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 49 13 CreateDatabase

      Error 10 Identifier expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 49 18 CreateDatabase

      Error 11 Expected class, delegate, enum, interface, or struct F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 49 20 CreateDatabase

      Error 12 Expected class, delegate, enum, interface, or struct F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 52 28 CreateDatabase

      Error 13 Expected class, delegate, enum, interface, or struct F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 54 1 CreateDatabase

      Error 14 Expected class, delegate, enum, interface, or struct F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 60 13 CreateDatabase

      Error 15 Identifier expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 61 16 CreateDatabase

      Error 16 Expected class, delegate, enum, interface, or struct F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 61 18 CreateDatabase

      Error 17 Expected class, delegate, enum, interface, or struct F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 65 12 CreateDatabase

      Error 18 Expected class, delegate, enum, interface, or struct F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 75 27 CreateDatabase

      Error 19 Type or namespace definition, or end-of-file expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 95 10 CreateDatabase

      Error 20 ) expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 27 81 CreateDatabase

      Error 21 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 27 81 CreateDatabase

      Error 22 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 28 32 CreateDatabase

      Error 23 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 28 35 CreateDatabase

      Error 24 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 30 14 CreateDatabase

      Error 25 Expected ; or = (cannot specify constructor arguments in declaration) F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 22 CreateDatabase

      Error 26 Invalid expression term '[' F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 23 CreateDatabase

      Error 27 ) expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 24 CreateDatabase

      Error 28 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 33 CreateDatabase

      Error 29 Invalid expression term ']' F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 33 CreateDatabase

      Error 30 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 35 CreateDatabase

      Error 31 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 42 CreateDatabase

      Error 32 Invalid expression term ',' F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 42 CreateDatabase

      Error 33 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 58 CreateDatabase

      Error 34 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 66 CreateDatabase

      Error 35 Invalid expression term ',' F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 66 CreateDatabase

      Error 36 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 31 77 CreateDatabase

      Error 37 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 32 10 CreateDatabase

      Error 38 Invalid expression term ')' F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 32 10 CreateDatabase

      Error 39 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 32 11 CreateDatabase

      Error 40 ; expected F:\4th SEM\CreateDatabase\CreateDatabase\Form1.cs 32 13 CreateDatabase

    • profile image

      martellawintek 4 years ago

      yous ok toni if your still hanging around i think this is the site

      and some info , they have a deal on at the mo , mention marta gift him out

    • profile image

      Jay Prakash 3 years ago

      Have a look at here

      Using Access Database with c# -- geeksprogrammings.blogspot.com

      http://geeksprogrammings.blogspot.com/2013/10/conn...

      For Deletion Record

      http://geeksprogrammings.blogspot.com/2013/09/dele...

    Click to Rate This Article