-2

I am new to React and new to Web API. I am uploading data in a tabulator in react front end from the value that I am passing through the web API. I am passing value through the getReports function like this:

[HttpPost]
[Route("GetReports")]
public IHttpActionResult GetReports(string jwt, List<object> data)
{
    if (!Common.VerificationToken.VerifyJWToken(jwt))
    {
        return null;
    }
    var to = data[0];
    var from = data[1];
    DateTime toDate = Convert.ToDateTime(to);
    DateTime fromDate = Convert.ToDateTime(from);

    var ReportData = db.T_CQL_COIL_DESC.Where(t => t.CCD_CREATED_ON >= toDate && 
        t.CCD_CREATED_ON <= fromDate).ToList();




    ReportsDTO dto = new ReportsDTO();
    List<ReportsDTO> ReportDTO = new List<ReportsDTO>();
    try
    {
        foreach (var report in ReportData)
        {
            List<vehicledetail> vehicle = new List<vehicledetail>();
            var imgurl = "https://firebasestorage.googleapis.com/v0/b/tsl-coil-qlty- 
            monitoring-dev.appspot.com/";
            dto = new ReportsDTO();
            dto.Type = report.CCD_OP_TYPE;
            dto.ID = report.CCD_COIL_ID;
            vehicle = GetVehicleID(dto.ID);
            vehicledetail vehicledetails = vehicle[0];
            dto.vehicleno = vehicledetails.vehicleno.ToString();
            dto.wagonno = vehicledetails.wagonno.ToString();
            dto.Active = report.CCD_ACTIVE;
            dto.ImgURL = report.CCD_IMAGE_URL != null ? imgurl + report.CCD_IMAGE_URL : "NA";
            dto.Desc = report.CCD_VIEW_DESC != null ? report.CCD_VIEW_DESC : "NA";
            ReportDTO.Add(dto);
        }

        return Ok(ReportDTO);
    }
    catch (Exception ex)
    {
        return Content(HttpStatusCode.NoContent, "Something went wrong");
    }


}

The data in vehicledetail in vehicledetail vehicledetails = vehicle[0]; is getting populated from this function

public List<vehicledetail> GetVehicleID(string coilID)
{
    List<vehicledetail> vehicle = new List<vehicledetail>();
    vehicledetail vehicledetails = new vehicledetail();

    string oradb = Utilities.STAR_DB;
    OracleConnection conn = new OracleConnection(oradb);


    string query = "SELECT a.Vbeln, b.WAGON_NO FROM sapr3.lips a, sapr3.ZVTRRDA b WHERE 
    a.MANDT='600' AND a.CHARG='" + coilID + "' AND a.LFIMG > 0 AND a.MANDT = b.MANDT AND 
    a.VBELN = b.VBELN";


    OracleDataAdapter da = new OracleDataAdapter(query, conn);
    conn.Open();
    DataTable dt = new DataTable();
    da.Fill(dt);

    foreach (DataRow row in dt.Rows)
    {

        vehicledetails.vehicleno = row["VBELN"].ToString();
        vehicledetails.wagonno = row["WAGON_NO"].ToString();
    }

    conn.Close();
    vehicle.Add(vehicledetails);

    return (vehicle);

}

It is working fine except when the value of vehicleno or wagonno is null

dto.vehicleno = vehicledetails.vehicleno.ToString();
dto.wagonno = vehicledetails.wagonno.ToString();

I wanted to know how to handle the null exception for this value in the code. What is the standard way?

Please help

zaggler
  • 7,254
  • 6
  • 26
  • 47
sambit
  • 33
  • 6
  • `dto.vehicleno = vehicledetails?.vehicleno?.ToString();` and `dto.wagonno = vehicledetails?.wagonno?.ToString();` but then `vehicleno` and `wagonno` could be `null`. It fixes your issue, but it depends on if you would want them possibly to still be `null`. TBH I would change your select – zaggler May 28 '21 at 16:21
  • can you explain the code please? – sambit May 28 '21 at 16:22
  • You could also change your select to check for null, if so, return an empty string. You can also check the value when you are trying to assign it in the loop... many ways this could be done. – zaggler May 28 '21 at 16:27
  • The only time those would be null is when `dt.Rows` is empty. Above the `foreach` add `vehicledetails.vehicleno = ""; vehicledetails.wagonno = "";`, then they will be set to something other than null at all times. – Heretic Monkey May 28 '21 at 16:30
  • @HereticMonkey or default them in the class as empty strings... – zaggler May 28 '21 at 16:31
  • 1
    @zaggler Or just remove the `.ToString()` altogether, since they must be typed as strings already. – Heretic Monkey May 28 '21 at 16:32
  • 1
    @HereticMonkey yes, more than a few ways to do it. – zaggler May 28 '21 at 16:33
  • You need to post ReportsDto Class. Is vehicleno can be null? – Serge May 28 '21 at 16:33
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Heretic Monkey May 28 '21 at 16:34

2 Answers2

0

try this

dto.vehicleno = vehicledetails.vehicleno==null? string.Empty:vehicledetails.vehicleno .ToString();
dto.wagonno = vehicledetails.wagonno==null? string.Empty:vehicledetails.wagonno.ToString();
Serge
  • 7,259
  • 2
  • 7
  • 21
0

If you are using C# 6 or newer, you can use a null-conditional operator

So, something like the following:

dto.vehicleno = vehicledetails.vehicleno?.ToString();

In short, the logic is that if the vehicleno is null, it won't call the ToString method, and the whole expression will return null. Otherwise, it will call the ToString method as normal.

Jose Nuno
  • 507
  • 2
  • 8