Interesting programming ideas, solutions, and logic that I have used to solve problems or have come across throughout my career.
About Me
- William Andrus
- Northglenn, Colorado, United States
- I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).
Thursday, September 20, 2007
Regular Expression App
http://williamandrus.tripod.com/RegEx.html
Wednesday, September 12, 2007
Reminder: Bulk insert through a store procedure
This is accomplished by using XML in the store procedure and sending it in through the ADO.Net
In the C# code:
//Make xml
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]))
{
SqlCommand cmd = new SqlCommand("insBulkFTSEDCFileDetail", conn);
cmd.CommandType = CommandType.StoredProcedure;
StringBuilder sb = new StringBuilder();
sb.Append("
foreach (oEDCItem item in edc)
{
sb.AppendFormat("
item.ModuleName, item.PartNumber, item.SerialNumber, item.TestPosition, item.SupplierName, item.EDC, item.Opt);
}
sb.Append("
cmd.Parameters.AddRange(new SqlParameter[] {
new SqlParameter("@EDCFileID", EDCFileID),
new SqlParameter("@LastUpdateUserID", UserID),
new SqlParameter("@XMLDOC", sb.ToString())});
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
The stored procedure:
Create Procedure insBulkFTSEDCFileDetail
{
@EDCFileID int,
@LastUpdateUserID int,
@XMLDOC varchar(MAX)
}
AS
DECLARE @xml_handle int
EXEC sp_XML_preparedocument @xml_handle OUTPUT, @XMLDOC
INSERT INTO [TABLE]
(EDCFileID,
ItemType,
Model,
SerialNumber,
EDCPosition,
SupplierName,
EDC,
Options,
LastUpdateDate,
LastUpdateUserID)
SELECT @EDCFileID,
EDCXML.ItemType,
EDCXML.Model,
EDCXML.SerialNumber,
EDCXML.EDCPosition,
EDCXML.SupplierName,
EDCXML.EDC,
EDCXML.Options,
getUTCDate(),
@LastUpdateUserID
FROM OPENXML( @xml_handle, '/EDCItems/EDCItem')
WITH ( ItemType varchar(8),
Model varchar(50),
SerialNumber varchar(50),
EDCPosition varchar(50),
SupplierName varchar(50),
EDC varchar(50),
Options varchar(80)) AS EDCXML
EXEC sp_XML_removedocument @xml_handle
Tuesday, September 11, 2007
Quick reminder: Implement ICallbackEventHandler
The page source (client-side):
<script language="javascript" type="text/javascript">
function Save()
{
var message = '';
var context = '';
<%=sCallBackFunctionInvocation%>
}
function Result(result, context)
{
if(result != 'ok')
alert("Error: " + result + "\n\nFile was not saved.");
}
function OnError(message, context)
{
alert('An unhandled exception has occurred:\n' + message);
}
</script>
The code-behind (server-side):
public partial class EDCWUC : System.Web.UI.UserControl, ICallbackEventHandler
{
public string sCallBackFunctionInvocation;
protected void Page_Load(object sender, EventArgs e)
{
sCallBackFunctionInvocation = this.Page.ClientScript.GetCallbackEventReference(this, "message", "Result", "context", true);
}
#region ICallbackEventHandler Members
public string GetCallbackResult()
{
return sCallBackFunctionInvocation;
}
public void RaiseCallbackEvent(string eventArgument)
{
sCallBackFunctionInvocation = DoServerSideEvent();
}
#endregion
}
A good hint: If you need to do several callbacks, then set it up so that the eventArgument sends in the function name with its values, then all you need to do is split it, get the function name and send in the necessary values
For example:
FunctionName;<uniqueDelimeter>;Values
Saturday, September 08, 2007
Some Interview Questions given to me in the past.
2. What is ViewState? How is it encoded? Is it encrypted? Who uses ViewState?
3. Demonstrate definition of a property in a class (in c#)
4. Demonstrate populating a dropdownlist from an xml file (in c#)
5. How do you assign a static IP Address to a Windows 2003 Server?
6. Draw out what the outcome of an Inner Join, Left Outer Join and Right Outer Join would be on two tables.
7. How would you access the database connection strings from a web.config file in VS 2003 and VS 2005?
8. Where would you setup “Impersonation” and when would it be a good time to use it?
9. How many static IP Addresses can a Windows 2003 Server have?
10. A client needs a web page that collects the following: first name, last name, phone number and then pushes the data to them via email. Please provide sample code of a form that sends that data to the client.
11. Demonstrate in Asp.Net how to pull a remote user’s IP Address, Referring URL, and the [cookieid] from a user’s machine for domain [SAN].
12. Please provide an example of a time when you have ever used web services.
13. How would you go about debugging a web service SOAP Message?
14. How do you view the methods and properties of a web service within Visual Studio?
1) What is a postback?
http://www.xefteri.com/articles/show.cfm?id=18 does a nice job in explaining what a postback is, in detail.
In my own words: In a standard postback a web page, with a web form is submitted and then processed on the server-side. The server-side then updates the page on the client-side by reposting the data. This is usually seen with something similar to a page refresh. (Which is why, if you are buying something on the web, it's a good idea not to hit refresh button or it would duplicate the last sent request.)
Now with, javascript and nowadays Ajax, it isn't necessary to refresh the whole page. It is possible to just refresh a certain control, allowing the user to see updated information without refreshing and redrawing other controls.
2) What is ViewState? How is it encoded? Is it encrypted? Who uses ViewState?
In my own words:
A viewstate is a serializable hashtable of objects that are used to store information on the client-side. During a postback, the informatin is then used on the server-side before reposting to the client-side. This allows the data found in controls like a textbox to be repoulated, or to use the viewstate to store global variables which can be reused.
A viewstate is stored in base64, and encryption by default is set to auto. This means, as long as no control in the form does not need encryption, then the whole viewstate is unencrypted; otherwise the whole viewstate is encrypted. You can set the view state for each page:
<%@Page ViewStateEncryptionMode="Always" %>
, or you can set it for the whole website by using the web.config:
<configuration>
<system.web>
<pages ViewStateEncryptionMode="Always" />
</system.web>
</configuration>
3) Demonstrate definition of a property in a class
class MyClass
{
private string _name = “”;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
4) Demonstrate populating a dropdownlist from an xml file
Not tested:
XML file:
<library>
<book>
<author>Dietel & Dietel</author>
<title>C how to program</title>
</book>
<book>
<author>William Shakespare</author>
</book>
</library>
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Library.xml"));
XmlNodeList nodeList = doc.SelectNodes("library/book/"); //xslt path
foreach(XmlNode node in nodeList)
{
DropDownListTitle.Items.Add(new ListItem(node.SelectSingleNode("title").InnerText));
}
5) How do you assign a static IP Address to a Windows 2003 Server?
I personally don't know much about setting up servers, I'm a developer. So, I look it up:
http://technet2.microsoft.com/windowsserver/en/library/36c35b2c-8b8b-4517-812e-913a60ff551d1033.mspx?mfr=true
The basic info provided:
To configure TCP/IP for static addressing
1.
Open Network Connections.
2.
Right-click the network connection you want to configure, and then click Properties.
3.
On the General tab (for a local area connection) or the Networking tab (for all other connections), click Internet Protocol (TCP/IP), and then click Properties.
4.
Click Use the following IP address, and do one of the following:
•
For a local area connection, in IP address, Subnet mask, and Default gateway, type the IP address, subnet mask, and default gateway addresses.
•
For all other connections, in IP address, type the IP address.
5.
Click Use the following DNS server addresses.
6.
In Preferred DNS server and Alternate DNS server, type the primary and secondary DNS server addresses.
6) Draw out what the outcome of an Inner Join, Left Outer Join and Right Outer Join would be on two tables.
Can't draw


7) How would you access the database connection strings from a web.config file in VS 2005? (Don't care about VS 2003 anymore :P )
<appSettings>
<add key="sqlConn" value="Server=myPc;Database=Northwind" />
<add key="smtpServer" value="smtp.mydomain.com" />
</appSettings>
If I need to access it in the program:
this.sqlConnection1.ConnectionString = ((string)( System.Configuration.ConfigurationSettings.AppSettings.GetValues("sqlConn.ConnectionString"))));
8) Where would you setup “Impersonation” and when would it be a good time to use it?
Tuesday, September 04, 2007
Lorem Ipsum
The use of using this text for dummy text has been around since 1500, which was taken from a book "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
http://www.lipsum.com/
What is also cool about this site, it has a Lorem Ipsum generator.
Monday, August 27, 2007
How to programatically bind a value to a parameter in a ObjectDataSource
From here I just add the value:
protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
e.InputParameters["IP"] = Request.UserHostAddress;
}
Friday, August 24, 2007
File Download in ASP.Net
So on my download page I have:
protected void Page_Load(object sender, EventArgs e)
{
String filepath =
Request.Params.Get("Author") + "-" + Request.Params.Get("Title") + ".mp3";
if
(filepath.Length != 0)
{
Stream readStream =
MusicManager.GetSong(Int32.Parse(Request.Params.Get("file")));
int Length =
256;
Response.Clear();
Response.ContentType =
"application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=\"" + filepath + "\"");
Response.Flush();
Byte[]
buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0,
Length);
while (bytesRead > 0)
{
Response.OutputStream.Write(buffer,
0, bytesRead);
bytesRead = readStream.Read(buffer, 0,
Length);
}
Response.End();
readStream.Close();
}
}
Of course I have a page that sends in the query string with the necessary info
This seems all simple when it starts working, but it took me 2 hours to get the file to download correctly.
Wednesday, August 22, 2007
Tuesday, August 21, 2007
Interview question: What structure would you use to represent shapes in a database?
I then started thinking about this question, again. The idea of the question was to see what tables and columns would you set up to make this feasible. I wanted an easy solution with minimal work, so I avoided the polymorphism concept and went to a simpler definition of a shape (polygon).
I figure I would make 1 table called SHAPE with 2 columns and make the assumption that the shapes are defined as "simple polygons":
ID int, COORDINATES varchar
The ID is obviously the primary key for each shape.
The Coordinates is a order list of Cartesian coordinates going counter-clockwise.
Now, given a shape, the most common questions asked would be what is the perimeter and area of said shape. I could easily make 2 store procedures to find the perimeter and area.
To figure out the perimeter, I just need to add up the distance from each point, simple enough.To figure out the area of the polygon, I knew there had to be a formula that would do this for me, and thanks to wikipedia I found one:

"The formula was described by Meister in 1769 and by Gauss in 1795. It can be verified by dividing the polygon into triangles".
I was looking around, to see if others seen this question. I did find something similar at http://lists.mysql.com/mysql/207823
If I needed to query the table for certain shapes, then it would be good to add an additional column called: Points int
So, if I needed to query the table for triangles only then I can do a search on points = 3.