Monday, December 5, 2011

how to subtract date

Just use the following code:

DateTime dateFrom = new DateTime(1982, 8, 23);
DateTime dateTo = new DateTime(2011, 12, 5);

TimeSpan numberOfDaysInterval = dateTo.Subtract(dateFrom);

double daysAgo = numberOfDaysInterval.TotalDays;

string showDays = daysAgo.ToString("0");

This will just output the integer value of the difference of the dates.

Thursday, October 27, 2011

in queries from sql to linq

try this:

List Ids = new List;
Ids.Add(1);
Ids.Add(2);
Ids.Add(3);

var t = from n in productList
where Ids.Contains(n.Id)
select n;

this is like:

select * from productList where Id in (1, 2, 3)

Tuesday, October 4, 2011

sql error in changing a db structure and saving again

Sometimes, we encounter sql error in changing a db structure and saving again.

The solution is:

Go to Tools -> Options of your SQL Management Studio.

Then, in the Designers -> Tables and Database Designers, uncheck the:

"Prevent saving changes that require table re-creation"

That it! Simple way, but mind boggling.

Thanks for reading!

Monday, September 19, 2011

download Silverlight Developer Runtime

In our programming experience, we need to install Silverlight Developer Runtime to a newly formatted pc.

The following link will get you there:

http://go.microsoft.com/fwlink/?LinkID=188039

or

http://www.silverlight.net/getstarted/ and search windows developer runtime.

Keep on fighting!

Wednesday, July 27, 2011

opening xaml with Source Code (Text) Editor



If you want to speed up your XAML Environment in Silverlight, just Right-Click a XAML file, then Select "Source Code (Text) Editor" and Set it As Default. You will now open the XAML files in Source Code (Text) Editor everytime you double-click a XAML file.

If you now want to open the Design of the XAML. Just select View from the Menu, then Select Designer.

The faster, the better!

Wednesday, July 20, 2011

send email using smtp

This is how we implemented sending email using smtp:

First, we created a public class in the custom classes project, then we declared the constructor like this:

public EmailSender(string host, int port, string username, string password)
{
_smtpClient = new SmtpClient(host, port);
_smtpClient.EnableSsl = false;
_credentials = new System.Net.NetworkCredential(username, password);
_smtpClient.Credentials = _credentials;
}

After that, we created a function that will create the email message:

public void SendEmail()
{
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress("abc@email.com", "Sender FullName");
mailMsg.To.Add(new MailAddress("xyz@email.com", "Recipient FullName"));
mailMsg.Subject = "Sample Email";

var message = new StringBuilder();
message.AppendLine("<span style='font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;'>");
message.AppendLine("Dear Recipient FullName",<br/><br/>");
message.AppendLine("This is a test email.<br/>");
message.AppendLine("Thank you,<br/>");
message.AppendLine("Sender FullName");
message.AppendLine("</span>");
mailMsg.Body = message.ToString();
mailMsg.IsBodyHtml = true;
_smtpClient.Send(mailMsg);
}

And now, we are ready to call that function in the Custom Business Layer using this:

EmailSender emailSender = new EmailSender("smtp.gmail.com", 465, "yourgooglemailname@gmail.com", "yourpassword");
emailSender.SendEmail();

Yey! You can now send an email using your program...

Tuesday, July 19, 2011

format number with 2 decimal places in XAML presentation

In Silverlight XAML presentation, you can use the following code to format number with 2 decimal places in data binding:

<TextBlock Text="{Binding Amount, StringFormat=n, Mode=TwoWay}"/>

Edit:
StringFormat='C'

StringFormat='N3'

StringFormat='yyyy MMM dd'

This sample shows StringFormat markup extension. C for currency, N3 for a number with 3 decimal places and yyy MM dd for a date that displays year 3 letter month and 2 number date.

Keep on typing! Code and code until you succeed!

Monday, July 18, 2011

printing in silverlight 4

Printing is one of the features that was not present in Silverlight 3 and previous versions.

All you have to do is add a reference to:

System.Windows.Printing

Then, add the following code in the .cs file:

void PrintButton_Click(object sender, RoutedEventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += (a, b) =>
{
b.PageVisual = this.grdReport;
};
pd.Print("Print");
}

Make sure that you have a x:Name="grdReport" in your xaml to make it available for printing during runtime. This code will open the Print Dialog box and will show the printers in your network, then just click Print button.

That's all folks! Programming is in your veins, don't stop til your blood is flowing...

Thursday, July 14, 2011

asp .net required field validation on dropdown list

First, in the code-behind, retrieve the DataSource of the dropdownlist but add a new item with "XXX" code and [Please select] name, somewhat like this:

List countries = EnumHelper.CountryList();
Country unselectedCountry = new Country { CountryCode = "XXX", CountryName = "[Please select]" };
countries.Insert(0, unselectedCountry);
ddCountry.DataSource = countries;

Then, create a RequiredFieldValidator in your aspx file with the following properties:

<asp:RequiredFieldValidator runat="server" ControlToValidate="ddCountry" Display="Dynamic" ErrorMessage="*" InitialValue="XXX" ToolTip="Required">
</asp:RequiredFieldValidator>

It will now validate if the dropdown list has no selected index yet because of the added code.

Happy coding!

how to call a messagebox in asp .net code-behind

This is how to call a messagebox in asp .net code-behind:

First, create a javascript in your aspx file:

<script type="text/javascript">
function alertMessage(someval) {
alert(someval);
}
</script>

Then, in code-behind, call the function using this:

ClientScript.RegisterStartupScript(typeof(Page), "myscript2", "");

Happy cooking programmers!

removing time in datetime XAML presentation

In Silverlight XAML presentation, you can use the following code to remove time in a datetime binding:


<TextBlock Text="{Binding EffectivityDate, StringFormat='MM/dd/yyyy', Mode=TwoWay}"/>

Smile! Coding is like playing!