0

I have executed the above query, and I got an error :

{System.Data.OleDb.OleDbException: Syntax error. in query expression 
'Select [UserID] from UserDetails'

and my sample code is below:

OleDbCommand cmd1 = new OleDbCommand("Insert into UserCompanyDetails([UserID],[CompanyID]) values (" + "Select [UserID] from UserDetails" + "," + "Select @@identity" + ");", conn);
                        conn.Open();
                        cmd1.ExecuteNonQuery();
                        conn.Close();

Why am I getting this error?

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Siva
  • 259
  • 6
  • 24

2 Answers2

0

You should put your subqueries between parentheses:

"Insert into UserCompanyDetails([UserID],[CompanyID]) values ((Select [UserID] from UserDetails),(Select @@identity))"
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
0

Another approach:

Try to save the results of the following queries in variables of their respective datatypes.

Select [UserID] from UserDetails
Select @@identity

Then you can simply use those variables in your Main Query String

Safeer
  • 184
  • 3
  • 17