-2
private void btnUpdate_Click(object sender, EventArgs e)
{
    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ALNETTE\Desktop\New folder\AccessDatabase.accdb");

    string Db = "Select * from StudentInfo";
    OleDbCommand cmd = new OleDbCommand(Db, con);

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        MessageBox.Show("Update");
        con.Close();
    }
    catch (Exception)
    {
        MessageBox.Show("error");
    }
}

I have this code for my update but when I am updating it, it only updates the datagridview and not my database. What should I do to update it also in database?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

4 Answers4

1

instead of using Select * from StudentInfo you need to use update query ("UPDATE TABLENAME SET COLUMN2=Values1 WHERE COLUMN1=Values")

For Example:

   private void btnUpdate_Click(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data   Source=C:\Users\ALNETTE\Desktop\New folder\AccessDatabase.accdb");
        string Db = "UPDATE StudentInfo SET StudentName='SomeoneName' WHERE StudentID=1";
        OleDbCommand cmd = new OleDbCommand(Db, con); 

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Update");
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("error"+ex.Message);
        }
    }
StartCoding
  • 394
  • 5
  • 16
ravi
  • 21
  • 1
  • i change the code in statement into, string Db = "UPDATE StudentInfo SET Surname='@Surname'WHERE STUDENT_ID=2015-001-0001"; still the same did i make a mistake in editing it? – Allan Patrick Caldito Sep 15 '14 at 04:39
0

You have a SELECT sql statement, you will need an INSERT or UPDATE statement to add or update data to the database.

eg:

INSERT INTO StudentInfo VALUES ('John Doe', '555-1212');

or

UPDATE StudentInfo SET 
Name = 'Joe Bloggs', PhoneNumber = '555,1234'
WHERE ID = 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • how do i type the value on datagridview? when i press update it automatically input the value i that code – Allan Patrick Caldito Sep 15 '14 at 04:51
  • 1
    @AllanPatrickCaldito **YOU** need to do research before you post questions and don't post the same question twice!! I was able to google **ms access datagridview update** and find 197,000 results. eg: http://stackoverflow.com/questions/9770561/updating-ms-access-database-from-datagridview and http://tech.pro/tutorial/664/csharp-tutorial-binding-a-datagridview-to-a-database - sooner or later you will need to learn programming yourself. [so] is here to help but we are not your research assistants. – Jeremy Thompson Sep 15 '14 at 05:06
  • i got tons of tabs in my browser is that what you call not reaserching? :( – Allan Patrick Caldito Sep 15 '14 at 05:30
0

Use update query instead. You were selecting rather updating.

Update synatx is:

Update tablname 
set col1 = values, col2 = values... 
WHERE some_column = some_value;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
StartCoding
  • 394
  • 5
  • 16
0
try
            {
                cBuild = new OleDbCommandBuilder(adapter);
                adapter.Update(dt);
            }
            catch (Exception)
            {
                MessageBox.Show("DO NOT DUPLICATE STUDENTID");
            }