Monday, February 11, 2008

Selecting Text

You can do selection like above image by holding "alt" key and doing the selection


Format your code

You can format your code by selecting the code you want to format
and pressing ctl + K +F.

This shortcut can also be applied to html


Adding required validator to DropDownList

You can set a required field validator to a drop down list as follows

<asp:DropDownList ID="ddlItems" runat="server">
<asp:ListItem Text="<--Select Item-->" Value=""></asp:ListItem>
<asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Item 3" Value="3"></asp:ListItem>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="ddlItems" runat="server" ErrorMessage="Please select an item" Text="Pelase select an Item"></asp:RequiredFieldValidator>

Friday, February 8, 2008

Create custom web part (part I)

Add a class library and set its output path (right click on the project name, properties, Built tab) to the bin folder of the sharepoint web site you want to deploy it on (wwwrootwss\VirtualDirectories\ [port#])

Add reference to System.Web
Add the following using statements
Inherit your class from WebPart class

namespace BilalWebParts
public class MyWebPart : WebPart
GridView gv;
protected override void CreateChildControls()
gv = new GridView();
protected override void Render(HtmlTextWriter writer)
writer.Write("<table border='1' bordercolor='black'>");
writer.Write("Students List");
protected void BindStudents()
string connectionString = "UID=sa;PWD=P@ssw0rd;Initial Catalog=test;Data Source=MOSS;";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("select * from Students", con);
cmd.CommandType = CommandType.Text;

DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);

gv.DataSource = dt;
Build your application
Add the following line to safe control block in the web.config of the sharepoint website you want to add the webpart to.
<SafeControl Assembly="BilalWebParts" Namespace="BilalWebParts" TypeName="*" Safe="True" />
Modify trust tag in web.config
<trust level="WSS_Medium" originUrl="" />
Adding WebPart:
Open sharepoint site
Go to site settings, then go to webparts link under Galleries tab, click new and check BilalWebParts.MyWebPart then click populate gallery.
Go back to you site and Edit Page then Add a webpart, search for MyWebPart and click ok.Students List will apear

Best Regards

Thursday, February 7, 2008

Importing data from active directory

DirectoryEntry oRoot = new DirectoryEntry
//My domain name is called
//I have OU Engineering and below I have Development

DirectorySearcher oSearcher = new DirectorySearcher(oRoot);
SearchResultCollection oResults;
oResults = oSearcher.FindAll();

for (int i = 0; i < oResults.Count; i++)
if (oResults[i].GetDirectoryEntry().Properties["cn"].Value != null)
Response.Write(oResults[i].GetDirectoryEntry().Properties["cn"].Value + " , ");
Variables you can use:
[0]: "homemdb"
[1]: "countrycode"
[2]: "cn"
[3]: "msexchuseraccountcontrol"
[4]: "mailnickname"
[5]: "msexchhomeservername"
[6]: "msexchhidefromaddresslists"
[7]: "msexchalobjectversion"
[8]: "usncreated"
[9]: "objectguid"
[10]: "msexchrequireauthtosendto"
[11]: "whenchanged"
[12]: "memberof"
[13]: "accountexpires"
[14]: "displayname"
[15]: "primarygroupid"
[16]: "badpwdcount"
[17]: "objectclass"
[18]: "instancetype"
[19]: "msmqdigests"
[20]: "objectcategory"
[21]: "samaccounttype"
[22]: "whencreated"
[23]: "lastlogon"
[24]: "useraccountcontrol"
[25]: "msmqsigncertificates"
[26]: "samaccountname"
[27]: "userparameters"
[28]: "mail"
[29]: "msexchmailboxsecuritydescriptor"
[30]: "adspath"
[31]: "lockouttime"
[32]: "homemta"
[33]: "description"
[34]: "msexchmailboxguid"
[35]: "pwdlastset"
[36]: "logoncount"
[37]: "codepage"
[38]: "name"
[39]: "usnchanged"
[40]: "legacyexchangedn"
[41]: "proxyaddresses"
[42]: "userprincipalname"
[43]: "admincount"
[44]: "badpasswordtime"
[45]: "objectsid"
[46]: "msexchpoliciesincluded"
[47]: "mdbusedefaults"
[48]: "distinguishedname"
[49]: "showinaddressbook"
[50]: "givenname"
[51]: "textencodedoraddress"
[52]: "lastlogontimestamp"


Confirmation before leaving a page

What if the user didn't hit save before navigating to another page, what will happen then?

Below is a solution for this commonly asked question, hope it will help.

Add the following javascript to your page
<script type="text/javascript">
var needToConfirm = false;//Flag to check if save button is hit or changes has happened
window.onbeforeunload = confirmExit;//calling confirmExit function onbefore unload
function confirmExit()
if (needToConfirm==true)//Save button was not hit or changes to the page has happened
return "--------------------------------------------------\n" +
"Warning:\n" +
"If you have made any changes to this page without clicking the Save button, your changes will be lost\n" +

-Add some controls to your page

-Setting needToConfirm flag to true if any changes happened(code behind)
private void AddAttributes()
txtName.Attributes.Add("onkeyup", "needToConfirm=true");
txtEmail.Attributes.Add("onkeyup", "needToConfirm=true");
txtAddress.Attributes.Add("onkeyup", "needToConfirm=true");
ddlTests.Attributes.Add("onchange", "needToConfirm=true");
Now Open your page and write anything in any textbox, donot hit save and close your browser.

Confirmation message will appear asking you if you really want to leave the page or not


Sunday, February 3, 2008

My First Blog

Hello everyone....

This is my first post......

This blog will mostly contain information regarding .net technologies.

Hope my information will be helpful.

Keep your eyes here ....