Viewing 10 posts - 1 through 10 (of 10 total)
  • Any C# programers in house?
  • Nezbo
    Free Member

    I am looking inserting in to an access database…

    I have set up my dataset in C# Express.

    I want to simply send some data to the database when i click a button.

    i have been playing with code like this:

    DataRow newCustomersRow = timeDataSet.Tables["theTimes"].NewRow();
    newCustomersRow["ID"] = "1";
    newCustomersRow["User"] = "Alfreds Futterkiste";
    newCustomersRow["LieuHours"] = "23";
    timeDataSet.Tables["theTimes"].Rows.Add(newCustomersRow);

    But i can get it to work 🙁

    Any help please?

    GrahamS
    Full Member

    May I commend you to: http://stackoverflow.com ?

    Ask there and you will have an answer in minutes.

    toby1
    Full Member

    You are just working with an in memory dataset there, do you have a datasource and is it bound? I'm guessing these are answers you might not have yet, but they are the areas you'd need to look into first. Like GS says, there will be loads on forums all over about databinding using Access as a datasource.

    EDIT: Oh, and good luck 🙂

    glenh
    Free Member

    What does your debugger say? Often helpful…

    fubar
    Free Member

    so many potential problems but I guess there needs to be some kind of .Update() method to be called on the dataset or related adapter…
    see here for example

    Make sure you now what version of the .Net framework you are using as you may find lots of 'solutions' relating to .Net 1.1 etc and you may actually be using 4.0 and so the best solution may differ

    Nezbo
    Free Member

    cheers toby1 🙂

    i now have this code:
    private void button1_Click(object sender, EventArgs e)
    {

    System.Data.OleDb.OleDbConnection conn = new
    System.Data.OleDb.OleDbConnection();
    // TODO: Modify the connection string and include any
    // additional required properties for your database.
    conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
    @"Data source= C:\time.mdb";
    try
    {
    conn.Open();
    //MessageBox.Show("That worked");
    }
    catch (Exception ex)
    {
    MessageBox.Show("Failed to connect to data source");
    }
    finally
    {
    conn.Close();
    }

    }

    And it connects to the database 🙂 and it shows the message 'That worked'

    how do i now insert a row into the table?

    toby1
    Full Member

    Once you open the connection you can then use commands on the database, so selects, inserts and updates;

    http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection_methods.aspx

    Should help you link the dots along with this ExecuteNonQuery

    Nezbo
    Free Member

    Right…

    I have now got this far 🙂

    string query = "INSERT INTO theTimes (User) VALUES (?)";
    string query2 = "Select @@Identity";
    int ID2;
    string connect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|time.mdb;Persist Security Info=False;";
    //string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|time.mdb";
    using (OleDbConnection conn = new OleDbConnection(connect))
    {
    using (OleDbCommand cmd = new OleDbCommand(query, conn))
    {
    cmd.Parameters.AddWithValue("", NameTxtBox.Text);
    conn.Open();
    MessageBox.Show("OK So Far!!");
    cmd.ExecuteNonQuery();
    MessageBox.Show("Still OK??");
    cmd.CommandText = query2;
    ID2 = (int)cmd.ExecuteScalar();
    NameTxtBox.Text = "test";
    }
    }

    But i am now getting an error at the 'cmd.ExecuteNonQuery();'

    Anyone any ideas why?

    mtb_rossi
    Free Member

    Open the connection before you create the command.

    using (OleDbConnection conn = new OleDbConnection(connect))
    conn.Open();
    {
    using (OleDbCommand cmd = new OleDbCommand(query, conn))
    {
    cmd.Parameters.AddWithValue("", NameTxtBox.Text);

    Also add a parameter name. Probably doesn't like having a blank string.

    toby1
    Full Member

    In agreement with mtb_rossi here, you are adding a parameter with a empty string value so the complier knows not what to do with it. Traditionally you'd give it the name of the @@Identity equivalent from the insert statement. Bit of a late reply, I couldn't log into the site from work and couldn't be bothered to clear my cookies, I get more work done if I remain logged out of this site.

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Any C# programers in house?’ is closed to new replies.