إخوتي الكرام
السلام عليكم ورحمة الله
أحتاج خبراتكم في XML مع asp.net و C#
أعمل على InfoPath وكما تعلمون أنه يعتمد على XML
عملت كود لأخد البيانات المدخلة عن طريق InfoPath Form وإدخالها في SQL داتابيس
وأحتاج أن أفحص بعض البيانات المدخل (حيث أخذ بعض القيم وأبحث في أحد الجداول إن كانت يوجد مثلها أم لا) وأظهر لرسالة للمستخدم كـ validation
إن كان يوجد مثلها أريد أن يظهر في Form للمستخدم رسالة أن الرقم خطأ وتأكد منه كما نفعل عادة في label لتنبيه المستخدم لأمر ما في صفحات الويب
كيف أعمل هذا مع InfoPath Form ؟
احتاج مساعدتكم عاجلا فضلا
وشكرا لكم (f)
وهذا الكود
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 LICENSENum = doc.SelectSingleNode(@"//my:fLICENSE_numder", 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[3];
GenericDBParameter[] LocationInfo = new GenericDBParameter[5];
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_Num", LICENSENum.InnerText.ToString());
ORG[2] = new GenericDBParameter("VORG_TYPE", ORGTYPE.InnerText.ToString());
sqlQry = " SELECT LICENSE_Num FROM Organzation_information WERE LICENSE_Num = @VLICENSE_Num";
objCommand = new SqlCommand(sqlQry, objCon);
int Lnum = (int)objCommand.ExecuteScalar();
if ( Lnum != null )
{
// أحتاج كود هنا لإظهار الفاليديشون في Form
}
else
{
sqlQry = "INSERT INTO Organzation_information (ORG_NAME,LICENSE_Num,ORG_TYPE) " +
" VALUES (@VORG_NAME,@VLICENSE_Num,@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 OrgId = (int)objCommand.ExecuteScalar();
}
//******
LocationInfo[0] = new GenericDBParameter("VOrg_ID", OrgId.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 (ORG_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;
}
}
}