0

I want to add 35 days to endA and store it in string AA and use that AA in my if statement to compare today's date and date in AA But in the line AA = Convert.ToDateTime(enddateA).AddDays(35); I get an error saying Cannot Convert type System.DateTime to String it works if I do it through var A = Convert.ToDateTime(enddateA).AddDays(35); but than i cannot use it out of the scope.

string enddateA = "";
string dateNowA = "";
string AA = "";

string UPDATE_COMPLETE = String.Format("SELECT [FINAL_END_DATE] FROM [Campus6_convert].[dbo].[ACADEMICCALENDAR] where ACADEMIC_YEAR = '" + year + "'  and ACADEMIC_TERM='" + academicterm + "'  and  ACADEMIC_SESSION='A'");
SqlCommand cmd = new SqlCommand(UPDATE_COMPLETE, con);

myReader = cmd.ExecuteReader();

while (myReader.Read())
{
    enddateA = (myReader["FINAL_END_DATE"].ToString());
    AA = Convert.ToDateTime(enddateA).AddDays(35);

    dateNowA = DateTime.Now.ToString();

    Console.WriteLine("" + AA + "");
}
myReader.Close();
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();

if ( AA  == dateNowA)
{
    using (SqlConnection coe = new SqlConnection("server=fcpcdb02; database=campus6_convert; user id=""; password="";"))
    {
        coe.Open();

        string UPDATE_COMPLETEE = String.Format("UPDATE [Campus6_convert].[dbo].[TRANSCRIPTDETAIL] SET FINAL_GRADE = 'F', REVISION_OPID='WFLOW', REVISION_DATE='" + revisiondate + "', REVISION_TIME='" + currentDateString + "' where FINAL_GRADE='I' and ACADEMIC_YEAR = '" + year + "'  and ACADEMIC_TERM='" + academicterm + "'  and  ACADEMIC_SESSION='A'");
        SqlCommand cd = new SqlCommand(UPDATE_COMPLETEE, coe);
        cd.ExecuteNonQuery();
        cd.Dispose();
        coe.Close();
    }    
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
Naive
  • 345
  • 2
  • 6
  • 18
  • What is the dataType of FINAL_END_DATE in the database? – hardkoded May 31 '17 at 17:13
  • `AA` is a string and you're saying `Convert.ToDateTime(enddateA).AddDays(35);` which returns a date of course. Either declare AA as a `DateTime` or call `.AddDays(35).ToString()` – aquinas May 31 '17 at 17:18
  • And you have a SQL injection in your code. https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work – aquinas May 31 '17 at 17:20

2 Answers2

4

You miss the command ToString() to your addDays(35):

AA = Convert.ToDateTime(enddateA).AddDays(35).ToString()

BTW: What is the type of FINAL_END_DATE?

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
Bestter
  • 877
  • 1
  • 12
  • 29
2

Your AA variable is declared as "string", the command "Convert.ToDateTime(enddateA).AddDays(35)" will return a "DateTime" instance, that's why you cannot assign it to the AA variable

You can declare another "DateTime" variable, like you did with "var A = (...)" or you can convert it back to string with "Convert.ToDateTime(enddateA).AddDays(35)"

Pedro Drewanz
  • 1,242
  • 13
  • 14