Friday, February 18, 2011

Insert query return SCOPE_IDENTITY()

This is a example of how to get [table]ID back when you inserted a record to table.

this is very useful when you needed to do more business logic after table inserting.


ALTER PROCEDURE [dbo].[uspNewsletterSubscriptions_Insert]

@TitleId int,
@Forename nvarchar(255),
@Surname nvarchar(255),
@EmailAddress nvarchar(255),
@Id int OUTPUT

AS
BEGIN

insert into tNewsletterSubscriptions(TitleId, Forename, Surname, EmailAddress)
values(@TitleId, @Forename, @Surname, @EmailAddress)


SET @Id = SCOPE_IDENTITY()

SELECT [Id]
,[Created]
,[LastUpdated]
,[TitleId]
,[Forename]
,[Surname]
,[EmailAddress]

FROM tNewsletterSubscriptions
WHERE Id = SCOPE_IDENTITY()

END

Thursday, February 17, 2011

Email Templates with C# asp.net

From my expected, GlobalRescource .resx file is obviously handy to store email templates.

You write whatever you want that Html email template or text email template in eg(contorlsculture.resx) under App_GlobalResources.

And follows following codes -

private string FormatEmailBody(StringBuilder emailBody)
{
emailBody.Replace("{jobtitle}", HttpUtility.HtmlEncode(drVacancy.Title));
emailBody.Replace("{reference}", HttpUtility.HtmlEncode(drVacancy.Reference));
emailBody.Replace("{location}", HttpUtility.HtmlEncode(drVacancy.Location));
emailBody.Replace("{salary}", HttpUtility.HtmlEncode(drVacancy.Salary));

emailBody.Replace("{title}", HttpUtility.HtmlEncode(txtTitle.Text));
emailBody.Replace("{firstname}", HttpUtility.HtmlEncode(txtFirstName.Text));
emailBody.Replace("{surname}", HttpUtility.HtmlEncode(txtSurname.Text));
emailBody.Replace("{email}", HttpUtility.HtmlEncode(txtEmail.Text));
emailBody.Replace("{telephone}", HttpUtility.HtmlEncode(txtTelephone.Text));
emailBody.Replace("{message}", HttpUtility.HtmlEncode(txtMessage.Text));

return emailBody.ToString();
}

private void SendEmail()
{
// Sends Job application to recruiter
string strEmailBody = FormatEmailBody(new StringBuilder(Resources.ControlsCulture.JobApplicationTemplate));
EmailHelper.SendMail(drVacancy.Title + " Job - Application", strEmailBody, new string[] { ConfigHelper.RecruitmentEmail }, null, null,
ConfigHelper.NoReplyEmail.ToString(), fuApplicationForm.PostedFile, fuCV.PostedFile, fuOpsForm.PostedFile);


txtTitle.Text = txtFirstName.Text = txtSurname.Text = txtEmail.Text = txtConfirmEmail.Text =
txtTelephone.Text = txtMessage.Text = "";
phMessage.Visible = true;
}

protected void lkbSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
SendEmail();
}
}

That takes you get sucessful email sending process.


But on the other hand, some opposite idea programmer wants to use a physical files for email templates.

Then creates template files under App_data folder.
now a little bit needs to update the code of sending email. Basically, uses IO and stringbuilder class.

Something like that :

private string FormatEmailBody(StringBuilder emailBody)
{
emailBody.Replace("{contact}", HttpUtility.HtmlEncode("Administrator"));
emailBody.Replace("{name}", HttpUtility.HtmlEncode(txtTitle.Text + " " + txtFirstName.Text + " "
+ txtSurname.Text));
emailBody.Replace("{email}", HttpUtility.HtmlEncode(txtEmail.Text));
emailBody.Replace("{housename}", HttpUtility.HtmlEncode(txtHouseName.Text));

emailBody.Replace("{street}", HttpUtility.HtmlEncode(txtNumberStreet.Text));
emailBody.Replace("{area}", HttpUtility.HtmlEncode(txtLocation.Text));
emailBody.Replace("{town}", HttpUtility.HtmlEncode(txtCity.Text));
emailBody.Replace("{county}", HttpUtility.HtmlEncode(txtCounty.Text));
emailBody.Replace("{postcode}", HttpUtility.HtmlEncode(txtPostcode.Text));
emailBody.Replace("{telephone}", HttpUtility.HtmlEncode(txtTelephone.Text));
emailBody.Replace("{enquiry}", HttpUtility.HtmlEncode(txtEnquiry.Text));

return emailBody.ToString();
}

private void SendEmail()
{
string sFullTemplatePath = HttpContext.Current.Server.MapPath(drFormEmails.EmailTxtFileName);
StringBuilder sbTemplate = new StringBuilder();
using (StreamReader sr = new StreamReader(sFullTemplatePath))
{
sbTemplate = new StringBuilder(sr.ReadToEnd());
}

string strEmailBody = FormatEmailBody(sbTemplate);
EmailHelper.SendMail("Contact us", strEmailBody, new string[] { drFormEmails.MailTo }, null, null,
drFormEmails.MailFrom);
}

protected void lbSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
SendEmail();
Response.Redirect("/Contact-us-confirmation");
}
}


Any better suggestion, please comment it. I will reply you.

Thursday, February 10, 2011

how to display a value to textbox whos textmode is set to 'password' in c#

The code given below is to check the username n display the password via cookie. It works perfectly allright. since i want the value displayed in the password.text to be masked, i changed the textmode of the password.text to 'password'. as a result value isnt passed to the textbox.

Question: String cookiename = TextBox1.Text;

//grab cookie
HttpCookie cookie = Request.Cookies[cookiename];

//exists?
if (null == cookie)
{
Label1.Text = "cookie not found";
}
else
{
password.Text=cookie.Value.ToString();

}

Answer:

HttpCookie myCookie = new HttpCookie("MyTestCookie");
myCookie = Request.Cookies["MyTestCookie"];

TextBox2.Attributes.Add("value", cookie.Value.ToString());


The answer is just one line. but it saves a lot of time.

Monday, February 7, 2011

publicity event fire c#

publicity event fire



In Usercontrol


Declare this:

public event EventHandler FileUploaded;


In some of click event of usercontrol

// Fire public event.
FileUploaded(this.FileResourceId, e);




On .aspx page


On the page load

writes instance to know usercontrol first:

WRVS.Website.V1.WebApplication.Admin.inc.usercontrols.FileUploader uFileUploader =
(WRVS.Website.V1.WebApplication.Admin.inc.usercontrols.FileUploader)
fvContentPage.FindControl("uFileUploader");


if (uFileUploader != null)
{
uFileUploader.FileUploaded += new EventHandler(FileUploaded);
}



Then create event:

private void FileUploaded(object sender, EventArgs e)
{

// logic for page update.
}