My goal is to develop a Web API in .NET without using MVC that can store and retrieve data from an SQL database. The aim is to then utilize this API in another web application by leveraging JavaScript. The data to be stored will include fields for Id, comment, date, and name.
While attempting to implement the functionality in the Repository class, I encountered issues with the following code:
public string insertJsonData(JsonInfo json)
{
string message;
SqlConnection connection = null;
string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("INSERT INTO JsonDataTable(JsonValue, CommentDate, ModelName) VALUES(@JsonValue, @CommentDate, @ModelName)", connection);
connection.Open();
command.Parameters.AddWithValue("@JsonValue", json.JsonData);
command.Parameters.AddWithValue("@CommentDate", json.CommentDate);
command.Parameters.AddWithValue("@ModelName", json.ModelName);
int executionResult = command.ExecuteNonQuery();
if (executionResult == 1)
{
message = json.JsonData + " details inserted successfully";
}
else
{
message = json.JsonData + " details not inserted successfully";
}
connection.Close();
return message;
}