Χαιρετώ τους συνάδελφους Developers.
Γράφω ένα web application σε ASP.NET 2.0 - C# το οποίο είναι ένα web forum.
H database έχει τα εξής tables:
![]()
(Επειδή η εικόνα με τα tables δεν ανεβαίνει την έχω συνημμένη.)
Αντιμετωπίζω το εξής πρόβλημα:
Θέλω όταν ένα καινούργιο message γράφεται στη βάση να γίνονται τα εξής:
1. Να αυξάνονται οι Replies στο Table Topics κατα ένα.
2. Να αυξάνονται τα Posts στο Table Forums κατα ένα.
Στο δεύτερο ζητούμενο έχω πρόβλήμα.
Αυτός είναι ο κώδικας που εκτελείται όταν ένα καινούριο Message γράφεται στην βάση:
// get the connection string.string cs = WebConfigurationManager.ConnectionStrings["ForumConnectionString"].ConnectionString;
// set up the data objects
string insertMessage = "INSERT Messages (topicid, author, date, message) " +
"VALUES(@topicid, @author, @date, @message);" +
"UPDATE Topics SET replies = replies + 1 WHERE topicid = @topicid;" +
"UPDATE Forums SET posts = posts + 1 WHERE forumid = forumid";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(insertMessage, con);
// get the query strings
string topicid = Request.QueryString["topic"];string forumid = Request.QueryString["forum"];// insert the messagecmd.CommandText = insertMessage;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue(
"topicid", topicid);cmd.Parameters.AddWithValue(
"author", txtEmail.Text);cmd.Parameters.AddWithValue(
"date", DateTime.Now);cmd.Parameters.AddWithValue(
"message", txtMessage.Text);con.Open();
cmd.ExecuteNonQuery();
con.Close();
Το πρόβλημα βρήσκεται στο SQL string insertMessage. Οι δύο πρώτες γραμμές γράφουν το message, η τρίτη αυξάνει τα replies κατα 1. Η τέταρτη γραμμή που θέλω να αυξάνει τα posts για το συγκεκριμμένο forumid, αυξάνει τα posts για όλα τα forums.
Στην αρχή είχα δοκιμάσει αυτό: UPDATE Forums SET posts = posts + 1 WHERE forumid = @forumid αλλά παίρνω το εξής error: Must declare the scalar variable "@forumid".
(Αλήθεια τι είναι τα scalar variables ???)
Οποιαδήποτε πρόταση ή λύση για το θέμα που αντιμετωπίζω είναι ευπρόσδεκτη....