[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting an error : System.Data.SqlClient.SqlException: Incorrect syntax near '='.

Posted on 2011-09-22
2
Medium Priority
?
521 Views
Last Modified: 2012-05-12
I am trying to display meta tag information that is stored in SQL database on to my product page which is built in asp.net 2.0 and C#.

All I am trying to do is if the catalogItemId of SeoText table equals to the current Product Item id then display its page title, meta tag information

I am trying to call this code on Page_Load event of the product page. but it keeps throw like below


   System.Data.SqlClient.SqlException: Incorrect syntax near '='.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at mysite.product.Page_Load(Object sender, EventArgs e) in http://server/mysite/browse/product.aspx.cs:line 120


AND LINE 120 IS : SqlCmd.ExecuteNonQuery();

string query = "select MetaDescription, MetaKeywords, H1Text FROM SeoText" + "Where CatalogItemId ='" + this.CurrentProduct.ItemId + "'" ;
    SqlConnection myconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDBConnection"].ConnectionString);
    SqlCommand SqlCmd = null;
    SqlCmd = new SqlCommand(query, myconnection);
    SqlCmd.Connection.Open();
    SqlCmd.ExecuteNonQuery();
    
	SqlDataAdapter ad = new SqlDataAdapter(SqlCmd);
    DataTable dt = new DataTable();
    ad.Fill(dt);
   
   
   this.Page.Title = dt.Rows[0]["MetaDescription"].ToString();
   HtmlMeta metaDesc = (HtmlMeta)Page.Header.FindControl("metaDesc");
   metaDesc.Content = dt.Rows[0]["MetaDescription"].ToString();
   HtmlMeta metaKey = (HtmlMeta)Page.Header.FindControl("metaKey");
   metaKey.Content = dt.Rows[0]["MetaKeywords"].ToString();
   Label1.Text = dt.Rows[0]["H1Text"].ToString();

Open in new window


any help is appreciated.

Thanks
0
Comment
Question by:niceoneishere
2 Comments
 
LVL 30

Accepted Solution

by:
Brad Howe earned 2000 total points
ID: 36582497
Try this for your query.

string query = "select MetaDescription, MetaKeywords, H1Text FROM SeoText Where CatalogItemId ='" + this.CurrentProduct.ItemId + "'" ;

Cheers,
Hades666
0
 
LVL 2

Author Closing Comment

by:niceoneishere
ID: 36582557
Brilliant Thanks
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question