Perché il mio DataTable sempre restituisce “vero / falso”, ma mai una stringa?
Domanda
Sto cercando di utilizzare un DataTable, riempito da un MySqlDataAdapter, che contiene un elenco di commenti per un post di blog. Per alcune ragioni, se il campo "anonimo" è impostato a "1", il campo nome utente è vuoto e deve essere sostituita con una stringa specificata.
Il problema che ho è che ogni volta che cerco di ottenere il valore del campo, ricevo sia "vero" o "falso". Il mio codice è simile al seguente:
DataTable modifiedComments = new DataTable();
// The function GetCommentsById() returns a MySqlDataAdapter with the result of the query
MySqlDataAdapter commentsContainer = Wb.Entry.GetCommentsById(imageId);
commentsContainer.Fill(modifiedComments);
commentsContainer.Dispose();
foreach (DataRow row in modifiedComments.Rows)
{
string status;
// This never returns true, so we always get into the else
if (row["anonymous"] == "1")
{
status = "You are anonymous";
}
else
{
status = "You are not anonymous";
}
}
viewImageCommentsRepeater.DataSource = modifiedComments;
viewImageCommentsRepeater.DataBind();
Soluzione
Il campo è probabilmente un tipo di campo "bit", che mappa a Boolean in ADO.NET
È sufficiente verificare la presenza di vere o false:
if ((bool)row["anonymous"])
...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow