Category Archives: C#

C#

Clearing a drop down list value on a web form

Ran into this issue with trying to clear a data linked drop down list on a web form, it was tricky because there are two controls for the drop down list, the first one being the ‘source’ and the second one being the sub-source. All I had to do was clear the source by using the following line:

ddlSource.SelectedIndex = -1;
C# MSSQL

Writing form data to SQL DB with C#

In having to convert my current VB projects into C#, I ran into an issue trying to write to a DB from a form.  This blog opened my eyes to how this is accomplished and here is the code I used to get the unsubscribe feature to work.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Taleo_UnsubscribeJPN
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DateTime tstamp = DateTime.Now;
String email = Request.QueryString["email"];
String atsid = Request.QueryString["atsid"];
lblEmail.Text = email;

}

protected void btnRemove_Click(object sender, EventArgs e)
{
String strBrowserInfo = (Server.HtmlEncode(Request.UserAgent));
String strIPAddress = (Server.HtmlEncode(Request.UserHostAddress));
String atsid = Request.QueryString["atsid"];
string connection_string = "Data Source=DB_Server;Initial Catalog=TaleoData;Persist Security Info=True;User ID=tdowner;Password=password";
string query = "insert into OptOutEmail (email, tstamp, processed, atsid, remote_addr, http_user_agent) values ('" + lblEmail.Text + "', '" + DateTime.Now + "', " + 0 + ", '" + atsid + "', '" + strIPAddress + "', '" + strBrowserInfo + "')";
SqlConnection connection1 = new SqlConnection(connection_string);

SqlCommand insertCommand = new SqlCommand(query, connection1);

connection1.Open();

insertCommand.ExecuteNonQuery();

connection1.Close();

Server.Transfer("Confirmation.aspx?email=" + lblEmail.Text);

}
}
}

C# SSIS

C# to move files in SSIS script task

Most of the time that I’m creating integration packages, I have a central working directory where all the action is taking place, at the end of the package execution is where I’ll archive files so that should they be needed to troubleshoot an issue I’ve got them in a handy place.  This is a script that I created for that process.  Note that lines 7 through 19 are generated by the Visual Studio Tools for Applications in SSIS when you create the script task.

using System;
using System.IO;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_218640c1eafd4eb9ab909245d34e01c7.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{

#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion

public void Main()
{
string strOriginalFileName = @"C:\TCC\20110722-UpdateCandidate\UpdateHireEmpStatus.csv";
string strArchivedFileName;
string strDate;
DateTime dtDate = DateTime.Now;

strDate = String.Format("{0:yyyyMMdd_HH_mm_ss}", dtDate);

strArchivedFileName = @"C:\TCC\20110722-UpdateCandidate\Archive\UpdateHireEmpStatus-" + strDate + ".csv";

File.Move(strOriginalFileName, strArchivedFileName);
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
C#

C# to delete files older than X days from a directory

The vast majority of the time, when I build an integration I archive all the files so that they can be used to track down where something is going wrong.  Unfortunately if left unchecked these files can grow and grow and it becomes impossible (or rather terribly difficult) to sort through the files to see if there was indeed an issue.  The answer to this is to cull down the files when you archive them.  To this end it’s good to kill all files older than X days.  This is a console application that can do this, it actually does this in 3 directories.  Path, path1 and path2 define the directories and days is the amount of days back to delete.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TestConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string path, path2, path3;
int days;
System.DateTime date;
System.DateTime keepdate;
path = "Y:\\SQL_Backups\\SFDC_Data";
path2 = "Y:\\SQL_Backups\\TaleoData";
path3 = "Y:\\SQL_Backups\\dnn5_test";
days = 10;
date = (DateTime.Now);
keepdate = date.AddDays(-days);

DirectoryInfo fileListing = new DirectoryInfo(@path);

foreach (FileInfo f in fileListing.GetFiles())

{
if (f.LastWriteTime <= keepdate)
f.Delete();
}

DirectoryInfo fileListing2 = new DirectoryInfo(@path2);

foreach (FileInfo f in fileListing2.GetFiles())
{
if (f.LastWriteTime <= keepdate)
f.Delete();
}

DirectoryInfo fileListing3 = new DirectoryInfo(@path3);

foreach (FileInfo f in fileListing3.GetFiles())
{
if (f.LastWriteTime <= keepdate)
f.Delete();
}

}
}
}

I have yet to get this functionality to work in SSIS but will post how to do that once I figure it out.