troubleshooting Question

Data is not saving in the database

Avatar of Abdullah Abdulkarim
Abdullah AbdulkarimFlag for United States of America asked on
Microsoft Access.NET ProgrammingC#
31 Comments1 Solution353 ViewsLast Modified:
I have problem how to save the data into database, when I click on the save everything seems fine and saved and I can see the data in the app form but when I close the app and open the database "accdb" I did not see the data.

Can you please help me. I do not know where my mistake
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;


namespace AlbahraniNetwork2
{
    public partial class Add : Form

          
    {
       
         string appPath = Path.GetDirectoryName(Application.ExecutablePath);
         class Util
         {
             static public string GetUserDataPath()
             {

                 string appPath = Path.GetDirectoryName(Application.ExecutablePath);
                 if (!appPath.EndsWith("\\"))
                     appPath += "\\";

                 return appPath;
        
            }
        }



        OleDbConnection _Conn = new OleDbConnection();

        OleDbConnection sc = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Util.GetUserDataPath() + "Database31.accdb");
         
        //OleDbConnection sc = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Abdullah\documents\visual studio 2010\Projects\AlbahraniNetwork2\AlbahraniNetwork2\Database31.accdb");
        OleDbCommand cmd;
        OleDbDataReader dr;



        public Add()
        {
            InitializeComponent();
           
         
        }

        private void Add1_Click(object sender, EventArgs e)


        {
            

            try{
                sc.Open();
                cmd = new OleDbCommand("insert into AlbahraniNetwork(FirstName,SecondName,LastName,Phone,Cell,Email,Address) values('" + FirstName.Text + "','" + SecondName.Text + "','" + LastName.Text + "','" + Phone.Text + "','" + Cell.Text + "','" + Email.Text + "','" + Address.Text + "')", sc);
                cmd.ExecuteNonQuery();
                MessageBox.Show(" ¿¿ ¿¿¿¿¿ ¿¿¿ ¿¿ ¿¿¿¿ ¿¿¿¿¿");
               
                 }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);



                sc.Close();

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Index newform = new Index();
            this.Hide();
            newform.ShowDialog();
            
        }


        private void button3_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string connetionString = null;
            OleDbConnection connection ;
            OleDbCommand command ;
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            DataSet ds = new DataSet();
            DataView dv ;
            string oledb = null;
           


            string appPath = Path.GetDirectoryName(Application.ExecutablePath);
            if (!appPath.EndsWith("\\"))
                appPath += "\\";
            connetionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + appPath + "Database31.accdb");



            
          
            oledb = "Select * from AlbahraniNetwork";
            connection = new OleDbConnection(connetionString);
            try
            {
                connection.Open();
                command = new OleDbCommand(oledb, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds, "Add New");
                adapter.Dispose();
                command.Dispose();
                connection.Close();

               


                
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.ToString());
            }
        }
    }
        }

       

    
ASKER CERTIFIED SOLUTION
hjgode
Tier 3 Senior Technical Support Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 31 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 31 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros