Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

17/11/2011

Permission level cannot be found

Permission level cannot be found. at Microsoft.SharePoint.SPRoleDefinitionCollection.get_Item(String name)

This message happend when you try set permission to user in sharepoint by code with rong spelling.

Such us: you want set "Contribute" for the user, but you write it: "Contributer".

Read more...

06/04/2011

10 ways to call Javascript code using C# and ASP.NET

There 10 ways to call Javascript code using ASP.NET and C#, you can download the attached files and test all ways.
http://www.4shared.com/file/L8-tE7DV/CallJSCode.html



Read more...

02/11/2010

web service: The test form is only available for requests from the local machine.

if you create aweb service, and you want to test it from another server machine, the follwoing message will be appear:
"The test form is only available for requests from the local machine."
so, you should add the follwoing code into your web.config:
<webServices>
    <protocols>
        <add name="HttpSoap12" />
        <add name="HttpSoap" />
        <add name="HttpGet" />
        <add name="HttpPost" />
    </protocols>
</webServices>


before

</system.web>

Read more...

20/02/2010

Get The Serial Number Of The Hard Disk

You can prevent the user from entering your application but who has the same serial number by using the following code:





protected void Page_Load(object sender, EventArgs e)
{
lblResult.Text = GetHDDSerialNumber("c");
}

/////////////////////////////////////////
public string GetHDDSerialNumber(string drive)
{
//check to see if the user provided a drive letter
//if not default it to "C"
if (drive == "" || drive == null)
{
drive = "C";
}
//create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive + ":\"");
//bind our management object
disk.Get();
//return the serial number
return disk["VolumeSerialNumber"].ToString();
}

Read more...

To know the number of seconds that elapsed since the computer starts

        Int64 SecondsFromStart;

SecondsFromStart = Environment.TickCount / 1000;

lblResult.Text = SecondsFromStart.ToString();

Read more...

Check If The Input Is Numaric Or Not

To check if the input from text box is numaric or not, can we use the following idea:

1- In .aspx page, create TextBox control, and rename it to txtInput.
2- Create Label control, and rename it to lblResult.
3- Create Button control, and rename it to btnCheck.
4- In code behind, write the following code:


    protected void Page_Load(object sender, EventArgs e)
{
if (IsNumeric(txtInput.Text, IsNumericType.Integer))
{
lblResult.Text = "The Input Is Numeric";
}
else
{
lblResult.Text = "The Input Is Not Numeric";
}
}








public enum IsNumericType : int
{
Integer = 0,
Decimal = 1,
Double = 2,
Int64 = 3,
Byte = 4,
Float = 5
}








public static bool IsNumeric(string Input, IsNumericType NumericType)
{
try
{
switch (NumericType)
{
case IsNumericType.Byte:
byte ByteResult = byte.Parse(Input);
return true;
case IsNumericType.Decimal:
decimal DecResult = decimal.Parse(Input);
return true;
case IsNumericType.Double:
double DblResult = double.Parse(Input);
return true;
case IsNumericType.Float:
float FltResult = float.Parse(Input);
return true;
case IsNumericType.Int64:
Int64 I64Result = Int64.Parse(Input);
return true;
case IsNumericType.Integer:
int intResult = int.Parse(Input);
return true;
default:
return false;
}
}
catch { return false; }
}


5- Test it.


Read more...

17/02/2010

The Defrenet between Viewstate, Session, Application, Cache, and Cookies:

1- Viewstate

Viewstate is a hidden fields in an ASP.NET page, contains state of those controls on a page whose "EnableViewstate" property is "true".

You can also explicitly add values in it, on an ASP.NET page like:


Viewstate.Add( "TotalStudents", "87" );


Viewstate should be used when you want to save a value between diferent roundtrips of a single page as viewstate of a page is not accessible by another page.

Because Viewstate renders with the page, it consumes bandwith, so be careful to use it in applications to be run on low bandwith.



2- Session Variable

Session variables are usually the most commonly used.

When a user visits a site, it's sessions starts and when the user become idle or leave the site, the session ends.

Session variables should be used to save and retrive user specefic information required on multiple pages.

Session variables consumes server memory, so if your may have a huge amount visiters, use session very carefully and instead of put large values in it try to put IDs and references

To add session:

Session.Add("The Name Of Session Variable", The Object Value);


To read the session variable:

Session["The Name Of Session Variable"];


3- Application variables

Application variables are shared variables among all users of a web application

Application variables behave like static variables and they are substitute of static variables as static variables are stateless in web applications

Only shared values should be persisted in Application variables, and as soon as they are not in use they should be removed explicitly.

4- Cache

Cache is probably the least used state feature of ASP.NET.

Cache is basically a resource specific state persistence feature, means unlike session it stick with resource instead of user, for instance: pages, controls etc.

Cache should be used or frequently used pages, controls, and data structures

Data cache can be used to cache frequently used list of values e.g. list of products

5- Cookies

Cookies are some values saved in browsers by the website to retrivbbe and use afterwards.

Usually cookies are used to help dynamic websites to identify visitors and retrieve their saved preferences.

Cookies are also used to facilitate auto login by persisting user id in a cookie save in user's browser.

Because cookies have been saved at client side, they do not create performance issues but may create security issues as they can be hacked from browser.

Finally remember the following points on your finger-tips:

•Viewstate is bandwidth hungry

•Session variables are memory hungry as per number of users

•Applications variables are shared

•Cache is memory hungry as per number of resources

•Cookies are the least secure


Read more...

10/02/2010

Problem with Response.Redirect in Sharepoint

To solve the problem with Response.Redirect in Sharepoint, use the following code:

Response.RedirectLocation = "Put The Target Page URL";

Response.StatusCode = 301;



Read more...

Handle the 404 error in Sharepoint 2007

How to handle the 404 error in sharepoint 2007?
The Sharepoint include default page for 404 error, but we need to change it to our custom page.

So, follow the following steps:




1- Create a new page under your portal, such us: http://www.____.com/pages/FileNotFound.aspx

2- Go to the following path in your server:
\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\1025
For arabic,
OR
\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\1033
For english.


3- Copy and past the sps404.html file

4- Rename the new file to any friendly name, such us: Mysps404.html

5- Open Mysps404.html file, and modify the following line:
STSNavigate("/_layouts/spsredirect.aspx?oldUrl=" + requestedUrl);

to:
STSNavigate(http://www.____.com/pages/FileNotFound.aspx);


6- Create a console application, to modify the default 404 error page to new page,
write the following lines:



System.Uri webApplicationUri = new Uri("http://www.____.com");

SPWebApplication webApplication = SPWebApplication.Lookup(webApplicationUri);

webApplication.FileNotFoundPage = "Mysps404.html";

webApplication.Update();





7- Run it in your server, and test the scenario.


Read more...