In my code, I make several calls to the database in a row. However, when I try to read the integer value from the database, I get the following error: the input string was not in the correct format. This is my code:
private int getNumberOfProjectsAssigned()
{
ArrayList staffProjects = new ArrayList();
int numberOfProjects = 0;
try
{
string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
MySqlConnection connection = new MySqlConnection(strConnection);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT id_project_pk FROM `test`.`staff_on_project` WHERE id_staff_pk = " + Convert.ToInt32(Session["CurrentUserID"]);
connection.Open();
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int projectId = Convert.ToInt32(reader["id_project_pk"].ToString());
staffProjects.Add(projectId);
}
connection.Close();
foreach (int i in staffProjects)
{
command.CommandText = "SELECT still_active FROM `test`.`projects` WHERE idprojects = " + i;
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
int projectId = Convert.ToInt32(reader["still_active"].ToString());
if(projectId == 1)
{
projectsStaffWorksOn.Add(projectId);
}
numberOfProjects = projectsStaffWorksOn.Count;
}
connection.Close();
}
}
catch { }
return numberOfProjects;
}
It throws an error at the point marked in the code. Any help would be greatly appreciated!
source
share