HI
I am getting error - Incorrect Syntax near publishedAt at cmd.ExecuteNonQuery();
private void InsertVideoDetails(string VideoId, string VideoTitle, string Description, int ViewCount, string LikeCount, string CommentCount, DateTime PublishedAt)
{
string sql = "INSERT INTO Videos (VideoId, VideoTitle, Description,LikeCount, ViewCount,CommentCount,Published_At) VALUES (@VideoId, @VideoTitle, @Description,@LikeCount, @ViewCount,@CommentCount,@PublishedAt";
string constring = ConfigurationManager.ConnectionStrings["Cnn"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(sql))
{
cmd.Parameters.AddWithValue("@VideoId", VideoId);
cmd.Parameters.AddWithValue("@VideoTitle", VideoTitle);
cmd.Parameters.AddWithValue("@Description", Description);
cmd.Parameters.AddWithValue("@LikeCount", LikeCount);
cmd.Parameters.AddWithValue("@ViewCount", ViewCount);
cmd.Parameters.AddWithValue("@CommentCount", CommentCount);
cmd.Parameters.AddWithValue("@PublishedAt", PublishedAt);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Thanks
Eliana BlakePosted Mar 22, 2025, 3:28 PM
The error you're encountering, "Incorrect Syntax near publishedAt," is likely due to a syntax error in your SQL query. In the SQL query within your C# code snippet, there seems to be a missing closing parenthesis in the VALUES section of your INSERT INTO statement. This missing parenthesis causes the syntax error when the query is executed.
To address this issue, you should correct the SQL query by adding the closing parenthesis after @PublishedAt in your command. Here's the corrected line of code:
By adding the closing parenthesis in the VALUES section of your SQL query, you should be able to execute the command without encountering the "Incorrect Syntax near publishedAt" error.
Once you make this correction, your command should function correctly, and you should be able to insert the video details into your database without any issues. If you have any further questions or need additional clarification, feel free to ask!