March 2007 - Posts
Today I attended my first community session which discussed WCF (Windows Communications Foundation) held by Tarek Bouhsali, a Microsoft Technical Consultant.
Actually I enjoyed meeting other community members eventhough I was a little bit tired after a long working day ;)
Hope I will continue attending future sessions.
Best Regards,
HC
Have you ever tried to use the Between operator when you needed to filter the DataView object to retrieve a range of records?
When you use
DataView.RowFilter = "ID BETWEEN 1 AND 5";
An error message is displayed "The expression contains unsupported operator 'Between'.". Eventhough if you use this query in SQL Server, it will return a result set.
Walkaround:
You can modify the RowFilter property to use a different filter expression which will return your needed result set
DataView.RowFilter = "ID >= 1 AND ID<=5";
Best Regards,
HC
I just found out that my article Rich Text Editor - Part II is considered the 2nd most viewed article during the last 10 days.
Thank you all for reading and commenting on it. Hope in the future, i will still be able to publish informative articles to better help this community.
Best Regards,
HC
If you are in a process when a Contact Us form is needed, you have to give your users the ability to contact you by sending their comments, feedbacks, etc... Microsoft has provided classes to enable sending email. Before we hit the code, you need to configure your Smtp Server. I found a good faq which will guide you through the steps to configure your smtp server and a lot of more additional help.
First of all, you need to import the namespace System.Net.Mail.
using System.Net.Mail;
Second We will create a method which will do our purpose. This method takes 3 string parameters.
internal void SendEmail(string emailAddress, string subject, string body)
{
// Create an instance of the MailMessage Class
MailMessage contMail = new MailMessage();
// Set from whom the email is sent
contMail.From = new MailAddress(emailAddress);
// Set to whom the email is sent. This variable is saved in the web.config
contMail.To.Add(ConfigurationManager.AppSettings["To"].ToString());
// Set the subject. this value is retrieved from the paramaters
contMail.Subject = subject;
// Set the content of the email.
contMail.Body = body;
// Create a new instance of SmtpClient Class. You can replace 127.0.0.1 by localhost
SmtpClient email = new SmtpClient("127.0.0.1");
// Send the email
email.Send(contMail);
}
After creating the method, You just need to call it whenever you need to.
Don't forget to modify the web.config, just add the below code under the configuration tag.
<appSettings>
<add key="To" value="haissam50@hotmail.com"/>
</appSettings>
Best Regards,
HC
Refactor! is freely available to all ASP.NET 2.0 developers and offers a comprehensive suite of tools that enable you and your team to simplify and shape complex code and HTML markup - making your web applications easier to read and less costly to maintain
To download your copy, Learn more here
HC
In this blog post, i will show you how to get specific user information from the database and bind it to your control.
1- Create a DataTable object with specific Column names.
2- Get all users and save them in the MembershipUserCollection
3- Use the Profilecommon class to retrieve specific user information.
4- Create Datarow for each user in the MembershipUserCollection with the needed information
5- Bind the DataTable to the DataView.
// Create a datatable with specific column names
DataTable dt = new DataTable();
dt.Columns.Add("Username", typeof(string));
dt.Columns.Add("First name", typeof(string));
dt.Columns.Add("Last name", typeof(string));
dt.Columns.Add("City", typeof(string));
dt.Columns.Add("Country", typeof(string));
// Get all users in form of MembershipUserCollection
MembershipUserCollection users = Membership.GetAllUsers();
// Create a new ProfileCommon object
ProfileCommon prof;
// Instantiate a Datarow
DataRow r;
// Loop through all users inside the MembershipUser Collection, Retrieve the needed information, Add the Datarow to the Datatable.
foreach (MembershipUser onlineUser in users)
{
r = dt.NewRow();
prof = Profile.GetProfile(onlineUser.UserName);
r["UserName"] = onlineUser.UserName;
r["First name"] = prof.ProfileInfo.FirstName;
r["Last name"] = prof.ProfileInfo.LastName;
r["City"] = prof.ProfileInfo.City;
r["Country"] = prof.ProfileInfo.Country;
dt.Rows.Add(r);
}
// Set the datasource of the gridview to the datatable and bind it
GridView1.DataSource = dt;
GridView1.DataBind();
Page.DataBind();
Best Regards,
HC
While working on a Photo gallery project, i realized the power of .NET to enable the developer to create thumbnail images in few lines of code. This gave me the advantage to load my webforms without waiting the whole image to be download to the user. Working with big size images is considered a real pain specially when using a slow internet connection. So let's jump directly to the code.
// Get the path of the original Image
string displayedImg = Server.MapPath("~") + "/Testing.jpg";
// Get the path of the Thumb folder
string displayedImgThumb = Server.MapPath("~") + "/Thumb/";
// Get the original image file name
string imgFileName = System.IO.Path.GetFileName(displayedImg);
// Load original image
System.Drawing.Image myimg = System.Drawing.Image.FromFile(displayedImg);
// Get the thumbnail 100 X 100 px
myimg = myimg.GetThumbnailImage(100, 100, null, IntPtr.Zero);
// Save the new thumbnail image
myimg.Save(displayedImgThumb + imgFileName, myimg.RawFormat);
Best Regards,
HC
Throughout my contributions in forums, i noticed that a lot of developers are asking how to create a customized tooltip when a user clicks or mouse hover a specific control. Please find below an implementation.
First, Add the below Markup code inside your html page
<div id="Tooltip" style='DISPLAY:none;WIDTH:200px;HEIGHT:150px' runat='server'></div>
<input type="button" onclick="ShowDiv(event,'this is a text')" value="Testing" style='Z-INDEX: 112; LEFT: 488px; POSITION: absolute; TOP: 560px'>
Above we created a div layer (hidden by default) and a button once clicked the div layer will be shown below this button dynamically. Notice the onclick event is calling a javascript function and sending the message to be displayed inside the customized tooltip.
Javascript Code
Below is a function which will calculate the mouse position when the user click on the button, and we will set the Top and Left properties for the div layer to those coordinates in order to show it under the button, this javascript function takes the click event and the text to be displayed inside the div as parameters
Paste your below code inside the <head> tag
<script language="javascript">
function ShowDiv(e,text)
{
// Get the click location
var height = e.clientY + parseInt('5');
// Get the height inside the clicked image
var offsetHeight = parseInt(e.offsetY);
height = height - offsetHeight;
// Get the click location(width)
var width = e.clientX
// Get the width inside the clicked image
var offsetWidth = parseInt(e.offsetX);
width = width - offsetWidth;
var div = document.getElementById('Tooltip');
div.style.position = 'absolute';
// Set the div top to the height
div.style.top = parseInt(height) + parseInt('20');
// Set the div Left to the height
div.style.left = parseInt(width) + parseInt('20');
// Show the div layer
div.style.display = 'block';
// Set its Text to your desired text
div.innerText = text;
}
</script>
Best Regards,
HC