2 Replies - 2233 Views - Last Post: 20 February 2013 - 06:40 AM Rate Topic: -----

#1 ravva  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 28-November 12

calculating sum and add another table

Posted 20 February 2013 - 12:45 AM

Im trying to calculate sum of field(Marks)in table(marks) and sum value save in another table(wkall) field Sum1.
but it gives error message "Syntax error in query expression 'SELECT sum(marks) FROM data'."
Do you have any idea of this,Is my Sql Query has error..??
my code is like follow
 private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + @"data source=new.accdb");
            myConnection.Open();
           
            OleDbCommand myCommand = new OleDbCommand();
            myCommand.Connection = myConnection;

            myCommand.CommandText = "CREATE TABLE wkall([Sum1] double)";
            myCommand.ExecuteNonQuery();
            MessageBox.Show("Table Created Successfully");
                       
            OleDbCommand myCommand1 = new OleDbCommand();
            myCommand1.Connection = myConnection;

            myCommand1.CommandText = "INSERT INTO wkall(Sum1) VALUES(SELECT sum(marks) FROM data)";
            myCommand1.ExecuteNonQuery();
            MessageBox.Show("Data Inserted Successfully");
        }



data table id following format
Name(text) marks(double)

Is This A Good Question/Topic? 0
  • +

Replies To: calculating sum and add another table

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,531
  • Joined: 05-May 12

Re: calculating sum and add another table

Posted 20 February 2013 - 05:52 AM

This is more of a SQL question rather than a C# question and should be moved there.

Here is my feeble attempt at answering the SQL question: Isn't the correct syntax something like "INSERT INTO destTabel(col1, ...) SELECT col1, ... FROM srcTable" ?
Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is online

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,415
  • Joined: 08-April 09

Re: calculating sum and add another table

Posted 20 February 2013 - 06:40 AM

I suggest OP to read about SQL subqueries

This post has been edited by Michael26: 20 February 2013 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1