Blog from a software professional, Passionate to work in latest cutting edge technologies in order to get better results in business.
Sunday, December 6, 2009
How to read & write value from Infopath Datasource throught C#
In a requirement, we need to display views based on the current user who updates the infopath form. So I thought let me write the logic to read and store values in Info path Data source from code behind. Following Code useful.
Read:
XPathNavigator nav = MainDataSource.CreateNavigator();
string fieldValue = nav.SelectSingleNode("//my:Myfield1", NamespaceManager).Value;
Write:
XPathNavigator nav = MainDataSource.CreateNavigator();
nav.SelectSingleNode("//my:Myfield1", NamespaceManager).SetValue("NewValue");
essential Namespace is Microsoft.Office.InfoPath;
Hope it helps...
Best Regards
Fauzi ~4Z
Tuesday, June 2, 2009
Sample to read Excel file through C#
Today there was a requirement to project a graph from a excel sheet provided by a client. So i wrote a sample code in C# to read the data from the excel sheet. Thought let me post a sample code which will be helpful for readers...
The Class used here is System.Data.OleDb.OleDbConnection
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ExcelFromC : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Fauzi\Excelfile\Sample.xls;Extended Properties=Excel 5.0";
//Creating the OleDb Connection
System.Data.OleDb.OleDbConnection objOleDbConnection = new System.Data.OleDb.OleDbConnection(strConnectionString);
//creating query for Sheet#1
//Here A is the text in A[0][0] ~= A[Row #1][Column #1] So the first row is considered like a Column of a table
string strExcelQuery = "Select A,C from [Sheet1$]";
//strExcelQuery = "Select * from [Sheet1$]";
//create the OleDb Command
System.Data.OleDb.OleDbCommand objOleDbCommand = new System.Data.OleDb.OleDbCommand(strExcelQuery, objOleDbConnection);
objOleDbConnection.Open();
//Read Section:
//-------------
//Create OleDb DataReader
System.Data.OleDb.OleDbDataReader objOleDbDataReader = objOleDbCommand.ExecuteReader();
//Iterating for every records
Response.Write("Reading Data from Excel file");
Response.Write("</br>");
Response.Write(" A - B - C ");
while (objOleDbDataReader.Read())
{
Response.Write("</br>");
string strValue = "";
for (int i = 0; i < objOleDbDataReader.FieldCount; i++)
{
strValue = strValue + " - " + (objOleDbDataReader.GetValue(i)).ToString();
}
Response.Write(strValue);
}
objOleDbConnection.Close();
//Update Section:
//---------------
strExcelQuery = "Update [Sheet1$] set C = C+2"; // from Sheet1";
//Create the command to be executed
objOleDbCommand = new System.Data.OleDb.OleDbCommand(strExcelQuery, objOleDbConnection);
//Open the connection to the file
objOleDbConnection.Open();
//Execute update
objOleDbCommand.ExecuteNonQuery();
//Close connection
objOleDbConnection.Close();
Response.Write("</br>");
Response.Write("</br>");
Response.Write("Reading Data AFTER(C = C+2) Excel file was updated through code");
Response.Write("</br>");
Response.Write(" A - B - C ");
strExcelQuery = "Select A,C from [Sheet1$]";
//strExcelQuery = "Select * from [Sheet1$]";
//create the OleDb Command
objOleDbCommand = new System.Data.OleDb.OleDbCommand(strExcelQuery, objOleDbConnection);
objOleDbConnection.Open();
//Create OleDb DataReader
objOleDbDataReader = objOleDbCommand.ExecuteReader();
//Iterating for every records
while (objOleDbDataReader.Read())
{
Response.Write("</br>");
string strValue = "";
for (int i = 0; i < objOleDbDataReader.FieldCount; i++)
{
strValue = strValue + " - " + (objOleDbDataReader.GetValue(i)).ToString();
}
Response.Write(strValue);
}
objOleDbConnection.Close();
}
}
Exception:While reading the excel file the whole sheet is considered as a table & the first row texts are considered to be the column names. So when you give a select (string strExcelQuery = "Select Col from [Sheet1$]";) query in run time the compiler will look for a column 'Col' in the first row and if the particular row is missing, the following exception will be raised.
Syntax error (missing operator) in query expression 'Col'
System.Data.OleDb.Exception -> System.Data.OleDb.OleDbErrorCollection
how to read excel file through C#
Hope it helps :)
Regards
Fauzi
Tuesday, May 5, 2009
Read Resource file: Resource.resx dynamically thourgh C#
Dear Reader,
Following is a sample method to read a Resource file in your web application which resides in the location "App_GlobalResources\Resource.resx". The assembly references needed are
using System.Collections;
using System.Resources;
Is case if there is a message shown when solution is built
"The type or namespace name 'ResXResourceReader' does
not exist in the class or namespace 'System.Resources' (are you missing an
assembly reference?)"
Please refer ResXResourceReader missing an assembly reference
Source code:
public void ReadResourceresx()
{
//Creating a hashtable to store string in resource files
Hashtable resourceStringsHashTable = new Hashtable();
//Provide relative path of Resource file
string strResourceFilelocation = HttpContext.Current.Request.PhysicalApplicationPath.ToString() +
@"\App_GlobalResources\Resource.resx";
//Creating object for Class: ResXResourceReader
System.Resources.ResXResourceReader objResXResourceReader = null;
try
{
objResXResourceReader = new ResXResourceReader(strResourceFilelocation);
//Defines a DictonartEntry Key/Value to Set or retriev values
foreach (DictionaryEntry resourceString in objResXResourceReader)
{
if (!resourceStringsHashTable.Contains(resourceString.Key.ToString()))
{
//Adding values to HashTable one by one in a loop
resourceStringsHashTable.Add(resourceString.Key.ToString(), resourceString.Value);
}
}
}
finally
{
//Closing the created object
objResXResourceReader.Close();
}
}
Hope it helps :)
Regards
Fauzi