I am getting error during fetching data from Sqlite database. (Object reference not set to the instance of the object).
To fetch data I am using
var listStorageListData = await dbConn.Table<StorageList>().ToListAsync();
Scenario is when application start, I am checking that data is available in sqlite table or not (Table name is "StorageList"). If data is available then it directly redirect to home page otherwise it redirect to login page. from login page I am storing data in sqlite db. (don't know how to implement Isolated storage for storing persistent data that's why using local DB).
Login Page (From below service call Save method)
private async Task CallWebServiceForUserProfile(string EmpCode)
{
try
{
var response = await ServiceLayer.GetResponseFromWebService.GetResponse<ServiceLayer.ServiceClasses.Profile_RootObject>
(ServiceLayer.ServiceURL.UserProfile + "UserId=" + EmpCode);
if (response != null)
{
if (response.Flag == true)
{
objProfile = new Profile();
objProfile.BranchCd = response.PrInfoList[0].BranchCd != null ? response.PrInfoList[0].BranchCd : string.Empty; ;
objProfile.CLBalance = response.PrInfoList[0].CLBalance != null ? response.PrInfoList[0].CLBalance : string.Empty;
objProfile.COFFBalance = response.PrInfoList[0].COFFBalance != null ? response.PrInfoList[0].COFFBalance : string.Empty;
objProfile.Designation = response.PrInfoList[0].Designation != null ? response.PrInfoList[0].Designation : string.Empty;
objProfile.ELBalance = response.PrInfoList[0].ELBalance != null ? response.PrInfoList[0].ELBalance : string.Empty;
objProfile.EmpCode = response.PrInfoList[0].EmpCode != null ? response.PrInfoList[0].EmpCode : string.Empty;
objProfile.EmpName = response.PrInfoList[0].EmpName != null ? response.PrInfoList[0].EmpName : string.Empty;
objProfile.Grade = response.PrInfoList[0].Grade != null ? response.PrInfoList[0].Grade : string.Empty;
objProfile.HOD = response.PrInfoList[0].HOD != null ? response.PrInfoList[0].HOD : string.Empty;
objProfile.HPLBalance = response.PrInfoList[0].HPLBalance != null ? response.PrInfoList[0].HPLBalance : string.Empty;
objProfile.LocCd = response.PrInfoList[0].LocCd != null ? response.PrInfoList[0].LocCd : string.Empty;
objProfile.ReportingTo = response.PrInfoList[0].ReportingTo != null ? response.PrInfoList[0].ReportingTo : string.Empty;
objProfile.Unit = response.PrInfoList[0].Unit != null ? response.PrInfoList[0].CLBalance : string.Empty;
objProfile.Photo = response.PrInfoList[0].Photo != null ? response.PrInfoList[0].Photo : null;
objProfile.Join_Dt = response.PrInfoList[0].Join_Dt != null ? response.PrInfoList[0].Join_Dt : string.Empty;
App.MyEmployeeId = objProfile.EmpCode;
TotalInsertedData=await App.repoStorage.Save(objProfile);
}
else if (response.Flag == false)
{
await DisplayAlert(AppResources.LError, response.Message, "OK");
}
}
else
{
await DisplayAlert(AppResources.LError, AppResources.LConnectionError, "OK");
}
}
catch (WebException Exception)
{
await DisplayAlert(AppResources.LError, AppResources.LConnectionError, "OK");
}
}
To Save Data (From below method call GetAllData Function)
public async Task<int> Save(Profile objProfile)
{
int totalinserteddata = 0;
if (objProfile != null)
{
await this.GetAllData();
if (objProfile != null)
{
if (objProfile.Photo != null)
{
pic = new byte[objProfile.Photo.Count];
for (int j = 0; j < objProfile.Photo.Count; j++)
{
byte pic1 = IntToByte(objProfile.Photo.ElementAt(j));
pic[j] = pic1;
}
}
}
if (totalCountData == 0)
{
totalinserteddata = await dbConn.InsertAsync(
new StorageList
{
EmpCode = objProfile.EmpCode,
EmpName = objProfile.EmpName,
Designation = objProfile.Designation,
Join_Dt = objProfile.Join_Dt,
Photo = pic
});
}
else
{
totalinserteddata = await dbConn.UpdateAsync(
new StorageList
{
EmpCode = objProfile.EmpCode,
EmpName = objProfile.EmpName,
Designation = objProfile.Designation,
Join_Dt = objProfile.Join_Dt,
Photo = pic
});
}
}
return totalinserteddata;
}
GetAllData Function
public async Task<List<StorageList>> GetAllData()
{
try
{
var listStorageListData = await dbConn.Table<StorageList>().ToListAsync();
if (listStorageListData != null)
{
this.totalCountData = listStorageListData.Count;
}
else
{
this.totalCountData = 0;
}
return listStorageListData;
}
catch (Exception e)
{
return null;
}
}
Problem is : When My flow is comes from Login page (as mention above) it workes perfectly) but When I call GetAllData Function from App.cs then below line
List listOfStorageData = await repoStorage.GetAllData();
showing me error "Object reference not set to the instance of an object". Use OnStart() method to check Application is start with login page or Home page.
protected async override void OnStart()
{
// Handle when your app Start
#region Profile Data in Sqlite
List<StorageList> listOfStorageData = await repoStorage.GetAllData();
if (listOfStorageData != null)
{
if (listOfStorageData.Count > 0)
{
MainPage = new SideMenu();
}
else
{
MainPage = new NavigationPage(new LoginPage());
}
}
else
{
MainPage = new NavigationPage(new LoginPage());
}
#endregion
//MainPage = new NavigationPage(new LoginPage());
}
This is "StorageList" Sqlite table.
[Table("StorageList")]
public class StorageList
{
[PrimaryKey, AutoIncrement]
public int StorageId { get; set; }
[MaxLength(250), Unique,NotNull]
public string EmpCode { get; set; }
[MaxLength(250),NotNull]
public string EmpName { get; set; }
[NotNull]
public string Designation { get; set; }
[NotNull]
public string Join_Dt { get; set; }
public byte[] Photo { get; set; }
}