I want the statement update the database by 1 each time I enter a specific page..But it seems to update it by 3 🙁
public static void UpdateThreadView(int threadID)
{
StringBuilder sb = new StringBuilder();
sb.Append("UPDATE dbo.Threads");
sb.Append(" SET Views=(Views+1)");
sb.Append(" WHERE ThreadsID=@ThreadID");
string myConnectionString = AllQuestionsPresented.connectionString;
using (SqlConnection myConnection = new SqlConnection(myConnectionString))
{
myConnection.Open();
SqlCommand sqlCommand = new SqlCommand(sb.ToString(), myConnection);
sqlCommand.Parameters.Add("@ThreadID", SqlDbType.Int);
sqlCommand.Parameters["@ThreadID"].Value = threadID;
sqlCommand.ExecuteNonQuery();
}
}
Set a breakpoint in your code and run in the visual studio debugger.
I’m betting this gets called 3 times due to a combination of postback and events.
Alternatively you could use SQL Profiler to see if multiple updates happen.