Embedding few related dlls files into my class library project as a embedded resource & getting error - string reference not set to an instance of a s

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
this question is purely related to .net rather not about devexpress component. so please do not bypass.

i have install devexpress suit in my office pc and there i have created one class library project where i have refer few dll into that project.

first i add all reference and then add those devex dll into a folder called Lib in my class library project and later i add those devex dll to my resource and change each dll to embed resource from property window.

in my class library project i have one class which has one constructor from where i load those dll from resource. i used this code to do so.

i followed this link Embed one dll inside another as an embedded resource ~ C# Library

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = new AssemblyName(args.Name).Name + ".dll";
string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));

using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};

when i test this dll project in to my pc then it work because i have installed devex setup in my pc but when i run the same project from another pc
which has no devex installed and do not have devex license there i got a error.

the error was ambiguous. error text was string reference not set to an instance of a string

i really do not understand does this error throwing from devex component or can i embed devex dll as resource into another dll project ?

from this below code a exception was throwing to calling environment. exception was string reference not set to an instance of a string

anyone can tell me from which code or line suppose to throwing this exception called string reference not set to an instance of a string ?

see my class library project code which is not big

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using DevExpress.Spreadsheet;


namespace xExcel.Convertion
{
/// <summary>
/// Converter class will convert excel formatted values to actual values
/// </summary>
public class Converter
{
/// <summary>
/// Converter will load DevEx Assembly from resource
/// </summary>
public Converter()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = new AssemblyName(args.Name).Name + ".dll";
string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));

using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
}

private DevExpress.XtraSpreadsheet.SpreadsheetControl sscConverter;

public List<Status> Convert(string ExcelSourceFilePath, string ExcelSaveFilePath, List<string> ListOfSheets)
{
List<Status> _status = null;

#region Validating inputs

if (ExcelSourceFilePath.Trim()=="")
{
throw new Exception("Source excel file path is empty");
}

if (ExcelSaveFilePath.Trim() == "")
{
throw new Exception("Save excel file path is empty");
}

if (ListOfSheets.Count <= 0)
{
throw new Exception("No Sheet / tab name found");
}

if (!File.Exists(ExcelSourceFilePath))
{
throw new FileNotFoundException("Source excel file not found " + ExcelSourceFilePath);
}

if (!Directory.Exists(ExcelSaveFilePath))
{
throw new FileNotFoundException("Excel Save path not found " + ExcelSaveFilePath);
}
#endregion

#region convertion start
if (ExcelSourceFilePath.Trim() != "" & ExcelSaveFilePath.Trim() != "")
{
int counter = 0;
_status = new List<Status>();
string strExt = "";
string currentSheetName = "";

// storing file extension
strExt = Path.GetExtension(ExcelSourceFilePath).ToUpper();

// checking list of sheet name exist or not
if (ListOfSheets.Count > 0)
{
// if excel source file exist
if (File.Exists(ExcelSourceFilePath))
{
sscConverter = new DevExpress.XtraSpreadsheet.SpreadsheetControl();

// loading excel file into spread sheet
sscConverter.LoadDocument(ExcelSourceFilePath);

DevExpress.Spreadsheet.IWorkbook workbook = sscConverter.Document;

try
{
sscConverter.BeginUpdate();

// iterate in sheet collection
foreach (Worksheet sheet in workbook.Worksheets)
{
// if sheet name matched then clearing the format
if (ListOfSheets.Where(a => a.ToUpper() == sheet.Name.ToUpper()).Count() > 0)
{
currentSheetName = sheet.Name;

Range usedRange = sheet.GetUsedRange();
usedRange.ClearFormats();
// storing convertion status for reporting to client if passed
_status.Add(new Status { SheetName = currentSheetName, Success = true });
counter++;
}
}

sscConverter.EndUpdate();

if (counter > 0)
{
if (workbook != null)
{
// after convertion of mention sheets saving excel to new path
workbook.SaveDocument(ExcelSaveFilePath + "\\" + Path.GetFileName(ExcelSourceFilePath),
(strExt == ".XLSX" ? DevExpress.Spreadsheet.DocumentFormat.Xlsx : DevExpress.Spreadsheet.DocumentFormat.Xls));
}
}
}
catch (Exception ex)
{
// storing convertion status for reporting to client if fail
_status.Add(new Status { SheetName = currentSheetName, Success = false });
}
finally
{
sscConverter = null;
}

}
}
}
#endregion

// returning status to calling environment
return _status;
}
}

/// <summary>
/// Status class will store the Excel sheet by sheet processing status
/// </summary>
public class Status
{
public string SheetName { get; set; }
public bool Success { get; set; }
}
}


I post this issue to devexpress forum and they said we can include their dll into my class library project as resource.

they suggest me to post this issue in stackoverflow forum. here link where i have posted this issue T758350 - Embedding Spread sheet related dll files into my class library project as a embedded resource | DevExpress Support Center

please some one help me to catch this issue where the problem lies ?

this question is purely related to .net not devexpress.

Continue reading...
 
Back
Top