The below code that illustrates how to receive arguments for a WinForms Application.The example expects two arguments and validates that the are converted properly.
If the appropiate argumetns are sent, these are stored in a public class named GlobalVars.
[STAThread]
static void Main( string[] MyArgs)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (MyArgs.Length == 0 || MyArgs.Length < 2)
{
MessageBox.Show(
"Please specify Parm1 and Parm2 as Arguments in order to
Proceed.",
"YourAppTitle", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
return;
}
else
{
try
{
GlobalVars lGlobalVars = new GlobalVars();
lGlobalVars.gOrderSkey = Int64.Parse(MyArgs[0]);
lGlobalVars.gShipmentNo = Int64.Parse(MyArgs[1]);
}
catch (Exception Ex)
{
MessageBox.Show(
"Invalid Parm1 and/or Parm2 Arguments. Please verify. \rError: "
+ Ex.Message, "YourAppTitle",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
return;
}
Application.Run(new Form1());
}
}
Notice the "\n" to break down the MessageBox in two lines. Is a nice little trick that I often forget
Hope this helps,
Will
Thursday, October 27, 2011
How to receive execution arguments on a C# WinForms Application?
Thursday, October 20, 2011
Where is Microsoft Outlook Temp Folder? (Windows 7 and Outlook 2010)
Ever wondered where Microsoft Outlook stores temp files?
It is useful to know this location, so you can go there and delete the multiple copies of attachments and pictures and other Outlook files. It can also be useful if you are looking for an attachment contained in a message that you deleted by mistake.
For Outlook 2010 running under Windows 7 go to:
C:\Users\UserName\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Outlook
In this folder you will find that there is one or multiple folders named with a random combination of numbers and letters, for example: 82UOSVIP
Inside this directory you will find attachments and pictures stored by Outlook.
If you are curious. This is stored in a registry entry:
Data Type: REG_SZ
If you need more info, or you are looking for the temp folder in previous versions of Outlook, go to:
http://support.microsoft.com/kb/817878
CodeProject
It is useful to know this location, so you can go there and delete the multiple copies of attachments and pictures and other Outlook files. It can also be useful if you are looking for an attachment contained in a message that you deleted by mistake.
For Outlook 2010 running under Windows 7 go to:
C:\Users\UserName\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Outlook
In this folder you will find that there is one or multiple folders named with a random combination of numbers and letters, for example: 82UOSVIP
Inside this directory you will find attachments and pictures stored by Outlook.
If you are curious. This is stored in a registry entry:
HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\14.0\Outlook\Security
Value Name: OutlookSecureTempFolderData Type: REG_SZ
If you need more info, or you are looking for the temp folder in previous versions of Outlook, go to:
http://support.microsoft.com/kb/817878
CodeProject
Wednesday, October 19, 2011
Executing a Stored Procedure from a WCF Service Method
Here is an example of a method that is used within a WCF Service to execute a Stored Procedure and parse the return value (a success/failure string) to a local variable which can be manipulated to display a friendly success or failure message.
Now, let's review the above method. There are a few interesting things:
Once the connection is open, the SQLCommand has to be defined as a CommandType = CommandType.StoredProcedure. This signals the type of action we need to execute.
Next, we define the Stored Procedure to be executed by defining the CommandText="sp_YourSPName".
To get the return value, we set the string variable to the results of a SQL Command, ExecuteScalar ()
But wait. What should we do if our Stored Procedure returns a result set rather than a single value?
Well, you need to make a slight change to the method, we need to add a DataAdapter and a DataSet to parse the result.
See below:
Hope this is helpful,
Will
CodeProject
public String fSave (string aParm1, string aParm2, string aParm3, Int32 aParm4) { SqlConnection lSQLConn = null; SqlCommand lSQLCmd = new SqlCommand(); string lsResponse = ""; string connStr = ""; connStr = ConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString; try { // create and open a connection object lSQLConn = new SqlConnection(connStr); lSQLConn.Open(); //The CommandType must be StoredProcedure if we are using an ExecuteScalar lSQLCmd.CommandType = CommandType.StoredProcedure; lSQLCmd.CommandText = "sp_YourSPName"; lSQLCmd.Parameters.Add(new SqlParameter("@Parm1", aParm1)); lSQLCmd.Parameters.Add(new SqlParameter("@Parm2", aParm2)); lSQLCmd.Parameters.Add(new SqlParameter("@Parm3", aParm3)); lSQLCmd.Parameters.Add(new SqlParameter("@Parm4", aParm4)); lSQLCmd.Connection = lSQLConn; //Executes the SP and returns the single select output to a variable lsResponse = Convert.ToString(lSQLCmd.ExecuteScalar()); } catch (Exception Exc) { return "Error: " + Exc.Message; } finally { lSQLCmd.Dispose(); lSQLConn.Close(); } if (String.IsNullOrEmpty(lsResponse)) { return "Error: Unspecified problem while adding task."; } return lsResponse; }
Now, let's review the above method. There are a few interesting things:
Once the connection is open, the SQLCommand has to be defined as a CommandType = CommandType.StoredProcedure. This signals the type of action we need to execute.
Next, we define the Stored Procedure to be executed by defining the CommandText="sp_YourSPName".
To get the return value, we set the string variable to the results of a SQL Command, ExecuteScalar ()
But wait. What should we do if our Stored Procedure returns a result set rather than a single value?
Well, you need to make a slight change to the method, we need to add a DataAdapter and a DataSet to parse the result.
See below:
SqlConnection lSQLConn = null; SqlCommand lSQLCmd = new SqlCommand(); //Declare a DataAdapter and a DataSet SqlDataAdapter lDA = new SqlDataAdapter(); DataSet lDS = new DataSet(); //...Execution section // create and open a connection object lSQLConn = new SqlConnection(connStr); lSQLConn.Open(); //The CommandType must be StoredProcedure if we are using an ExecuteScalar lSQLCmd.CommandType = CommandType.StoredProcedure; lSQLCmd.CommandText = "sp_YourSPName"; lSQLCmd.Parameters.Add(new SqlParameter("@Parm1", aParm1)); lSQLCmd.Parameters.Add(new SqlParameter("@Parm2", aParm2)); lSQLCmd.Parameters.Add(new SqlParameter("@Parm3", aParm3)); lSQLCmd.Parameters.Add(new SqlParameter("@Parm4", aParm4)); lSQLCmd.Connection = lSQLConn; //Fill the DataAdapter with a SelectCommand lDA.SelectCommand = lSQLCmd; lDA.Fill(lDS);
Hope this is helpful,
Will
CodeProject
Thursday, October 13, 2011
Reading Keys from App.Config
I would like to share a small code snippet that is useful if you need to read entries from a configuration file, for example: App.config
Below are the entries I want to read from my App.config
Below is the best way I've found so far to read these entries in C#:
Keep in mind that you need to add a reference to the System.Manager assembly for this to work.
I've tested this with the .NET Framework 3.5 and 4.0
HTH,
Will
CodeProject
Below are the entries I want to read from my App.config
<?xml version="1.0"?> <configuration> <appSettings> <add key="MyServiceURL" value="http://10.12.64.190:8086/MyService"/> </appSettings> <startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
Below is the best way I've found so far to read these entries in C#:
//Scan Thru the keys and use the Configuration Manager to make this happen foreach (string key in ConfigurationManager.AppSettings) { //Get your key string value = ConfigurationManager.AppSettings[key]; this.Url = value; }
Keep in mind that you need to add a reference to the System.Manager assembly for this to work.
I've tested this with the .NET Framework 3.5 and 4.0
HTH,
Will
CodeProject
Saturday, October 8, 2011
Sample LINQ with Multiple Joins and Selected Columns
If you are like me, I sometimes find difficult remembering the syntax of LINQ statements because I jump from T-SQL to ASP.Net and C#.
I was searching for a sample that may show how to write a LINQ statement with multiple joins and I had little luck finding it, so finally after I put my few brain cells in action, I came up with the following:
var dbRegCourses = (from a in db.CourseRegistries
join b in db.Courses on a.courseid equals b.id
join c in db.aspnet_Users on a.userid equals c.UserId
where a.userid == sUserID
orderby a.regdate, b.code, b.description,
b.instructor, b.date, b.venue
select new
{
a.regdate, b.code, b.description,
b.instructor, b.date, b.venue});
if (dbRegCourses.Count() > 0)
{
ResultLbl.Text = "We found that you are registered to: " +
dbRegCourses.Count().ToString() + " Courses.";
return;
}
If you notice, here we are joining three tables, using a where statement and then pick and choosing columns from at least two tables.
I also added an if statement bottom to see if I got any rows back from the LINQ Statement and if that's the case return a message.
Hope this helps somebody out there,
Will
CodeProject
I was searching for a sample that may show how to write a LINQ statement with multiple joins and I had little luck finding it, so finally after I put my few brain cells in action, I came up with the following:
var dbRegCourses = (from a in db.CourseRegistries
join b in db.Courses on a.courseid equals b.id
join c in db.aspnet_Users on a.userid equals c.UserId
where a.userid == sUserID
orderby a.regdate, b.code, b.description,
b.instructor, b.date, b.venue
select new
{
a.regdate, b.code, b.description,
b.instructor, b.date, b.venue});
if (dbRegCourses.Count() > 0)
{
ResultLbl.Text = "We found that you are registered to: " +
dbRegCourses.Count().ToString() + " Courses.";
return;
}
If you notice, here we are joining three tables, using a where statement and then pick and choosing columns from at least two tables.
I also added an if statement bottom to see if I got any rows back from the LINQ Statement and if that's the case return a message.
Hope this helps somebody out there,
Will
CodeProject
Subscribe to:
Posts (Atom)