C# program that interface with MS Access: Modify data in an Access database file

1.      Open a new Visual C# .NET windows application. Name the project “Modify Data”.

2.      Design a form. Add two Textboxes, a Button and a Picturebox.

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

4.      Set Name property of Button to “btnReadData” with text property “Read Data”.

5.      In the code file add namespace:

using System.Text;

using System.Data.OleDb;

6.      Double click on the Button “Read Data” and pest the following code into the “btnReadData_Click” event.

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data
source=" + txtDatabase.Text + "");
           
conn.Open();
 
            switch (btnReadData.Text)
              {
                case "Read Data":
                    string sql = "Select
* from tblStudentInfo";
                    OleDbDataAdapter adapter = new
OleDbDataAdapter(sql, conn);
                    DataTable dtStudent = new
DataTable();
                   
adapter.Fill(dtStudent);
 
                   
StudentID = dtStudent.Rows[0]["StudentID"].ToString();
                   
txtStudentName.Text = dtStudent.Rows[0]["StudentName"].ToString();
 
                    byte[] content = (byte[])dtStudent.Rows[0]["Picture"];
                    try
                    {
                       
MemoryStream stream = new MemoryStream(content);
                       
picStudent.Image = Image.FromStream(stream);
                       
btnReadData.Text="Modify Data";
                    }
                    catch (Exception ex)
                    {
                       
MessageBox.Show(ex.Message.ToString());
                    }
                    break;
                case "Modify
Data":
 
                    try
                    {
                       
byte[] ImageStream = ImageToStream();
                       
OleDbCommand Update = new OleDbCommand("Update tblStudentInfo set StudentName=@StudentName,
Picture=@image where StudentID=" + StudentID + "", conn);
 
                       
OleDbParameter picParameter =
Update.Parameters.AddWithValue("@StudentName",
OleDbType.VarChar);
                       
picParameter.Value = txtStudentName.Text;
                       
picParameter.Size = 50;
                       
OleDbParameter imageParameter =
Update.Parameters.AddWithValue("@image",
SqlDbType.Binary);
                       
imageParameter.Value = ImageStream;
                        imageParameter.Size =
ImageStream.Length;
                       
Update.ExecuteNonQuery();
                       
MessageBox.Show("Data
Modified successfully");
                       
btnReadData.Text = "Read Data";
                    }
                    catch (Exception ex)
                    {
                       
MessageBox.Show(ex.Message.ToString());
                    }
                    finally
                    {
                       
conn.Close();
                    }
                    break;
              }

7.      Add the following modules the code file.

private byte[] ImageToStream()
        {
            Bitmap image = new Bitmap(picStudent.Image);
            MemoryStream stream = new
MemoryStream();
            image.Save(stream,
System.Drawing.Imaging.ImageFormat.Bmp);
            return stream.ToArray();
        }
 
 
        private void
picStudent_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog oFileDialog1 = new OpenFileDialog();
            oFileDialog1.InitialDirectory = "c:\\";
           
oFileDialog1.Filter = "Image
Files(*.BMP)|*.BMP";
           
oFileDialog1.FilterIndex = 1;
           
oFileDialog1.RestoreDirectory = true;
            if (oFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (oFileDialog1.FileName != null)
                {
                   
picStudent.Load(oFileDialog1.FileName);
                }
            }
        }

8.      Press F5 to build and run the project.

9.      Put your database name with location like c:\MyAccessDB in the text box “txtDatabase” and then press the “Read Data” Button to read the data. After that double click “picStudent” PictureBox to modify Student Picture and change text in “Student Name” text box and press “Modify Data” to save the new information to database.

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working