Wednesday, December 9, 2009

Convert DataReader To DataSet through C# Dynamically

Hi,

I was writing an application to read data from AS 400, Db2, based on the data I have to generate a report. I was trying to covert OdbcDataReader to DataSet. The following Code was helpful to do the same... We can use it for SqlDataReader as well.

Code:

public DataSet ConvertDataReaderToDataSet(System.Data.Odbc.OdbcDataReader reader)

{

DataSet dataSet = new DataSet();

do

{

// Create data table in runtime

DataTable schemaTable = reader.GetSchemaTable();

DataTable dataTable = new DataTable();

if (schemaTable != null)

{

for (int i = 0; i < schemaTable.Rows.Count; i++)

{

DataRow dataRow = schemaTable.Rows[i];

// Create a column name as provided in Schema

string columnName = (string)dataRow["ColumnName"];

// Define Column Type here

DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]);

//Adding Column to table

dataTable.Columns.Add(column);

}

dataSet.Tables.Add(dataTable);

// Fill the data table from reader data

while (reader.Read())

{

DataRow dataRow = dataTable.NewRow();

for (int i = 0; i < reader.FieldCount; i++)

dataRow[i] = reader.GetValue(i);

dataTable.Rows.Add(dataRow);

}

}

else

{

// No records were returned

DataColumn column = new DataColumn("RowsAffected");

dataTable.Columns.Add(column);

dataSet.Tables.Add(dataTable);

DataRow dataRow = dataTable.NewRow();

dataRow[0] = reader.RecordsAffected;

dataTable.Rows.Add(dataRow);

}

}

while (reader.NextResult());

return dataSet;

}


Hope It Helpzzzz

4ZZZZ

Monday, December 7, 2009

Infopath System.Security. Permissions.EnvironmentPermission Fulltrust

Hey All,

When I tried to get current user in code behind in the infopath form, the following security exception occurred. The solution is we need to set Full trust for the Form.

Code used: nav.SelectSingleNode("//my:Myfield", NamespaceManager).SetValue(System.Environment.UserName);

Exception Message:
Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand() at System.Environment.get_UserName() at ItemCreation.FormCode.FormEvents_Loading(Object sender, LoadingEventArgs e) at Microsoft.Office.InfoPath.Server.SolutionLifetime.FormEventsHost.<>c__DisplayClass6.<>c__DisplayClass8.b__1() at Microsoft.Office.InfoPath.Server.Util.DocumentReliability.InvokeBusinessLogic(Thunk thunk) at Microsoft.Office.InfoPath.Server.SolutionLifetime.FormEventsHost.<>c__DisplayClass6.b__0(Object sender, LoadingEventArgs e) at Microsoft.Office.InfoPath.Server.SolutionLifetime.FormEventsHost.<>c__DisplayClass34.b__30() at Microsoft.Office.InfoPath.Server.DocumentLifetime.OMExceptionManager.CallFormCodeWithExceptionHandling(UserMessages userMessages, OMCall d) at Microsoft.Office.InfoPath.Server.SolutionLifetime.FormEventsHost.FireLoadingEvent(Document document, Dictionary`2 inputParameters) at Microsoft.Office.InfoPath.Server.DocumentLifetime.Document.PerformOnLoadEvent(Dictionary`2 intputParameters) at Microsoft.Office.InfoPath.Server.DocumentLifetime.Document.LoadSolutionAndDocument(HttpContext context, Solution solution, DocumentMetaInformation documentMetaInformation, OnInitializeCallback onInitializeCallback, OpenParameters openParameters) at Microsoft.Office.InfoPath.Server.DocumentLifetime.Document.OpenDocumentWithSolution(HttpContext context, SPSite contextSite, Solution solution, DocumentMetaInformation documentMetaInformation, OnInitializeCallback onInitializeCallback, OpenParameters openParameters) at Microsoft.Office.InfoPath.Server.DocumentLifetime.Document.NewFromSolution(HttpContext context, SPSite contextSite, Solution solution, DocumentMetaInformation documentMetaInformation, Boolean disableFirstRequestOptization, OnInitializeCallback onInitializeCallback, OpenParameters openParameters) at Microsoft.Office.InfoPath.Server.Controls.XmlFormView.DataBindInternal(SolutionMetaInformation solutionMetaInformation, DocumentMetaInformation documentMetaInformation, String absoluteSolutionLocation, Boolean hasCloseHandler, Document& document) at Microsoft.Office.InfoPath.Server.Controls.FormServerPage.InitializeRenderInLine(SolutionMetaInformation solutionMetaInformation, DocumentMetaInformation documentMetaInformation, String absoluteSolutionLocation) at Microsoft.Office.InfoPath.Server.Controls.FormServerPage.NewEditingSession(SPSite contextSite, FormServerPageQueryParameters queryParameters) at Microsoft.Office.InfoPath.Server.Controls.XmlFormView.StartNewEditingSession() at Microsoft.Office.InfoPath.Server.Controls.XmlFormView.EnsureDocument(EventLogStart eventLogStart) at Microsoft.Office.InfoPath.Server.Controls.XmlFormView.<>c__DisplayClass3.b__1() at Microsoft.Office.Server.Diagnostics.FirstChanceHandler.ExceptionFilter(Boolean fRethrowException, TryBlock tryBlock, FilterBlock filter, CatchBlock catchBlock, FinallyBlock finallyBlock) The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.EnvironmentPermission The first permission that failed was: version="1" Read="UserName"/> The demand was for: version="1" Read="UserName"/> The granted set of the failing assembly was: version="1"> version="1" Flags="Execution"/> version="1" PublicKeyBlob="0024000004800000940000000602000000240000525341310
00400000100010083EF9CF1283A631F1C2513A36EA356E01870A92F21E61D5D53
1420B2C3905A6E3F8A0BD9E2BFAD3EAB3362877D233232ED1AE4836A1C51EAA7
717A16047A0F6F6E589F8689BCC5A15A20211252DB101B889228C5E15A3D49DC
2E112650853F16A85DD01652057E5D2CE7A41B01AA56576FFED4B4567FD8367
EE777C222C590E6"
Name="ItemCreation" AssemblyVersion="1.0.3628.18207"/> version="1" Url="file:///c:/inetpub/wwwroot/"/> version="1" Zone="MyComputer"/> version="1" Level="Minimal"/> version="1" Connections="True"/> The assembly or AppDomain that failed was: ItemCreation, Version=1.0.3628.18207, Culture=neutral, PublicKeyToken=f4d1ad1bba384fc7 The method that caused the failure was: Void FormEvents_Loading(System.Object, Microsoft.Office.InfoPath.LoadingEventArgs) The Zone of the assembly that failed was: MyComputer The Url of the assembly that failed was: file:///c:/inetpub/wwwroot/

How to set Permission: In Tools -> Form Options -> Security & trust -> Full Trust & Publish



Hope it Helpz

Thanks & Regards
Fauzi ~ 4Z

Sunday, December 6, 2009

How to read & write value from Infopath Datasource throught C#

Hi,

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