4 Replies - 523 Views - Last Post: 21 March 2012 - 02:31 AM Rate Topic: -----

#1 gngr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-March 12

SharePoint C# Syntax error

Posted 20 March 2012 - 09:14 AM

Hi
I’m trying to build a SharePoint web part in c# that will pull SQL data into a drop down list and filter the results based on the current user.

So far I’ve managed to pull the data from SQL but when I’ve tried to filter it I get the error message
“Incorrect syntax near ‘Account’”

The code I’ve used to get the SP user is
 SPUser currentUser = SPContext.Current.Web.CurrentUser;
String userName = currentUser.Name; 

And the Where clause is
”…where useritem.name =”+ currentUser.Name, MySQLconnection); 

I’m a complete c# newbie so I’ve had to guess this from other bits of code I’ve seen.
If someone could point me in the right direction that would be great!

Is This A Good Question/Topic? 0
  • +

Replies To: SharePoint C# Syntax error

#2 Zigon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: SharePoint C# Syntax error

Posted 20 March 2012 - 09:51 AM

Could be way off, but this

Quote

where useritem.name =
looks wrong!

IMHO it should be somethimg like this

Quote

where username =


that is, if the "username" represents the field in the table...
Was This Post Helpful? 0
  • +
  • -

#3 gngr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-March 12

Re: SharePoint C# Syntax error

Posted 20 March 2012 - 10:11 AM

it works if I do
 where useritem.name = 'System Account' 

useritem.name is the database where the users are stored
Was This Post Helpful? 0
  • +
  • -

#4 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: SharePoint C# Syntax error

Posted 20 March 2012 - 07:44 PM

Yes, that's because in Sql, you must have a quotation mark around what you're looking for, just like a string in C#. So just change it to this


”…where useritem.name ='”+ currentUser.Name + "'", MySQLconnection);



Was This Post Helpful? 1
  • +
  • -

#5 gngr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-March 12

Re: SharePoint C# Syntax error

Posted 21 March 2012 - 02:31 AM

That works and I understand it now! Thank you! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1