?
Solved

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

Posted on 2011-09-22
2
Medium Priority
?
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1

Author Closing Comment

by:niceoneishere
ID: 36582557
Brilliant Thanks
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

762 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