Friday, April 30, 2010

"Modified" Rotating ads automatically for every 10 minutes in ASP.Net C#

Found some solution to write compact code for the Rotating ads. This topic is to explain how well we can code this is called CLEVER programming.
In the previous topic there is If -Else ladder,



Now,
if (time == 0)
      tempTime = 1;
   else
       tempTime = (int)(Math.Floor((double)((time - 1) / 10)))+1;




Here Math.Floor cannot be used on integer so converting integer to double explicitly is necessary and then whole expression is converted to integer explicitly because "tempTime" is a integer variable,

The above code is replacement over this:

if (time >= 00 && time <= 10) //
              tempTime = 1; 
            else
                if (time >= 11 && time <= 20)
                    tempTime = 2;
                else
                    if (time >= 21 && time <= 30)
                        tempTime = 3;
                    else
                        if (time >= 31 && time <= 40)
                            tempTime = 4;
                        else
                            if (time >= 41 && time <= 50)
                                tempTime = 5;
                            else
                                   tempTime = 6;









Monday, April 5, 2010

Sending Email from your website, Feed back form, ASP.NET, C#

I was thinking to make the email part better in ByJoel.com, before it was just a link to Microsoft Outlook. I've added the email form and set up the SMTP, now I can receive feedback directly to my inbox. see here : www.byjoel.com/FeedBack.aspx



Sending feed back email WebForm controls:
I have built the User Interface using ASP.Net WebForm controls as shown above. As you can see there are four text boxes, one multi line text box and a Submit button. I have also applied proper ASP.Net Validation controls to avoid errors. 
The "Name", "Email" and "Subject" text boxes are normal text boxes where user can insert the values. but "To" text box has read only property and email address of our web admin, so that user can not change the "To" address.      


Code: 

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MailMessage objEmail = new MailMessage();
        objEmail.To = txtTo.Text;
        objEmail.From = txtEmail.Text;
        objEmail.Subject = txtSub.Text;
        objEmail.Body = "Name: " + txtName.Text + "

" + "Comments: " + txtComments.Text; objEmail.Priority = MailPriority.High; objEmail.BodyFormat = MailFormat.Html; // Make sure you have appropriate replying permissions from your local system. // or contact your web hosting and get the SMTP server name, I have added our SMTP server name SmtpMail.SmtpServer = "localhost"; try { SmtpMail.Send(objEmail); Response.Redirect("OnSale.aspx"); //for testing you can add Response.Write("Feed back sent"); //But it is not advised for working website, So i have redirected to other page. } catch (Exception exc) { Response.Redirect("Default.aspx"); //here exception object created but never used, //it can be used as Response.Write("Send failure: " + exc.ToString()); } }