Me, myself and Apple PC

30 January, 2010 | | 3 comments |

Share |
Apple's products have been eluding me since years now(including school & college days) and the biggest pain wasn't about not being able to afford when your friends could but not being able to taste the sweetest thing in Technology. Truth be told and for all to agree - Apple has some revolutionary products under its arsenal and their class is EXTRAORDINARY. Now having expressed my anguish and love for it, I had to move on but AVAILABILITY is still a factor for Apple products here in Mumbai | INDIA. Not to be surprised, my friends having Apple iPod Touch or iPhone get a jumping ovation followed by the curiosity of others(including me) to get their hands on it. So people possessing Apple stuff are considered privileged here, better have them if you're visiting my country.

Some retailers here in Mumbai have now officially started selling Apple products claiming they are the lone official Apple reseller but I'm not interested in that and I consider just barging in to get my hands on the Apple products. Looks, quality, functionality and ease of the Apps., H/W, etc is something I look for and I did manage to get all of these at CHROMA | Bandra | Mumbai. Snapping was strictly prohibited but liberal I am and so I managed to get some. The Apple iMac MC207(we PC enthusiasts can also call it as "Apple Desktop Computer"), Apple Macbook Pro MC950 and Apple Macbook Air retails at 56,900/-*, 64,900/-* and 1,07,000/-* respectively. * Local VAT extra.

All my pictures come with a strict DISCLAIMER :

" I'm not endorsing myself with Apple PC. It's only [ME with Apple PC]. "

Apple Macbook Pro MC950

[Apple Macbook Pro MC950]

Apple iMac MC207

[Apple iMac MC207]

Hardik Shah [Guru] with Apple iMac MC207

Me with [Apple iMac MC207]

Hardik Shah [Guru] with Apple iMac MC207

Me with [Apple iMac MC207]

Hardik Shah [Guru] with Apple iMac MC207

Me with [Apple iMac MC207]

Apple Macbook Air

[Apple Macbook Air]

Hardik Shah [Guru] with Apple Macbook Air

Me with [Apple Macbook Air]

Post a Comment

Insertion problems with single-quotes in MS-SQL and case insensitive comparison in .Net

20 January, 2010 | | 1 comments |

Share |
Problem 1 :

Anybody working with Microsoft SQL Server as the back-end face this annoying issue while using insert statements, especially when they want to insert values containing single quotes within them. Now to clear the concept regarding this issue, the way Microsoft SQL (or say MS-SQL) works is that it treats anything between two single-quotes as a string and hence if the string itself has a single quote then it'll throw an SQL Exception.

1] Consider the following case wherein a simple string is inserted into a column :

INSERT into Student(name) VALUES(' Hardik ');

1 Row INSERTED successfully.

2] In the following case, a string containing a single quote is inserted which throws an SQL Exception :

