0
Response<BranchDeleteResponse> response = new Response<BranchDeleteResponse>();

// calling Deletion API
response = new ApiManager().InvokeGetHttpClientWithoutRequest<Response<BranchDeleteResponse>>(Baseurl + "api/BranchDelete").Item1;

if (Convert.ToString(response.status) == "SUCCESS" && response.Data.isDataAvailable)
{
    Response<BranchMasterResponse> response2 = new Response<BranchMasterResponse>();

    string str = "select * from branch_master";
    dtaccount = dbobj.GetDataTable(str, null, CommandType.Text);
               
    List<BranchInsertRequestData> brlist = new List<BranchInsertRequestData>();
                
    if (dtaccount.Rows.Count > 0)
    {
        for (int i = 0; i <= dtaccount.Rows.Count - 1; i++)
        {
            // AccountInsertReq accountRequest = new AccountInsertReq();
            BranchInsertRequestData branch_request = new BranchInsertRequestData();
                     
            branch_request.BRANCH_ID = Convert.ToInt32(dtaccount.Rows[i][0]);
            branch_request.BRANCH_NAME = Convert.ToString(dtaccount.Rows[i][1]);
            branch_request.FIRM_ID = Convert.ToInt32(dtaccount.Rows[i][2]);
            branch_request.REGION_ID = Convert.ToInt32(dtaccount.Rows[i][3]);
            branch_request.DISTRICT_ID = Convert.ToInt32(dtaccount.Rows[i][4]);
            branch_request.STATE_ID = Convert.ToInt32(dtaccount.Rows[i][5]);
            branch_request.TRA_DT = Convert.ToDateTime(dtaccount.Rows[i][6]);
            branch_request.UPTO_DATE = Convert.ToChar(dtaccount.Rows[i][7]);
            branch_request.STATUS_ID = Convert.ToInt16(dtaccount.Rows[i][8]);
            branch_request.INAUGURATION_DT = Convert.ToDateTime(dtaccount.Rows[i][9]);
            branch_request.BRANCH_NO = Convert.ToInt32(dtaccount.Rows[i][10]);
            branch_request.INT_WAIVER_APPRD = Convert.ToInt16(dtaccount.Rows[i][11]);
            branch_request.SHIFT_ID = Convert.ToInt32(dtaccount.Rows[i][12]);

            brlist.Add(branch_request);
        }

        BranchMasterRequest br_insertreq = new BranchMasterRequest();
        br_insertreq.branch_InsertReq_Datas = brlist;
    }
}

When control goes to loop at

branch_request.FIRM_ID = Convert.ToInt32(dtaccount.Rows[i][2])

an exception is thrown:

Object cannot be cast from DBNull to other types

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • branch_request.FIRM_ID = (dtaccount.Rows[i][2] == DBNull.Value)? null :Convert.ToInt32(dtaccount.Rows[i][2]) Then you need to change request.FIRM_ID from int to int? – jdweng Oct 07 '20 at 07:22
  • What is the type of `FIRM_ID`? – mjwills Oct 07 '20 at 08:17
  • FIRM_ID type is number(3) – Bhavya Pious Oct 07 '20 at 08:37
  • What is the type of the property in C#? – mjwills Oct 07 '20 at 23:31
  • Error solved by adding nvl check in select query string str = "select nvl(BRANCH_ID,0),nvl(BRANCH_NAME,''),nvl(FIRM_ID,0),nvl(REGION_ID,0),nvl(DISTRICT_ID,0),nvl(STATE_ID,0),nvl(TRA_DT,INAUGURATION_DT),nvl(UPTO_DATE,''),nvl(STATUS_ID,0),nvl(INAUGURATION_DT,TRA_DT),nvl(BRANCH_NO,0),nvl(INT_WAIVER_APPRD,0),nvl(SHIFT_ID,1) from branch_master"; – Bhavya Pious Oct 09 '20 at 06:29

0 Answers0