0

I understand that this may be considered a duplicate question, but I am at an impasse and i do not know what it is I am doing wrong, I need help. The duplicate question said to use OLEDB in order to write to the Microsoft Access file, where as before I was using the SQL connection to accomplish my task. As far as i can tell there are no syntax, logic, or runtime errors and Visual Studios doesnt have an issue either.

When i run the code and go to add a new entry to the Microsoft Access Database Table, it says it worked, but when I go and look at the file there is NOTHING there. Someone please help me, I have goe through all the links, all the web pages, all the search engines, and I dont know what is wrong. I would love to learn what is wrong and how to fix it so I wont ever have to ask for help again.

Currently I am a college student and I am working on a team assignment. Our task, is to create a window that will take input from a user and then add it as a entry into an Microsoft Access File as if it were a SQL database.

The issue we are having is that we are trying to add the new entry to a local Microsoft Access file under the table named Artist. I have connected to an actual SQL server before and no one else in my group has, even worse no one has done this with using a Microsoft Access file on our PC either.

I have added the Database (Microsoft Access File) in Visual Studios using the Database Configuration Wizard. At first I was using straight SQL do to this and then i was told i need to use OLEDB in order to do this. SO I have implemented in the code and for some reason I still cannot get it to work. If anyone can give me a hand and tell me what it is I am doing wrong, I would greatly appreciate it.

Details that I believe are important for anyone to help me:

The name of the table I am attempting to write a new entry to: Artist

Name of the Microsoft Access File: Kays.accdb

Location of the Microsoft Access file: C:\KayDB\Kays.accdb (local machine)

Once again I would greatly appreciate any help that anyone can give me. I really am curious as to why the code is not working, please help me understand.

