Tuesday, June 2, 2009

Export C#.Net DataSet to a CSV File

You can export a Data set to a CSV file in C#.Net. The following method get Path to CSV file and DataSet as parameters and create the CSV file in given path.

public static void ExportGridView(string Path, DataSet ds)
{
StreamWriter sw = new StreamWriter(Path);
DataTable dt = ds.Tables[0];
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}

Create PDF Files in C#.Net

Creating PDF files in C# is very simple. But you should download the library called “itextsharp.dll” and add a reference to your project . You can download “itextsharp.dll” by clicking following link

Download itextsharp.dll

Following Code is a sample to use it

Document myDocument = new Document(PageSize.A4.Rotate());
SavePDFDialog.ShowDialog();
if (SavePDFDialog.FileName != "")
{
try
{
PdfWriter.GetInstance(myDocument, new FileStream(SavePDFDialog.FileName, FileMode.Create));
myDocument.Open();
myDocument.Add(new Paragraph(txtInput.Text));
myDocument.AddAuthor("Tharindu Sanjeewa Rathnathunga");
myDocument.AddTitle("tsr.edu@live.com");
}
catch (DocumentException de)
{
MessageBox.Show(de.Message);
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
finally
{
myDocument.Close();
}
}

NOTE: Your project should

  • Have a SaveFileDialog called SavePDFDialog and TextBox called txtInput
  • You should use following using statements
  • using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;

    To download the source project click here.

How to Encrypt the Connection String in App.config of a C# Application

1. First you should add a App.config to your C# project

  • Right click on your project –> Add –>New Item
  • From Following window chose Application Configuration file

image

  • Visual studio will add a XML file with following content
2. Add following content in between tags of App.config
3. Now you can store the connection string from following code (you should enter the connection string to a text box in your application named as txtConString)

private void EncryptConStr()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["connectionString"].Value = txtConString.Text;
config.Save(ConfigurationSaveMode.Modified);
EncryptSection("appSettings");
}

private void EncryptSection(String SectionName)
{

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection section = config.GetSection(SectionName);

if (section != null)
{
if (!section.IsReadOnly())
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
}

4. And you can view Connection string in App.config using following code

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
txtConString.Text = config.AppSettings.Settings["connectionString"].Value.Trim().ToString();

NOTE: For this example you should add a reference to System.Configuration from your project.
* To Download source project click here