0

I have created a custom class list which is filled from the result of a query. Specifically, me query returns (int, timestamp) 2 columns -> 2 values.

public class SucessfulCompletion
        {
            public int Result;
            public string Timestampvalue;

            public SucessfulCompletion(int result, string timestamp) => (Result, Timestampvalue) = (result, timestamp);
        }

        public List<SucessfulCompletion> SuccessfulCalculationsTimestamp(string connectionstring)
        {
            List<SucessfulCompletion> QueryListResult = new List<SucessfulCompletion>();

            using (SqlConnection sqlConnection = new SqlConnection(connectionstring))
            {
                var query_table_timestamp = (
                    @"SELECT CASE 
                                WHEN t.STATUS = 'SUCCESS' AND t.STATUS_DESCRIPTION = 'ALL QUERIES COMPLETED SUCCESSFULLY' THEN 1
                                ELSE 0
                             END SuccessfulCompletion, t.TIMESTAMP
                    FROM (SELECT TOP (1) l.TIMESTAMP, l.STATUS, l.STATUS_DESCRIPTION
                          FROM LOG_DETAILS l
                          ORDER BY 1 DESC) t");

                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand(query_table_timestamp, sqlConnection))
                {
                    using (SqlDataReader reader = sqlCommand.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            QueryListResult.AddRange(new List<SucessfulCompletion>
                            {
                                new SucessfulCompletion(reader.GetInt32(0), reader.GetDateTime(1).ToString())
                            });
                        }
                        reader.Close();
                    }
                    sqlCommand.Cancel();
                }
            }
            return QueryListResult;
        }

The code to create the custom class list was taken from this SO question

So the QueryListResult would be like [1, "2020-10-04 HH:MM:SS"]

Now I want to make an if statement to check if the first index of the QueryListResult is ether 0 or 1.

List<SucessfulCompletion> reportsucessfulcompletion = new List<SucessfulCompletion>();
reportsucessfulcompletion = SuccessfulCalculationsTimestamp(SQLServerConnectionDetails());

if (reportsucessfulcompletion[0]=1) //my problem is here
{
    //Enable is only if successful
    PreviewCalculationsButton.IsEnabled = true;
    PreviewReportButton.IsEnabled = true;

    //add textbox of success
    SQLSuccessfulTextCalculations.Text = String.Format("Completed On: {0}", reportsucessfulcompletion[1]);
}
else
{
    //add textbox of fail
    SQLFailedTextCalculations.Text = String.Format("Failed On: {0}", reportsucessfulcompletion[1]);
}

In the if statement I get an error

Cannot implicitly convert type 'int' to 'TestEnvironment.MainWindow.SucessfulCompletion'

I know it may be a silly question for someone experienced with C# but I am a newbie, so I would appreciate your help. Please inform me in the comments if the theme of the question is a duplicate one I will close the question.

NikSp
  • 1,262
  • 2
  • 19
  • 42

1 Answers1

1

You're comparing an jnstance of your class to a number.

These are different things.

And one equal sign sets rather than compares

Try

  If ( reportsucessfulcompletion[0].Result == 1)

You should make these properties rather than variables.

I also recommend Dapper as a "micro" orm very close to the ado metal, but which saves a fair bit of coding whilst implementing best practice for you.

Andy
  • 11,864
  • 2
  • 17
  • 20
  • yeah the equal sign was missed by me I know the difference. Thnx a lot for the answer. Apologize if this was relatively obvious – NikSp Oct 04 '20 at 14:25
  • You're welcome. It's only obvious once you know it. – Andy Oct 04 '20 at 14:34