My code is as follows:

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 Kay
{
public partial class Kay_Green : Form
{

    string Username,Fname, Mname, Lname, streetaddress, city, phonenumber, emailaddress, zipcode, taxIDnummber, state;

    string[,] SQLTable = new string[0, 10];

    public Kay_Green()
    {
        InitializeComponent();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {//Save Button

        /*Upon clicking the save button, gather all info and save it into an temp array.
          Then send it to the SQL database.*/

        /*The order of the data in the array will be the same order in the SQL table.*/

        Fname = tbFirstName.Text;
        Mname = tbMiddleInitial.Text;
        Lname = tbLastName.Text;
        streetaddress = tbStreet.Text;
        city = tbCity.Text;
        state = cbState.Text;
        phonenumber = tbPhoneNumber.Text;
        emailaddress = tbEmailAddress.Text;
        zipcode = tbZipCode.Text;
        taxIDnummber = tbTaxID.Text;
        Username = tbUserName.Text;

        /*SQLTable[0,0] = taxIDnummber;
        SQLTable[0,1] = Fname;
        SQLTable[0,2] = Mname;
        SQLTable[0,3] = Lname;
        SQLTable[0,4] = streetaddress;
        SQLTable[0,5] = city;
        SQLTable[0,6] = state;
        SQLTable[0,7] = zipcode;
        SQLTable[0,8] = phonenumber;
        SQLTable[0,9] = emailaddress;*/



        /*Below is the details for the SQL connection*/

        string connectstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\KayDB\\Kays.accdb";
        OleDbConnection connection=new OleDbConnection(connectstring);
        OleDbCommand command;
        OleDbDataAdapter adapter;
        DataTable dt = new DataTable();


        string sql ="Insert into ARTIST values ('" + taxIDnummber + "','" 
            + emailaddress + "','" + Fname + "','" + Mname + "','"
            + Lname + "','" + phonenumber+"','"+ Username + "','" 
            + streetaddress + "','" + city + "','" +state+ "','" 
            + zipcode + "')";



        try
        {

            connection.Open();

            command = new OleDbCommand(sql, connection);

            MessageBox.Show("Connection Open And data added to table! ");


            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection ! " + ex.StackTrace.ToString());

        }
        /*Above is the details for the SQL connection*/

    }

    private void btnCancel_Click(object sender, EventArgs e)
    {//Cancel Button
        tbCity.Clear();
        tbEmailAddress.Clear();
        tbFirstName.Clear();
        tbLastName.Clear();
        tbMiddleInitial.Clear();
        tbPhoneNumber.Clear();
        tbStreet.Clear();
        tbTaxID.Clear();
        tbZipCode.Clear();
        tbUserName.Clear();

        Close();//Go back to switchboard from here


    }

    private void btnClear_Click(object sender, EventArgs e)
    {//Clear button

        tbCity.Clear();
        tbEmailAddress.Clear();
        tbFirstName.Clear();
        tbLastName.Clear();
        tbMiddleInitial.Clear();
        tbPhoneNumber.Clear();
        tbStreet.Clear();
        tbTaxID.Clear();
        tbZipCode.Clear();
        tbUserName.Clear();
    }


}

}

Spr89
  • 81
  • 1
  • 9
  • 1
    http://stackoverflow.com/questions/7764707/sql-connection-string-for-microsoft-access-2010-accdb – Trey Mar 28 '17 at 14:29
  • See https://www.connectionstrings.com/access/ to build a proper connection string. Use OleDbXxxxx for the proper connection types. – Igor Mar 28 '17 at 14:31
  • You may wanna add the connection provider as "Provider=Microsoft.ACE.OLEDB.14.0". Have a look at [this](http://stackoverflow.com/questions/8302349/oledb-connection-to-access-database-accdb) – amyn Mar 28 '17 at 14:33
  • Sorry about the duplicate question, that specific result did not come up for me when i was searching. Ill read it now to see if it can hep me. Thank you for your help @stuartd – Spr89 Mar 28 '17 at 14:33
  • @Spr89 the first answer whould get you going: note you need to use `OleDbCommand` and `OleDbConnection` for Access. – stuartd Mar 28 '17 at 14:35
  • @stuartd, I have updated my code and it works, but it doesnt write to the access file. Any thoughts on why? – Spr89 Mar 28 '17 at 21:57
  • 1
    First thing to do is _rename_ your MS Access file in the file system and see if you get an error. This will confirm whether you are writing to the file you think you are. If you don't get an error, then next thing to do is rename your table and see if you get an error. – Nick.Mc Mar 28 '17 at 22:59
  • @Nick.McDermaid, Thanks for the troubleshooting tip, that wouldnt have dawned on me for a while. I changed the name of the file, and now the stack trace says that I have an issue on line 78 (connection.Open()). Looking at what is involved for this line of code, your test worked. Visual Studios does see the file, but i still cannot write to it. Any other thoughts/suggestions? I would be more than happy to listen. – Spr89 Mar 28 '17 at 23:08
  • I'm looking through your code. You don't actually execute the SQL anywhere. You build a string but you don't run it. I think you're missing `command.execute` or something like that – Nick.Mc Mar 28 '17 at 23:18
  • 1
    This is definitely not a duplicate. You need to call `command.ExecuteNonQuery` to actually execute your command object. This has nothing to do with connection strings. – Nick.Mc Mar 28 '17 at 23:21
  • @Nick.McDermaid Thank you, i feel the same way about my question too :-). This command seems to be used with "System.Data.SqlClient;" (or at least the internet says so), can it also be used excatly the same way with "System.Data.OleDb;"? – Spr89 Mar 29 '17 at 01:36
  • I don't know. I'd just be googling it. You should try and see.I know that you need to run something to execute your command. Google will tell you which one. Lets have a google race and see – Nick.Mc Mar 29 '17 at 01:38
  • My original google turned up this page: https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executenonquery(v=vs.110).aspx which says it's from namespace `System.Data.OleDb` – Nick.Mc Mar 29 '17 at 01:40

1 Answers1

0

This code

command = new OleDbCommand(sql, connection);

sets up a command but does not run it

you need to run this afterwards:

command.ExecuteNonQuery();

This has nothing to do with connection strings

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91