Reputation: 782
I have a table called Town which holds a relation between player and a players buildings. The table has the these Columns: userid (int) buildingid (int) number (int)
I have successfully inserted a new building for a player and now i have trouble updating the specific row for a player.
I have this code:
if (!existPlayerBuilding(userid, buildingid))
{
SqlConnection sqlcon = new SqlConnection(connectionString);
string query = "INSERT INTO Town VALUES(@userid, @buildingid, @number)";
SqlCommand sqlcom = new SqlCommand(query, sqlcon);
sqlcom.Parameters.Add("@userid", userid);
sqlcom.Parameters.Add("@buildingid", buildingid);
sqlcom.Parameters.Add("@number", number);
try
{
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
}
catch (Exception i)
{
lblTestlabel.Text = "SQL FEILET";
}
lblTestlabel.Text = "Bygningen ble kjøpt!!!!";
}
else
{
SqlConnection sqlcon = new SqlConnection(connectionString);
string query = "UPDATE Town VALUES(@userid, @buildingid, @number) WHERE userid = @userid AND buildingid = @buildingid";
SqlCommand sqlcom = new SqlCommand(query, sqlcon);
sqlcom.Parameters.Add("@userid", userid);
sqlcom.Parameters.Add("@buildingid", buildingid);
sqlcom.Parameters.Add("@number", number);
try
{
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
}
catch (Exception i)
{
lblTestlabel.Text = "SQL FEILET";
}
lblTestlabel.Text = "Bygningen ble kjøpt!!!!";
}
The existPlayerBuilding method returns true if a player already has a building with the given id and false otherwise. When i run and test if i can update a number for a users building nothing happens.
Upvotes: 3
Views: 577
Reputation: 70718
Your Update Query is wrong:
UPDATE Town
SET number = @number
WHERE userid = @userid AND buildingid = @buildingid
Upvotes: 6
Reputation: 136074
The syntax for an Update in SQL would be
UPDATE Town
SET number = @number
WHERE userid = @userid AND buildingid = @buildingid
Upvotes: 11