the variable “doc” is not accessible anywhere else in the method outside of the if statement so that if doc==null fails since the scope of “doc” is only within those if statements that it is defined in….How do I deal with this issue? adding public just leads to more errors..
protected void Page_Load(object sender, EventArgs e)
{
try
{
string id, type, UniqueColID;
string FilePath = Server.MapPath("~").ToString();
type = Request.QueryString["type"];
if (type.Equals("template"))
{
MergeDocument template = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");
template.DrawToWeb();
}
else
{
id = Request.QueryString["id"];
UniqueColID = DBFunctions.DBFunctions.testExist(id);
if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
{
MergeDocument doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
}
else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
{
MergeDocument doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
}
DBFunctions.DBFunctions.FlagDriverPrintOnly = false;
if (doc == null)
doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");
doc.DrawToWeb();
}
}
catch(Exception err)
{
MessageBox("Creating PDF file was not successful. " + err.ToString());
}
I have tried declaring it at higher level but I still get same error:
**Use of unassigned local variable 'doc' -->>>at: if (doc==nul)**
After doing MergeDocument=null; at higher level I get a new error:
System.NullReferenceException: Object reference not set to an instance of an object. at GeneratePDF.Page_Load(Object sender, EventArgs e)
this error points to "if (type.Equals("template"))"
Define the
docvariable just before you actually want to use it: