Convert Xml node list to Data table

August 24, 2012

XmlDocument xmldoc = new XmlDocument();
FileStream fs = new FileStream(Application.StartupPath + “\\Response.xml”,FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
fs.Close();
XmlNodeList xmlnode;
xmlnode = xmldoc.GetElementsByTagName(comboBox1.Text.Trim());

//ComboBox1.Text means Xml node Name  Here
if (xmlnode.Count > 0)
{
//DataSet ds = new DataSet();
//ds.ReadXml(Application.StartupPath + “\\Response.xml”);
DataTable dt = ConvertXmlNodeListToDataTable(xmlnode);

//ds.ReadXml(
if (dt != null && dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
}

//—-Method for convert xmlnode list to data table
public DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
{
DataTable dt = new DataTable();
int TempColumn = 0;

foreach (XmlNode node in xnl.Item(0).ChildNodes)
{
TempColumn++;
DataColumn dc = new DataColumn(node.Name, System.Type.GetType(“System.String”));
if (dt.Columns.Contains(node.Name))
{
dt.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());
}
else
{
dt.Columns.Add(dc);
}
}

int ColumnsCount = dt.Columns.Count;
for (int i = 0; i < xnl.Count; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < ColumnsCount; j++)
{
dr[j] = xnl.Item(i).ChildNodes[j].InnerText;
}
dt.Rows.Add(dr);
}
return dt;
}

}


How to extract whole number from a string using javascript

August 24, 2012

You’ll surely get your desired Output.

<script language=”javascript”>

var txt = “GUJ 12.50 VAT”;
var numb = txt.match(/\d+./g);
numb = numb.join(“”);
alert(numb);

//You’ll get 12.50 in numb….
</script>


Bulk Insert in to Db-Xml to Stored Procedure

August 24, 2012

//Create Data table to collect data

DataTable dt = new DataTable();
dt.TableName = “Test”;
dt.Columns.Add(“DeviceCode”, typeof(Int32));
dt.Columns.Add(“Message”, typeof(string));
dt.Columns.Add(“EmpId”, typeof(string));
dt.Columns.Add(“EmpCode”, typeof(string));
dt.Columns.Add(“CardID”, typeof(string));
DataRow dr = dt.NewRow();
dr[“DeviceCode”] = “1”;
dr[“Message”] = “1”;
dr[“EmpId”] = “1”;
dr[“EmpCode”] = “1”;
dr[“CardID”] = “1”;

dt.Rows.Add(dr);
dr = dt.NewRow();
dr[“DeviceCode”] = “2”;
dr[“Message”] = “2”;
dr[“EmpId”] = “2”;
dr[“EmpCode”] = “2”;
dr[“CardID”] = “2”;

dt.Rows.Add(dr);

string result;
using (StringWriter sw = new StringWriter())
{
dt.WriteXml(sw);
result = sw.ToString();
}

string cn = Convert.ToString(ConfigurationManager.ConnectionStrings[“mycn”]);
SqlConnection conn = new SqlConnection();
conn.ConnectionString = cn;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = “sp_Insert_DeviceCommand_Bulk”;
SqlParameter param = new SqlParameter(“@mytable”, SqlDbType.Xml);
param.Value = result;
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();

===================================================
Stored Procedure for bulk insert/Xml to Database
===================================================
ALTER PROCEDURE [dbo].[sp_Insert_DeviceCommand_Bulk]
@mytable as xml
AS
BEGIN
SET NOCOUNT ON;

–INSERT INTO dbo.tblqueuedata
–(DeviceCode,Message,EmpId,EmpCode,CardId)
–SELECT DeviceCode,Message,EmpId,EmpCode,CardId FROM @dtQueue
INSERT INTO tblqueuedata
(DeviceCode,Message,EmpId,EmpCode,CardId)
SELECT
cast(convert(varchar(20), colx.query(‘data(DeviceCode) ‘)) as int) as DeviceCode,
cast(colx.query(‘data(Message) ‘) as varchar) as Message,
cast(colx.query(‘data(EmpId) ‘) as varchar) as EmpId,
cast(colx.query(‘data(EmpCode) ‘) as varchar) as EmpCode,
cast(colx.query(‘data(CardId) ‘) as varchar) as CardId
FROM @mytable.nodes(‘DocumentElement/Test’) AS Tabx(Colx)


Get Uploaded File Bytes

August 24, 2012

Read the rest of this entry »