i am trying to send data to the db but i keep running into this exception
i tried some logging to see whether the data send was incorrect but that was not the case.
i also tried asking chatgbt to no luck obv.
please help me with this
public bool SaveProfile(int userId, string bio, string region, string country, string platform, Dictionary<string, int> checkboxStatesAndValues)
{
try
{
// Retrieve the user ID using the provided username
using (MySqlConnection connection = ConnectionString.GetConnection())
{
connection.Open();
string query = "INSERT INTO Profile (UserID, Bio, Region, Country, Platform, Fun, Competitive, Serious, Communication, Dedication) VALUES (@UserID, @Bio, @Region, @Country, @Platform, @Fun, @Competitive, @Serious, @Communication, @Dedication)";
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.Parameters.AddWithValue("@UserID", User.UserId);
cmd.Parameters.AddWithValue("@Bio", bio);
cmd.Parameters.AddWithValue("@Region", region);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Parameters.AddWithValue("@Platform", platform);
// Set checkbox values for each category
foreach (string category in new[] { "Fun", "Competitive", "Serious", "Communication", "Dedication" })
{
for (int i = 1; i <= 5; i++)
{
string checkboxName = $"{category}{i}";
string paramName = $"@{category}";
// Check if the checkboxName exists in checkboxStatesAndValues
if (checkboxStatesAndValues.ContainsKey(checkboxName))
{
// Add the parameter to the command
cmd.Parameters.AddWithValue(paramName, checkboxStatesAndValues[checkboxName]);
}
else
{
// If the checkboxName does not exist, add DBNull.Value
cmd.Parameters.AddWithValue(paramName, DBNull.Value);
}
}
}
int rowsAffected = cmd.ExecuteNonQuery();
return rowsAffected > 0;
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
return false;
}
}
New contributor
Kealan Thal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.