Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
0 comments Thursday, June 28, 2007

HI,

This is a code that i found online which is used to add a row to a datagrid data it is really nice.

private void dgView_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item e.Item.ItemType==ListItemType.AlternatingItem)
{
DataGrid dg = (DataGrid)sender;
TableCell tc = new TableCell();
tc.Controls.Add(new LiteralControl(e.Item.Cells[9].Text));
tc.ColumnSpan = 10;
DataGridItem di = new DataGridItem(e.Item.ItemIndex,0,ListItemType.Item);
di.Cells.Add(tc);
Table t = (Table)dg.Controls[0];
t.Rows.Add(di);
}
}

Pankaj

0 comments Wednesday, April 25, 2007

Hi,

Find out about the general-purpose query facilities added to the .NET Framework that apply to all sources of information, not just relational or XML data. This facility is called .NET Language Integrated Query (LINQ).

example

using System;
using System.Query;
using System.Collections.Generic;
class app {
static void Main() {
string[] names = { "Burke", "Connor", "Frank",
"Everett", "Albert", "George",
"Harris", "David" };
IEnumerable expr = from s in names
where s.Length == 5
orderby s
select s.ToUpper();
foreach (string item in expr)
Console.WriteLine(item);
}
}

0 comments Wednesday, February 28, 2007

Hi All,
Some time we need to generate random sequence of alphanumeric /nemeric/character for password/key etc. Below is static function which takes integer as parameter for the length of the output.


private static string RandomNumberGenerator(int length)
{
RandomNumberGenerator rng = RandomNumberGenerator.Create();
char[] chars = new char[length];
string validChars = "abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ1234567890"; //based on your requirment you can take only alphabets or number
for (int i=0; i< length; i++)
{
byte[] bytes = new byte[1];
rng.GetBytes(bytes);
Random rnd = new Random(bytes[0]);
chars[i] = validChars[rnd.Next(0,61)];
}
return (new string(chars));
}

Regards
Pankaj

1 comments Sunday, February 25, 2007

Hi All,

This code sample shows how to convert a string to a byte array and and byte array to string.

//function to convert string to byte array
public static byte[] ConvertStringToByteArray(string stringToConvert)
{
return (new UnicodeEncoding()).GetBytes(stringToConvert);
}

//function to convert byte array to string
public static string ConvertByteArrayToString(byte[] byteArray)
{
return (new UnicodeEncoding()).GetString(byteArray);
}


Regards
Pankaj

2 comments

Hi All,

This function is used to convert file into byte array. some time we require to convert file into byte to save into database or send to other system using remoting.

private byte [] StreamFile(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);

// Create a byte array of file stream length
byte[] ImageData = new byte[fs.Length];

//Read block of bytes from stream into the byte array
fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));

//Close the File Stream
fs.Close();

return ImageData; //return the byte data
}

Regards
Pankaj

0 comments Tuesday, January 23, 2007

Hi All,
The below function will authenticate user using Active Directory.


public bool IsAuthenticated(string domain, string username, string pwd)

{

string _path;

string _filterAttribute;

string servername = "ServerName"; // Give the server Name

string domainAndUsername = domain + "\\" + username;

DirectoryEntry entry = new DirectoryEntry("LDAP://" + servername, domainAndUsername, pwd);

try

{

object obj = entry.NativeObject;

DirectorySearcher search = new DirectorySearcher(entry);

search.Filter = "(SAMAccountName=" + username + ")";

search.PropertiesToLoad.Add("cn");

SearchResult result = search.FindOne();

if (result == null)

{

return false;

}

_path = result.Path;

_filterAttribute = ((string)(result.Properties["cn"][0]));

}

catch (Exception ex)

{

return false;

}

return true;

}



Hope this will help all

Regards
Pankaj

0 comments

Hi All

To quickly add assemblies to the GAC - a registry key can be created
which allows you to right click the DLL and 'GAC it' in one click.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\dllfile\shell\gacutil\command]
@="c:\\windows\\Microsoft.NET\\Framework\\v1.1.4322\\gacutil.exe /i
\"%1\""

Reference link
http://markharrison.co.uk/blog/2005/12/easier-way-to-add-dll-to-gac.htm

Thanks & Regards
Pankaj

0 comments


Namespace


C# namespace allow us to organize our class.unlike a file and component namespace provide us a flexibility to group our class and other types
For eg
Namespace MyCustomNameSpace
{
using System;
public int show()
{
return 0;
}
}
Using keyword in C#
C# allows us to abbreviate a class full name for doing this we have to list the class namespace at the top of the file with the “Using” key word
Eg
Using MyNameSpace.CsharpExample
Class MyClass
{
public static int Main()
{
SomeOtherNameSpace.SomeOtherClass loMyObj = new SomeOtherNameSpace.SomeOtherClass loMyObj();
Return 0;
}
}
Namespace Aliases
The another use of the namespace in c# is creating aliases.this is use ful when we have a long namespace for eg
Using myAliases = MyNameSpace.CsharpExample;

Regards
Pankaj