I need to retrieve the table and check if the variable 'verb' exists in either one of the three columns and then pass that specific row's values to a Verb Object, as stated below.
In this code however, the if condition that reads the columns skips through the condition as if the field doesn't exist in the table even though it actually does. Are there any better ways to do this?
public static Verb GetVerbs(string verb)
{
List<string> Verbs = new List<string>();
Verb v = new Verb();
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=QABase;Integrated Security=True");
try
{
conn.Open();
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
}
try
{
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select * from EnglishVerbs", conn);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
if ((myReader["BaseForm"].ToString().ToLower().Contains(verb.ToLower()))
|| (myReader["PastForm"].ToString().ToLower().Contains(verb.ToLower()))
|| (myReader["PastPartForm"].ToString().ToLower().Contains(verb.ToLower())))
{
v.BaseTense = Convert.ToString(myReader["BaseForm"]);
v.PastTense = Convert.ToString(myReader["PastForm"]);
v.PastParticiple = Convert.ToString(myReader["PastPartForm"]);
}
else
{
//row doesnt exist
v.BaseTense = null;
v.PastTense = null;
v.PastParticiple = null;
}
}
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
}
try
{
conn.Close();
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
}
return v;
}