0

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");
        }
    }
Bob C
  • 55
  • 1
  • 1
  • 10
  • Are you getting an exception? If so, what exception is being thrown? – xspydr Feb 11 '14 at 21:01
  • FYI, if you're trying to troubleshoot, you would do better to display `expe.ToString()` since it includes all of the information. Additionally, your `OleDbConnection` and `OleDbCommand` should be in `using` blocks to ensure they are cleaned up, even if an exception is thrown. – John Saunders Feb 11 '14 at 21:02
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 11 '14 at 21:04
  • What is `Name` and where is defined? – Steve Feb 11 '14 at 21:08
  • The exception is: Update requires a valid UpdateCommand when passed DataRow collection with modified rows. @Name is one field I had in my table, just trying to test the writing part on it. Pretty sure I am doing this wrong. – Bob C Feb 11 '14 at 21:11
  • This message belongs to `OleDbDataAdapter.Update` command, it is not from this code. – Steve Feb 11 '14 at 21:17
  • I was asking about the variable `Name` used to assign the value for the parameter `@Name` that will be used to insert a new value in the field `TestFC` of the table `TestFC`. I suspect that your table is not as it seems from the code above. – Steve Feb 11 '14 at 21:19
  • check your parameter @name. Named parameters are not supported. http://stackoverflow.com/questions/1476770/oledbcommand-parameters-order-and-priority – smiggleworth Feb 11 '14 at 21:20
  • Ok I am in over my head, like I said I was new to this. Just trying to get something that works. I appreciate the comments, but not sure how to implement the solution. – Bob C Feb 11 '14 at 21:23
  • The code I have works for viewing data inside the form (data being the specific fields in the table:db) I just can't write new info into the db. – Bob C Feb 11 '14 at 21:24
  • What is the schema of your table? – Steve Feb 11 '14 at 21:24
  • If I understand correctly = XSD? – Bob C Feb 11 '14 at 21:54
  • No, the table inside the database Access you are using, the table named TestFC – Steve Feb 11 '14 at 22:09
  • Sorry I have no idea how to find your request. Yes the table is TestFC...Sorry, again new to this. I think I am just going to give up, I don't know what to do on this... – Bob C Feb 11 '14 at 22:12
  • @BobC look at the connection string, there is a path ending with the name of an MS-Access database. If you open that database using MS-Access you should find a table named `TestFC` and this is the table where you are trying to write your data. It is composed of fields. I wish to know the names of these fields. However, if you don't know how to work out these details, then I effectively think you need a programmer to help you. – Steve Feb 11 '14 at 22:16
  • The fields are WOID (double), WONum (txt), WOdesc (txt), WOStatus (txt), ISD (txt). – Bob C Feb 11 '14 at 22:22
  • 1
    So your insert statement is wrong. Should be something like `INSERT INTO TestFC (WOID, WONum, WODesc, WOStatus, ISD) VALUES (?,?,?,?,?)` and then in the part where you add the parameters, you should add all the values marked by the ?. (Of course these values should be read from somewhere on your input form) – Steve Feb 11 '14 at 22:55

2 Answers2

2

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

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thank you Steve, I will test this out & report back. I do appreciate your time on this! – Bob C Feb 12 '14 at 14:40
  • I get the name 'these' does not exist in the current context?? 'these' = doubleValue, valueForWO, valueForDESC, valueForStatus, valueForISD what did I miss, as this will not run now? Ok, I changed Parameters with the WOID, WONum, WODesc, etc. I do not have the option to find a programmer. I am doing this as there is no one else to help, that is why I am on here! – Bob C Feb 12 '14 at 15:36
  • I have 5 boxboboxes with those field names in the form. – Bob C Feb 12 '14 at 15:48
  • I also get 'Invalid expression term 'try'. – Bob C Feb 12 '14 at 15:51
  • Yes, you need to extract the values of the textboxes and put the values in the variables named 'these' then call the click event to submit the insert. Really I can't help you more on this without the project files and the database files. – Steve Feb 12 '14 at 15:52
  • Could you explain why the accepted answer now works? – Steve Feb 12 '14 at 20:57
  • It doesn't I just gave up on it. – Bob C Feb 13 '14 at 23:07
  • I get down voted for this comment? I tried everything I could, everything you mentioned. And it still doesn't work, does not commit changed to the database, plus new errors I didn't have before. I appreciate your help, just a lost cause. – Bob C Feb 13 '14 at 23:14
  • I haven't given a downvote. I have just asked why you accepted a clearly wrong answer after the comments here on my answer. Still I think that I could help if you share the project and the database file (if possible) – Steve Feb 13 '14 at 23:16
  • You have lost 2 reps because you have unaccepted an answer (the same two reps given when you accepted the other answer) – Steve Feb 13 '14 at 23:21
0

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");
    }
}
smiggleworth
  • 534
  • 4
  • 16