Question

I am having issues writing new data from my form to my access (mdb). I have a form with a few fields that work fine, I can read the data in the table. However, I can't edit in the form and write back new info into the database? See code below and if possible please edit a solution. I have exhausted all options at this point. I am also new to C#, so I apologize in advance for not explaining everything correctly!

    public Form1()
    {
        InitializeComponent();
    }

    private void WObutton_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Michael\Documents\Visual Studio 2012\Projects\WorkOrderTKv2\WorkOrderTKv2TestAccessdb.mdb";

        conn.Open();

        OleDbCommand cmmd = new OleDbCommand("INSERT INTO TestFC (TestFC) Values(@Name)", conn);
        if (conn.State == ConnectionState.Open)
        {
            cmmd.Parameters.Add("@Name", OleDbType.VarWChar, 20).Value = Name;

            try
            {
                cmmd.ExecuteNonQuery();

                MessageBox.Show("DATA ADDED");

                conn.Close();
            }
            catch (OleDbException expe)
            {
                MessageBox.Show(expe.Message);
                conn.Close();
            }
        }
        else
        {
            MessageBox.Show("CON FAILED");
        }
    }
Was it helpful?

Solution

It is not easy to give you a correct answer because many details are missing from your question. In particular I don't know where you get the values to insert in the database. These values should be read from some kind of input textboxes and passed to your database table for insertion.

Just to fix your procedure called when you click the button (with notes where the missing info should be added by you looking at your input form)

private void WObutton_Click(object sender, EventArgs e)
{
    string cmdText = "INSERT INTO TestFC (WOID, WONum, WODesc, WOStatus, ISD) VALUES (?,?,?,?,?)";
    string cnString = @"Provider=Microsoft.ACE.OLEDB.12.0;
     Data Source=C:\Users\Michael\Documents\Visual Studio 2012\Projects\WorkOrderTKv2\WorkOrderTKv2TestAccessdb.mdb";
    using(OleDbConnection conn = new OleDbConnection(cnString))
    using(OleDbCommand cmmd = new OleDbCommand(cmdText, conn))
    {
        conn.Open();
        cmmd.Parameters.AddWithValue("@FirstParam", doubleValue);  // Need a double for field ID
        cmmd.Parameters.AddWithValue("@SeconParam", valueForWO);   // Need a string
        cmmd.Parameters.AddWithValue("@ThirdParam", valueForDESC);   
        cmmd.Parameters.AddWithValue("@FourthParam", valueForStatus);
        cmmd.Parameters.AddWithValue("@FifthParam", valueForISD);   

        try
        {
            cmmd.ExecuteNonQuery();
            MessageBox.Show("DATA ADDED");
            conn.Close();
        }
        catch (OleDbException expe)
        {
            MessageBox.Show(expe.Message);
        }
    }
}

As I have said this is just a mock up of what should be your code. You need to retrieve the values to pass to your parameters from your input form

OTHER TIPS

In the insert statement can you replace @Name with ?. All parameters will be applied in the order you add the parameters to the command.

public Form1()
{
    InitializeComponent();
}

private void WObutton_Click(object sender, EventArgs e)
{
    OleDbConnection conn = new OleDbConnection();
    conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Michael\Documents\Visual Studio 2012\Projects\WorkOrderTKv2\WorkOrderTKv2TestAccessdb.mdb";

    conn.Open();

    OleDbCommand cmmd = new OleDbCommand("INSERT INTO TestFC (TestFC) Values(?)", conn);
    if (conn.State == ConnectionState.Open)
    {
        cmmd.Parameters.Add("@Name", OleDbType.VarWChar, 20).Value = Name;

        try
        {
            cmmd.ExecuteNonQuery();

            MessageBox.Show("DATA ADDED");

            conn.Close();
        }
        catch (OleDbException expe)
        {
            MessageBox.Show(expe.Message);
            conn.Close();
        }
    }
    else
    {
        MessageBox.Show("CON FAILED");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top