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 are encountering, "Incorrect Syntax near publishedAt," is likely due to a typo in your SQL command where you are missing a closing parenthesis. In your SQL command, you need to close the list of columns and the list of values being inserted. Here's the corrected line of your SQL command:
Notice the additional closing parenthesis at the end of the line. This change should resolve the syntax error near "publishedAt" when executing the `cmd.ExecuteNonQuery()`.
By fixing the syntax in your SQL command, you ensure that all columns and values are properly enclosed and matched, preventing the syntax error during the database query execution.
Please make this correction in your code, and it should help you overcome the syntax error you are currently facing. If you have any more questions or need further assistance, feel free to ask!