INSERT into Student(name) VALUES(' Hardik's ');

System.Data.SqlClient.SqlException : Incorrect syntax near 's'. Unclosed quotation mark after the character string ')'.

Solution :

The problem as seen above is silly and so is its solution. If you want to include a single quote included in a string then use the single quotes twice. The single-quotes used twice is treated as a single-quote in MS-SQL. So the solution to the above SQL Query will be as follows :

INSERT into Student(name) VALUES(' Hardik''s ');

1 Row INSERTED successfully.

Now having done with the problem and the solution, a real challenge would be to implement the same in projects using our own programming skills. Now the challenge is to have the single-quotes twice when we have to insert a string containing a single-quote and not when we don't have a single-quote. An important method in .Net and Java which can be used here is the 'replace()' method. The replace method accepts two arguments where the first is the character which you want to replace and the second being the character which you want to replace with.

CODE Snippet in C# :
  2. String s = "Guru says Lolz !!!";
  4. s.Replace("!", ".");
  6. Console.WriteLine(s); // OP : Guru says Lolz ...

If you felt that the above example still doesn't suffice then consider a real-time example in use with SQL.

CODE Snippet :

  2. String s = " Hardik's ";
  4. try
  5. {
  6.     SqlConnection con = new SqlConnection(conn); // where conn is the Connection String
  7.     SqlCommand cmd = new SqlCommand("INSERT into Student(name) VALUES('" + s.Replace("'","''") + "')", con);
  8.     con.Open();
  10.     cmd.ExecuteNonQuery();
  12.     con.Close();
  13. }
  14. catch(Exception ex)
  15. {
  16.     Console.WriteLine("Exception : " + ex);
  17. }

The string [Hardik's] will be successfully inserted in the Column [name] of the Table [Student]. The case of having single-quotes in it is also dealt with appropriately.

Problem 2 :

Another very simple problem which people come across in .Net is when making case-insensitive comparisons between strings(or say its alias "Strings"). If the Itellisense feature in Visual Studio is used wisely then you are not away from the solution but still a lot of people are found searching for the solution over search engines. Also for people who are well versed with Java might recall the 'equalsIgnoreCase()' method but what about its equivalent in .Net?

CODE Snippet :

  2. String s1 = "Guru";
  4. if (s1.Equals("guru", StringComparison.OrdinalIgnoreCase) == true)
  5. {
  6.     Console.WriteLine("The String comparison was successful.");
  7. }

Post a Comment

Sending E-Mails in .NET effectively

13 January, 2010 | | 0 comments |

Share |
The intent of this blog is to enable you to send E-mails leveraging all the features available in .Net Framework. Probably, if you already know how to send an E-mail in .Net then hold on and keep reading because you'll learn some other not-so-usually-known aspects/properties while sending an E-mail. You should be having your IIS configured with your SMTP details on your web-server OR should mention them explicitly in the [web.config] file before sending your first E-Mail. The former option is possible only in fraction of the cases since developers do not have access to the IIS Console on the web-server and hence the latter is the only option which a majority of developers use. The [web.config] method to declare the SMTP details has been included in this chapter. Note that the third option of declaring the SMTP details explicitly in the CODE is also available but as such it is vulnerable to exploits and it also contradicts the very concept of ENCAPSULATION.

The classes and methods for sending an E-mail are defined in the "System.Net.Mail" namespace and are accessible in both Windows and Web Forms(i.e. ASP.Net). The three main classes in use from the namespace while sending an E-mail are the MailAddress, MailMessage and SmtpClient.

The following are the attributes which will come into play while sending an E-Mail :

  • To address
  • CC address
  • Bcc address
  • From address
  • Body of the mail
  • Subject of the mail
  • Priority of the mail
  • Attachments (if any)
  • Does the body have HTML(as in case of Newsletters) ?
  • ReplyTo Address

Make sure the the requisite namespace [System.Net.Mail] is referenced in your code-behind.

CODE Snippet :

  2. try
  3. {
  4.     MailAddress to = new MailAddress("", "Mr. RECEIVER");
  5.     MailAddress from = new MailAddress("", "Mr. SENDER");
  6.     MailAddress cc = new MailAddress("", "Mr. CC");
  7.     MailAddress bcc = new MailAddress("", "Mr. Bcc");
  8.     MailAddress rto = new MailAddress("", "DO NOT REPLY");
  10.     MailMessage mm = new MailMessage(to, from);
  12.     mm.CC.Add(cc);
  13.     mm.Bcc.Add(bcc);
  15.     mm.Subject = "";
  16.     mm.Body = "";
  18.     mm.ReplyTo = rto;  //Specifies the E-Mail address of the SENDER which will be seen if the RECEIVER decides to reply to the mail
  19.     mm.IsBodyHtml = true; // "true" if your body has some Html content. DEFAULT is "false".
  20.     mm.Priority = MailPriority.Normal; //Possible values are "High/Normal/Low" depending upon the priority required. DEFAULT is "Normal"
  22.     mm.Attachments.Add(new Attachment(Server.MapPath("./files/image.jpg")));
  24.     SmtpClient sc = new SmtpClient();
  26.     sc.Send(mm);
  27. }
  28. catch (Exception ex)
  29. {
  30.     Response.Write("Exception : " + ex);
  31. }

[web.config] CODE Snippet :

Sending mails in .Net - 2.JPG

You can also store some requisite variables like the recipient's address, sender's address, signature or say any values in [web.config] for access at runtime.

[web.config] CODE Snippet :

Sending mails in .Net - 1.JPG

To access this global variable from CODE-BEHIND:

  2. String from = System.Configuration.ConfigurationSettings.AppSettings.("FromAddress");

Post a Comment