locked
Trying to Show a Date from a db Table RRS feed

  • Question

  • User-292646819 posted

    Hi,

    I have a record in a database that needs to be edited on an edit page. In order to edit it I get the data from the database and display it in the value fields. So far so good, but the StartDate and EndDate from the database come out blank. I know the values are in the database but cannot assign them to a variable to display on the page.

            
            //tried
            //DateTime startDate;
            //DateTime endDate
            var startDate = "";
            var endDate = "";
    
    
            var coursedata = db.QuerySingle("SELECT * FROM DataCourses WHERE CourseA = @0", courseA);
            
            courseA = coursedata.CourseA;
            
            //tried :
            //startDate.ToString(coursedata.StartDate);
            //endDate = coursedata.endDate.ToString("dd/mm/YYYY");
            
            startDate = coursedata.startDate;
            endDate = coursedata.endDate;
    
            venue = coursedata.Venue;
            details = coursedata.Details;   

    I'm trying to display them as @startDate and @endDate

    I can see they are blank using a breakpoint.

    Any help appreciated.

    Thanks,

    K

    Tuesday, February 24, 2015 8:53 AM

Answers

  • User1713851783 posted

    There is something strange in your code.

    The only statement that shouldn't give errors is

    endDate = coursedata.endDate.ToString("dd/mm/YYYY");

    but it too isn't right. Its correct form is

    endDate = coursedata.endDate.ToString("dd/MM/yyyy");

    I don't understand why you don't receive server errors like "Cannot implicitly convert type 'System.DateTime' to 'string'".

    Try to replace

    startDate = coursedata.startDate;
    endDate = coursedata.endDate;
    
    

    with

    startDate = DateTime.Now.ToString("dd/MM/yyyy");
    endDate = DateTime.Now.ToString("dd/MM/yyyy");

    and look if your fields display today's date.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 24, 2015 9:59 AM

All replies

  • User1577371250 posted

    Hi,

    Check Date values are present in cousedata object.

    if(coursedata != null)
    {
    var startDate = coursedata.startDate; // it should be startDate if am not wrong var endDate = coursedata.endDate;
    }
    Tuesday, February 24, 2015 9:00 AM
  • User-292646819 posted

    Hi, 

    Thanks for the reply - coursedata is not null as the other fields show on the page fine, just startDate and endDate that don't.

    (You're right about startDate, my typing is off.)

    Keith

    Tuesday, February 24, 2015 9:14 AM
  • User1713851783 posted

    There is something strange in your code.

    The only statement that shouldn't give errors is

    endDate = coursedata.endDate.ToString("dd/mm/YYYY");

    but it too isn't right. Its correct form is

    endDate = coursedata.endDate.ToString("dd/MM/yyyy");

    I don't understand why you don't receive server errors like "Cannot implicitly convert type 'System.DateTime' to 'string'".

    Try to replace

    startDate = coursedata.startDate;
    endDate = coursedata.endDate;
    
    

    with

    startDate = DateTime.Now.ToString("dd/MM/yyyy");
    endDate = DateTime.Now.ToString("dd/MM/yyyy");

    and look if your fields display today's date.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 24, 2015 9:59 AM
  • User-292646819 posted

    Hi,

    Thanks for your help, when changed to the correct form it was still not initially displaying. I had to also change :

    <input type="date" />
    TO
    <input type="text" />

    To display the value.

    Thanks for your help.

    K

    Tuesday, February 24, 2015 11:36 AM
  • User692055318 posted

    You could always just through a

    @startDate

    To see what is being passed.

    Tuesday, February 24, 2015 1:27 PM