إخوتي الكرام
السلام عليكم
أحتاج مساعدتكم فضلا في إكسبشن يظهر لي في هذا الكود عندما لا أقوم برفع ملف من خلال InfPath Form المربوط بالداتابيس
حيث ان الأتاشمنت فايل أمر خياري
فكيف أحل هذه المسألة بحيث لا يحدث الإكسبشن عند عدم إرفاق ملف
وشكرا لكم
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
using System.Data;
using System.Text;
namespace GISFORM
{ //1
//the class for array to use it in store data
public class GenericDBParameter
{
string p_ParamName;
string p_ParamValue;
public string Parameter_Name
{
get { return p_ParamName; }
set { p_ParamName = value; }
}
public string Parameter_Value
{
get { return p_ParamValue; }
set { p_ParamValue = value; }
}
public GenericDBParameter(string Parameter_Name, string Parameter_Vlaue)
{
p_ParamName = Parameter_Name;
p_ParamValue = Parameter_Vlaue;
}
}
public partial class _Default : System.Web.UI.Page
{
string attachmentFileName = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.InputStream.Length != 0)
{
#region "Fixed code"
System.Xml.XPath.XPathDocument xDoc = new System.Xml.XPath.XPathDocument(Request.InputStream);
System.Xml.XPath.XPathNavigator navigator = xDoc.CreateNavigator();
//Converting xPath into XML Document
XmlDocument doc = new XmlDocument();
doc.LoadXml(navigator.InnerXml);
//Setting up namespaces, to access the data
XmlNameTable tbl = navigator.NameTable;
XmlNamespaceManager mgr = new XmlNamespaceManager(tbl);
mgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
mgr.AddNamespace("d", "http://schemas.microsoft.com/office/infopath/2003/ado/dataFields");
mgr.AddNamespace("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
mgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-03-25T09:45:27");
mgr.AddNamespace("xd", "http://schemas.microsoft.com/office/infopath/2003");
//------------------------------
#endregion
//Organzation_information
XmlNode ORGName = doc.SelectSingleNode(@"//my:fORG_NAME", mgr);
XmlNode LICENSEDATE = doc.SelectSingleNode(@"//my:fLICENSE_DATE", mgr);
XmlNode LICENSEFrom = doc.SelectSingleNode(@"//my:fLICENSE_From", mgr);
XmlNode ORGTYPE = doc.SelectSingleNode(@"//my:fORG_TYPE", mgr);
//
XmlNode fLocation = doc.SelectSingleNode(@"//my:fLocation_name", mgr);
XmlNode f_Phone = doc.SelectSingleNode(@"//my:fPhone", mgr);
XmlNode f_Email = doc.SelectSingleNode(@"//my:fEmail", mgr);
#region "File Attachment"
XmlNode fileName = doc.SelectSingleNode(@"//my:fMap_attachment_file", mgr);
if (fileName.InnerText != null && uploaFile(fileName) == false)
{
throw new Exception("File Upload failed.");
}
#endregion
SqlConnection objCon = new SqlConnection();
objCon.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlDataAdapter objAdapter = new SqlDataAdapter();
SqlCommand objCommand = new SqlCommand();
GenericDBParameter[] ORG = new GenericDBParameter[4];
GenericDBParameter[] LocationInfo = new GenericDBParameter[4];
SqlTransaction objTrans = null;
string sqlQry = "";
try
{//5
objCon.Open();
objTrans = objCon.BeginTransaction();
//Store in Organzation_information
//********************************************
ORG[0] = new GenericDBParameter("VORG_NAME", ORGName.InnerText.ToString());
ORG[1] = new GenericDBParameter("VLICENSE_DATE", LICENSEDATE.InnerText.ToString());
ORG[2] = new GenericDBParameter("VLICENSE_From", LICENSEFrom.InnerText.ToString());
ORG[3] = new GenericDBParameter("VORG_TYPE", ORGTYPE.InnerText.ToString());
sqlQry = "INSERT INTO Organzation_information (ORG_NAME,LICENSE_DATE,LICENSE_Num,ORG_TYPE) " +
" VALUES (@VORG_NAME,@VLICENSE_DATE,@VLICENSE_From,@VORG_TYPE);" +
" SELECT CAST(scope_identity() AS int)";
objCommand = new SqlCommand(sqlQry, objCon);
objCommand.Transaction = objTrans;
foreach (GenericDBParameter apram in ORG)
{
objCommand.Parameters.AddWithValue(apram.Parameter_Name, apram.Parameter_Value);
}
int EmpId = (int)objCommand.ExecuteScalar();
LocationInfo[0] = new GenericDBParameter("Vemp_ID", EmpId.ToString());
LocationInfo[1] = new GenericDBParameter("VfLocation", fLocation.InnerText.ToString());
LocationInfo[2] = new GenericDBParameter("Vf_Phone", f_Phone.InnerText.ToString());
LocationInfo[3] = new GenericDBParameter("Vf_Email", f_Email.InnerText.ToString());
LocationInfo[4] = new GenericDBParameter("VattachmentFileName", attachmentFileName.ToString());
sqlQry = "INSERT INTO Location_Information (emp_ID ,Location_name,Phone,EmailوEmailMap_attachment_file) " +
" VALUES (@VORG_ID,@VfLocationname,@VfOwnerName,@Vf_Phone,@Vf_Email@VattachmentFileName)";
objCommand = new SqlCommand(sqlQry, objCon);
objCommand.Transaction = objTrans;
foreach (GenericDBParameter apram in LocationInfo)
{
objCommand.Parameters.AddWithValue(apram.Parameter_Name, apram.Parameter_Value);
}
objCommand.ExecuteScalar();
//End Location_Information
objTrans.Commit();
}
catch (Exception exp)
{
Label1.Text = exp.ToString();
objTrans.Rollback();
}
finally
{
objCommand.Dispose();
objCon.Close();
}
}
}
private bool uploaFile(XmlNode xFile)
{
bool bolReturn = false;
int SP1Header_Size = 20;
int FIXED_HEADER = 16;
int fileSize;
int attachmentNameLength;
try
{
byte[] decodedAttachment;
byte[] theData = Convert.FromBase64String(xFile.InnerText);//
//fileName = System.IO.File
using (MemoryStream ms = new MemoryStream(theData))
{
BinaryReader theReader = new BinaryReader(ms);
byte[] headerData = new byte[FIXED_HEADER];
headerData = theReader.ReadBytes(headerData.Length);
fileSize = (int)theReader.ReadUInt32();
attachmentNameLength = (int)theReader.ReadUInt32() * 2;
byte[] fileNameBytes = theReader.ReadBytes(attachmentNameLength);
Encoding enc = Encoding.Unicode;
attachmentFileName = enc.GetString(fileNameBytes, 0, attachmentNameLength - 2);
decodedAttachment = theReader.ReadBytes(fileSize);
string fullFileName = ConfigurationManager.AppSettings["FilePath"].ToString();
if (!fullFileName.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
fullFileName += Path.DirectorySeparatorChar;
}
fullFileName += attachmentFileName;
if (File.Exists(fullFileName))
File.Delete(fullFileName);
FileStream fs = new FileStream(fullFileName, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(decodedAttachment);
bw.Close();
fs.Close();
}
bolReturn = true;
}
catch (Exception exp)
{
throw exp;
}
return bolReturn;
}
}
}