C# Error : NullReferenceException was unhandled by user code - c#

I am developing a payroll add-on for SAP business 1. I keep getting an error:
"NullReferenceException was unhandled by user code :Object reference not set to an instance of an object." When I try to select a combobox item that is embedded in a SAP matrix column cell.
My Code:
public void HandleMenuEvent(ref SAPbouiCOM.MenuEvent pVal)
{
// Handle Add Menu
if (pVal.MenuUID == "1282")
{
_form.Freeze(true);
oMatrix.AddRow();
_edCode.ValueEx = string.Empty;
_cmbEDDescription = oMatrix.Columns.Item("EDDesc").Cells.Item(oMatrix.RowCount).Specific;
var earnDeductDescription = Program.Kernel.Get().GetAllEarnDeductMasters().Distinct();
if (_cmbEDDescription.ValidValues.Count > 0)
{
// Do nothing
}
else
{
foreach (var item in earnDeductDescription)
{
_cmbEDDescription.ValidValues.Add(item.U_PD_description, string.Empty);
}
}
_cmbEDDescription.Select(0, SAPbouiCOM.BoSearchKey.psk_Index);
var edDescValue = string.Empty;
edDescValue = _cmbEDDescription.Value;
var edCode = earnDeductDescription.Where(x => x.U_PD_description.Trim() == edDescValue.Trim()).Select(y => y.U_PD_code).SingleOrDefault();
for (int i = 1; i
The error occurs on the item changed event
#region ItemChanged
if (pVal.ItemChanged && pVal.ColUID == "EDDesc" && pVal.Before_Action == false)
{
var earnDeductDescription = Program.Kernel.Get().GetAllEarnDeductMasters().Distinct();
var edDescValue = string.Empty;
edDescValue = _cmbEDDescription.Selected.Value; x.U_PD_description.Trim() == edDescValue.Trim()).Select(y => y.U_PD_code).SingleOrDefault();
for (int i = 1; i
This is where I attach a user data source to the SAP column
private void BindMatrixToUserDataSource()
{
// Get main matrix
oItem = _form.Items.Item("JournalMat");
oMatrix = oItem.Specific;
_edDescription = _form.DataSources.UserDataSources.Add("EDDesc", SAPbouiCOM.BoDataType.dt_SHORT_TEXT, 30);
oColumns = oMatrix.Columns;
_coledDescription = oColumns.Item("EDDesc");
_coledDescription.DataBind.SetBound(true, "", "EDDesc");
...some code
}
Can anyone help me solve this?

My suggestion is that _cmbEDDescription.Selected is null at that moment, because no item is selected in the ComboBox. You might change your code like that:
var edDescValue = _cmbEDDescription.Selected == null ? string.Empty : _cmbEDDescription.Selected.Value;

Related

Trying to get data from wsdl using c#

I have this c# code where i am trying to get data from a wsdl in visual studio .
The code is okay but when i try to get the data to written on notepad it just shows an empty space :
WindowsService1.ServiceReference1.GetModifiedBookingsOperationResponse getModbkgsResp;
using (var proxy = new WindowsService1.ServiceReference1.InventoryServiceClient())
{
int noofBookings = 1;
getModbkgsResp = proxy.GetModifiedBookings(getModBkgsReq);
WindowsService1.ServiceReference1.Booking[] bookings = new WindowsService1.ServiceReference1.Booking[noofBookings];
getModbkgsResp.Bookings = new WindowsService1.ServiceReference1.Booking[noofBookings];
getModbkgsResp.Bookings = bookings;
if (getModbkgsResp.Bookings != null)
{
for (int i = 0; i < bookings.Length; i++)
{
Booking bk = new WindowsService1.ServiceReference1.Booking();
getModbkgsResp.Bookings[i] = bk;
if (bk != null )
{
bookingSource = bk.BookingSource;
if (bk.BookingId == Bookingcode)
{
this.WriteToFile("Booking Source =" + bookingSource + "");
}
else
{
this.WriteToFile("Sorry could not find your source of booking");
}
}
else
{
this.WriteToFile("Looks like source is null " );
}
}
}
else
{
this.WriteToFile("ERROR: Booking details not returned from GetModifiedBookings! " +StartDate);
}
}
I'm not sure why you are using the new keyword to create items that should have been retrieved from the service. Naturally anything created with new will be initialized with default values and will not contain any data that was retrieved from the service.
My guess is your code should look more like this:
using (var proxy = new WindowsService1.ServiceReference1.InventoryServiceClient())
{
var response = proxy.GetModifiedBookings(getModBkgsReq);
if (response.Bookings == null)
{
this.WriteToFile("ERROR: Booking details not returned from GetModifiedBookings! " +StartDate);
return;
}
var booking = response.Bookings.SingleOrDefault( b => b.BookingId == bookingCode);
if (booking == null)
{
this.WriteToFile("Sorry could not find your source of booking");
return;
}
var bookingSource = booking.BookingSource;
this.WriteToFile("Booking Source =" + bookingSource + "");
}

Getting The Wait Operation Timeout Exception for a query from Skip Value 100

I am writing a small data migration tools from one big database to another small database. All of the others data migration method worked satisfactorily, but the following method has given an exception from the SKIP VALUE IS 100. I run this console script remotely as well as inside of the source server also. I tried in many different was to find the actual problem what it is. After then I found that only from the SKIP VALUE IS 100 it is not working for any TAKE 1,2,3,4,5 or ....
Dear expertise, I don't have any prior knowledge on that type of problem. Any kind of suggestions or comments is appreciatable to resolve this problem. Thanks for you time.
I know this code is not clean and the method is too long. I just tried solve this by adding some line of extra code. Because the problem solving is my main concern. I just copy past the last edited method.
In shot the problem I can illustrate with this following two line
var temp = queryable.Skip(90).Take(10).ToList(); //no exception
var temp = queryable.Skip(100).Take(10).ToList(); getting exception
private static void ImporterDataMigrateToRmgDb(SourceDBEntities sourceDb, RmgDbContext rmgDb)
{
int skip = 0;
int take = 10;
int count = sourceDb.FormAs.Where(x=> x.FormAStateId == 8).GroupBy(x=> x.ImporterName).Count();
Console.WriteLine("Total Possible Importer: " + count);
for (int i = 0; i < count/take; i++)
{
IOrderedQueryable<FormA> queryable = sourceDb.FormAs.Where(x => x.FormAStateId == 8).OrderBy(x => x.ImporterName);
List<IGrouping<string, FormA>> list;
try
{
list = queryable.Skip(skip).Take(take).GroupBy(x => x.ImporterName).ToList();
//this line is getting timeout exception from the skip value of 100.
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
sourceDb.Dispose();
rmgDb.Dispose();
sourceDb = new SourceDBEntities();
rmgDb = new RmgDbContext();
skip += take;
continue;
}
if (list.Count > 0)
{
foreach (var l in list)
{
List<FormA> formAs = l.ToList();
FormA formA = formAs.FirstOrDefault();
if (formA == null) continue;
Importer importer = formA.ConvertToRmgImporterFromFormA();
Console.WriteLine(formA.FormANo + " " + importer.Name);
var importers = rmgDb.Importers.Where(x => x.Name.ToLower() == importer.Name.ToLower()).ToList();
//bool any = rmgDb.Importers.Any(x => x.Name.ToLower() == formA.ImporterName.ToLower());
if (importers.Count() == 1)
{
foreach (var imp in importers)
{
Importer entity = rmgDb.Importers.Find(imp.Id);
entity.Country = importer.Country;
entity.TotalImportedAmountInUsd = importer.TotalImportedAmountInUsd;
rmgDb.Entry(entity).State = EntityState.Modified;
}
}
else
{
rmgDb.Importers.Add(importer);
}
rmgDb.SaveChanges();
Console.WriteLine(importer.Name);
}
}
skip += take;
}
Console.WriteLine("Importer Data Migration Completed");
}
I have fixed my problem by modifying following code
var queryable =
sourceDb.FormAs.Where(x => x.FormAStateId == 8)
.Select(x => new Adapters.ImporterBindingModel()
{
Id = Guid.NewGuid().ToString(),
Active = true,
Created = DateTime.Now,
CreatedBy = "System",
Modified = DateTime.Now,
ModifiedBy = "System",
Name = x.ImporterName,
Address = x.ImporterAddress,
City = x.City,
ZipCode = x.ZipCode,
CountryId = x.CountryId
})
.OrderBy(x => x.Name);

Global service of the Team explorer's Query results window

What is the Query results window's global service (interface)? Code below:
var dteService = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
if (dteService == null)
{
Debug.WriteLine("");
return;
}
var something=Package.GetGlobalService(typeof(???)) as ???;
EDIT: The goal is, when I press the context menu button, I want the function callback to be able to access the service where the work item is selected (or the results list
Please check this case in MSDN forum for the details how to get it work: https://social.msdn.microsoft.com/Forums/vstudio/en-US/2d158b9c-dec1-4c59-82aa-f1f2312d770b/sdk-packageget-selected-item-from-query-results-list
The following code is quoted from above link for your quick reference:
Document activeDocument = _applicationObject.ActiveDocument;
if (activeDocument != null)
{
DocumentService globalService = (DocumentService)Package.GetGlobalService(typeof(DocumentService));
if (globalService != null)
{
string fullName = activeDocument.FullName;
IWorkItemTrackingDocument document2 = globalService.FindDocument(fullName, null);
if ((document2 != null) && (document2 is IResultsDocument))
{
int[] selectedItemIds = ((IResultsDocument)document2).SelectedItemIds;
}
}
}
var dteService = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
if (dteService == null)
{
Debug.WriteLine("");
return;
}
DocumentService documentService = Package.GetGlobalService(typeof(DocumentService)) as DocumentService;
if (documentService == null)
return;
string fullName = dteService.ActiveDocument.FullName;
IWorkItemTrackingDocument activeDocument = documentService.FindDocument(fullName, null);
if (activeDocument == null || !(activeDocument is IResultsDocument))
return;

How to insert ContentContol in footnote with specific range word.interop

How to go add content control in specific start and end range of footnote? I can add content control in document.range, but I am unable to add in footnote, please help to do this. If anyone reply quickly, I will be proved of you.
public void FindItalicFootnote(String FindText)
{
foreach (Word.Footnote footNote in Word.Document.Footnotes)
{
Word.Range RngFind = footNote.Range;
RngFind.Find.Forward = true;
if (RngFind.Find.Execute(FindText))
{
while (RngFind.Find.Found)
{
RngFind.Select();
object strtRange = Word.Selection.Range.Start;
object endRange = Word.Selection.Range.End;
string placeHolder = "";
bool findCase = false;
if (Word.Selection.Range.ParentContentControl == null && Word.Selection.Range.ContentControls.Count == 0)
{
RngFind.Select();
while (Word.Selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Font.Italic == -1)
{
Word.Selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Select();
strtRange = Word.Selection.Range.Start;
placeHolder = "{VerifiedBy='Italic'}";
findCase = true;
}
Word.ContentControl CC = RngFind.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText,
footNote.range(strtRange, endRange));
//my query is, how to say footNote.range(start, end), in main part I wrote as Word.Document.range(startRange, endRange)
CC.Title = "Case Reference";
CC.Tag = Guid.NewGuid().ToString();
CC.SetPlaceholderText(Text: placeHolder);
}
RngFind.Find.Execute(FindText);
}
}
}
}
Regards,
Saran

How to do pagination in couchdb using loveseat in c#

I need to do page load on scroll down in my application. I am using couchdb as my back end and I found a pagination option in couchdb which I think would satisfy my issue.
The thing is I can't find any working examples for pagination anywhere. I need someone's help in making my application work with this one.
Take a look at this for reference: https://github.com/soitgoes/LoveSeat/blob/master/LoveSeat/PagingHelper.cs
This is my code. I am getting an error in the options = model.GetOptions(); line, saying "object reference not set to an instance of an object".
public List<newVO> Getdocs(IPageableModel model)
{
List<newVO> resultList = new List<newVO>();
var etag = "";
ViewOptions options = new ViewOptions();
options = model.GetOptions();
options.StartKeyDocId = lastId;
options.Limit = 13;
options.Skip = 1;
var result = oCouchDB.View<newVO>("GetAlldocs", options);
//model.UpdatePaging(options, result);
if (result.StatusCode == HttpStatusCode.NotModified)
{
response.StatusCode = "0";
return null;
}
if (result != null)
{
foreach (newVO newvo in result.Items)
{
resultList.Add(newvo );
}
}
return resultList;
}
Thanks in advance. All ideas are welcome.
public List<newVO> Getdocs(IPageableModel model)
{
List<newVO> resultList = new List<newVO>();
var etag = "";
ViewOptions options = new ViewOptions();
options = model.GetOptions();
options.StartKeyDocId = lastId;
options.Limit = 13;
options.Skip = 1;
var result = oCouchDB.View<newVO>("GetAlldocs", options);
//model.UpdatePaging(options, result);
if (result.StatusCode == HttpStatusCode.NotModified)
{
response.StatusCode = "0";
return null;
}
if (result != null)
{
foreach (newVO newvo in result.Items)
{
resultList.Add(newvo );
}
}
return resultList;
}
This is my code and i am getting error in "options = model.GetOptions();" line that object reference not set to an instance of an object...
I've not used the LoveSeat paging implementation, but you can use the Limit and Skip properties on the ViewOptions to achieve paging:
public static IEnumerable<T> GetPage(this ICouchDatabase couchDatabase,
string viewName,
string designDoc,
int page,
int pageSize)
{
return couchDatabase.View(viewName, new ViewOptions
{
Skip = page * pageSize,
Limit = pageSize
}, designDoc);
}
This simple extension method will get a page of data from a CouchDB view

Categories

Resources