Error while connecting to access database

May 1 2012 8:25 AM
Hello All,

I am a newbie in C#. I am designing a form for inputting data to Access database. However after repeated attempts I keep on getting errors. Please help. It has ten textboxes, and few buttons. The error happens with Inser, Delete buttons. My code is below. I am also uploading code file.
Thanks in advance.

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;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private OleDbConnection conn;
   
        public Form1()
        {
            InitializeComponent();
            conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Power.accdb");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'powerDataSet.DailyJob' table. You can move, or remove it, as needed.
            this.dailyJobTableAdapter.Fill(this.powerDataSet.DailyJob);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.dailyJobBindingSource.MoveFirst();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtDate.Text = "";
            txtLocation.Text = "";
            txtIssues.Text = "";
            txtContractNumber.Text = "";
            txtContractorName.Text = "";
            txtNoOfWorkHours.Text = "";
            txtNoOfWorkmen.Text = "";
            txtPreparedBy.Text = "";
            txtWorkDone.Text = "";
            txtTotalManhours.Text = "";
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Insert into DailyJob (Date_Today, Location, Work_Done, Issues, Contractor_Name, No_of_Workmen, Contract_Number, No_of_Work_Hours, Total_Manhours, Prepared_By) Values('" + txtDate.Text + "','" + txtLocation.Text + "', '" + txtIssues.Text + "','" + txtContractorName.Text + "', '" + txtNoOfWorkmen.Text + "','" + txtContractNumber.Text + "','" + txtNoOfWorkHours.Text + "','" + txtTotalManhours.Text + "','" + txtPreparedBy.Text + "')";
                cmd.Connection = conn;
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error" + "" + ex.Source);
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Delete from DailyJob where (Location=" + txtLocation.Text + ")";
                cmd.Connection = conn;
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error" + "" + ex.Source);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select from DailyJob where (Location=" + txtLocation.Text + ")";
                cmd.Connection = conn;
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error" + "" + ex.Source);
            }
        }
    }
}



Answers (1)