Josh-CO Dev

Solving the worlds problems one line of code at a time.


Leave a comment

XNA – Make a sprite face your mouse

I decided that I wanted to start toying around with an overhead type shooter game and see how hard it would be to roll something out in XNA. As for the basics, I found it to be quite simple and thought I would show it off here. After this guide, you should end up with something like this:

Continue reading

Advertisements


Leave a comment

LINQ in the Business Database World

I have come across two dilemmas lately in my professional career, the first of which is that I love LINQ. The second dilemma is that the way we do business does not provide us with much of a need for it. Really, these two aren’t dilemmas on their own, but combine the two and the problems begin to arise. All of our apps are generally just a front end to a database. We fetch data via a dataset and use this for all databinding and pass all CRUD operations through a webservice to call stored procs. With this type of a model, LINQ just isn’t that efficient unless you want to pull all of your data at once and then parse through your collection, which can be cumbersome on the initial load for a large table.

There is one place where I have found good use for LINQ, in batch jobs where I am pulling data from one source and posting it to another, particularly if I need to scour the data. Below is some LINQ code that I have used to take a dataset that I have pulled in from a source database, scour out any rows that have a null or <0 value, then return the results in a datatable. I tried this many other ways and LINQ was by far the fastest. This operation will run on over 100,000 records in mere seconds.

 

private DataTable scourDatasetLINQ(DataSet ds)

{

try

{

var results = from myRow in ds.Tables[“PIData”].AsEnumerable()

where myRow.Field<double?>(“value”) != null && myRow.Field<double?>(“value”) >= 0

select myRow;

 

DataTable dt = results.CopyToDataTable<DataRow>();

return dt;

}

catch (Exception ex)

{

LogExceptionToFile(“CEMSBatch”, ex, 30);

return null;

}

}


Leave a comment

Sending Emails from Silverlight

One of the requirements of my application I am writing is to send emails upon the creation of a new comment. This turned out to be quite simple. I am using RIA services and a Domain Service. In my domain service, I simply added the following code. I have noticed that this does not seem to work through GoDaddy, but it does test fine from my machine.

[Invoke]
public string SendEmail(string to, string body, string subject)
{
try
{
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(to));
msg.From = new MailAddress(“noreply@josh-co.com”);
msg.Body = body;
msg.Subject = subject;
SmtpClient smtp = new SmtpClient();
smtp.Host = “smtp.gmail.com”;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential(“gmailaccount”, “gmailpassword”);
smtp.Port = 587;
smtp.Send(msg);
return “Success”;
}
catch (Exception ex)
{
return ex.ToString();
}
}

 


Leave a comment

Silverlight Image Source Binding

I figured out something that just happened to be a life saver yesterday. I have been working on a more social tool for my workout tracking program. One thing I wanted was a comment system that would show the user’s profile picture. I spend forever trying to think of a way to loop through the list box. What I ended up with was much more simple:

 Here is the XAML:

<StackPanel Orientation=”Horizontal”>

<Image Grid.Row=”1″ Height=”64″ Source=”{Binding ImagePath}” HorizontalAlignment=”Left” Margin=”5,5,0,0″ x:Name=”imgProfileImage” Stretch=”Fill” VerticalAlignment=”Top” Width=”64″ />

</StackPanel>

The backend code simply sets the ImagePath binding to a path that is stored in the database.

 


Leave a comment

XNA – Code Example. Moving a Sprite with Scrolling Backgrounds

So I decided to upgrade my project last night and add sprite movement. The ability has been added to control a sprite, left and right only, on the screen along with the scrolling background. If the character sprite reaches 150 pixels from either side of the screen, the sprite movement will halt and only the background will scroll. The next step is to implement some basic jumping. Then I think a tile engine needs to be built to handle ground objects, and then some gravity to hold the character on those objects. The background logic still needs to be seperated off into a different class as well.

The biggest changes here are that we upgraded the sprite class to handle some additional methods. We also added an inherited class hero to handle the main character. The biggest chunk of logic is as follows:

Continue reading