tags:

views:

129

answers:

3

it show me this error Incorrect syntax near the keyword 'Select' to make to clear Employee ID in this case is FK in the table (Attendance detail) and the other thing is i am using Data Grid View from another table(Employee information) to Show the list of the staff in my form. then i want to transfer each selected cell value from Data Grid View to attendance detail table. 10q

    private void time_in_Click(object sender, EventArgs e)
    {
        employee_InformationDataGridView.SelectedCells[0].Style.ForeColor = Color.Green;

            time_in.Enabled = false;
            time_out.Enabled = true;

            con = new SqlConnection(GlobalClass.conn);
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert into Attendancedetail  Values(" + "Select from  EmployeeInformation(EmployeeID)" + ",'" + employee_InformationDataGridView.SelectedCells[0] + "','" + DateTime.Now.ToLongDateString() + "','" + null + "','" + null + "','" + DateTime.Now.ToLongTimeString() + "'," + null + ")", con);
            int i = cmd.ExecuteNonQuery();
            MessageBox.Show(i.ToString() + " record inserted");
    }
+2  A: 

The syntax of your SQL query is wrong. A SELECT statement requires you to specify the columns you want to return. For example:

SELECT EmployeeID FROM EmployeeInformation
Darin Dimitrov
10q for the fast response but it is not work it show me this error Incorrect syntax near the keyword 'Select'.Incorrect syntax near ''.SqlCommand cmd = new SqlCommand("Insert into Attendancedetail Values(" + "Select EmployeeID From EmployeeInformation" + ",'" + null + "','" + null + "','" + null + "','" + null + "','" + DateTime.Now.ToLongTimeString() + "'," + null + ")", con);
Six fourty
+1  A: 

I agree with @Darin, but the full syntax is

Insert into Attendancedetail (columns list) select columns list from EmployeeInformation where employeeid = ' selected cells value'

The Values keyword is not used in such a statement. The 1st columns list is optional if the Select returns all needed columns.

See another question for more examples

Timores
10q for the fast response but it is not work it show me this errorIncorrect syntax near the keyword 'Select'.Incorrect syntax near ','. SqlCommand cmd = new SqlCommand("Insert into Attendancedetail Values(" + "Select Employee from EmployeeInformation where FullName = ' Selected Cells Value'" + ",'" + null + "','" + null + "','" + null + "','" + null + "','" + DateTime.Now.ToLongTimeString() + "'," + null + ")", con);
Six fourty
Remove the the values keyword. Your select is only returning one column, so specify its name after the inserted table name (except if this Attendancedetail table has only one column).Consider using Management Studio (or whatever client program of your DB) to have the statement correct, then add it to your code.And follow @Raj's advice of using parameterized queries.
Timores
A: 

Consider using parameterized queries. Writing code like this will result in sql injection.

http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html

Raj Kaimal