LINQ


You need to AJAX enable a asp.net application to use the progress bar control.

Place this asp.net script on the master page or the individual page as required and the progress bar will be displayed during page is loading.

<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<asp:UpdateProgress ID="Updateprogress1" runat="server">

<ProgressTemplate>

<iframe frameborder="0" src="about:blank" style="border: 0px; position: absolute;

z-index: 9; left: 0px; top: 0px; width: expression(this.offsetParent.scrollWidth);

height: expression(this.offsetParent.scrollHeight); filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0, FinishOpacity=0, Style=0, StartX=0, FinishX=100, StartY=0, FinishY=100);">

</iframe>

<div style="position: absolute; z-index: 10; left: expression((this.offsetParent.clientWidth/2)-(this.clientWidth/2)+this.offsetParent.scrollLeft);

top: expression((this.offsetParent.clientHeight/2)-(this.clientHeight/2)+this.offsetParent.scrollTop);">

<table border="0" cellpadding="0" style="border-collapse: collapse" width="100" id="tblloder"

runat="server">

<tr>

<td height="8">

</td>

</tr>

<tr>

<td align="center">

<img alt="" border="0" src="images/progress-bar.gif"></td>

</tr>

<tr>

<td height="6">

</td>

</tr>

</table>

</div>

</ProgressTemplate>

</asp:UpdateProgress>


Many times we need to search the database for e.g. to search the database for Customer names.

So the general Query to search a name ’Chapell’ will be

SELECT CustomerName FROM t_CustomerMaster WHERE CustomerName =’Chapell’

Now imagine that the user searching the database enters ‘Chapple’ in the textbox and hits enter. He will hit enter and will not get any results. So usually we use the LIKE operator in the WHERE clause and search for a substring.

SELECT CustomerName FROM t_CustomerMaster WHERE CustomerName LIKE ’%Chap%’

So this one will work and return all the names that contain the substring ‘Chap’ . So the result set can contain anything like Chapel, Chapele, Richap etc.

But will this search query still work if I search for ‘Chipell’ instead of ‘Chapell’ ?

The answer to his question is the SOUNDEX() function in Transact SQL.

The SOUNDEX() returns a four digit code for a varchar value passed to it. The same code is returned for all those names whose pronounciation is similar.

For example SOUNDEX(‘Smith’), SOUNDEX(‘Smythe’),SOUNDEX(‘Smithe’) will return the same code

More information on SOUNDEX() can be found here http://msdn.microsoft.com/en-us/library/aa259235(SQL.80).aspx

As a result the search query can be tuned to return the result set for ‘Chapell’ even if you search for ‘Chipell’. So re-constructing our search query by combining the power of LIKE and SOUNDEX() will return the same results even if the spelling Of ‘Chapell’ is changed

SELECT CustomerName

FROM t_CustomerMaster

WHERE (CustomerName LIKE ’%Chapell%’ OR (SOUNDEX(CustomerName) LIKE SOUNDEX(’%Chapell%’)))

The above query will return the same result set even if you use Chap,Chapel,Chapelle, Chipell instead of Chapell

Step 1:Populate the ViewState with the datasource of the gridview in the !Page.IsPostback event .

Step 2: In Gridview set AllowSorting="true"

Step: 3: Add the sortexpression="ColumnName"

Step 4: Handle the Onsorting event of the grid on the server side as follows:

protected void grdRoomTypeMaster_OnSorting(object sender, GridViewSortEventArgs e)
{
DataTable dtGridData = ViewState["grdDataSource"] as DataTable;
DataView dvGridDataView = dtGridData.DefaultView;
string strSortOrder = "";
if (ViewState["SortOrder"]==null)
{
ViewState["SortOrder"] = "asc";
}
if (ViewState["SortOrder"].ToString() == "asc")
{
ViewState["SortOrder"] = "desc";
strSortOrder = "desc";
}
else if (ViewState["SortOrder"].ToString() == "desc")
{
ViewState["SortOrder"] = "asc";
strSortOrder = "asc";
}
dvGridDataView.Sort = e.SortExpression + " " + strSortOrder;
dtGridData = dvGridDataView.ToTable();

grdRoomTypeMaster.DataSource = dtGridData;
grdRoomTypeMaster.DataBind();
}

Thanks Justin for his post and VB.NET code on

http://geekswithblogs.net/phoenix/archive/2009/07/02/gridview-sorting-with-datatables.aspx

You can get the values from a named column in the DataTable using a single line of code in LINQ.

Steps:

1. Convert the DataTable object to Enumerable()

2. Apply the select clause with the column name as field name .

3. Cast it to a string array.

string[] strSummCities = dtTemp1.AsEnumerable().Select(s => s.Field<string>("City")).ToArray<string>();

try

{

ConnectionOptions connection = new ConnectionOptions();

connection.Username = userNameBox.Text;

connection.Password = passwordBox.Text;

connection.Authority = "ntlmdomain:Finance123";

ManagementScope scope = new ManagementScope(

"\\dell417\root\CIMV2", connection);

scope.Connect();

ObjectQuery query= new ObjectQuery(

"SELECT * FROM Win32_DiskDrive");

ManagementObjectSearcher searcher =

new ManagementObjectSearcher(scope, query);

foreach (ManagementObject queryObj in searcher.Get())

{

Console.WriteLine("");

Console.WriteLine("Win32_DiskDrive instance");

Console.WriteLine("");

Console.WriteLine("Availability: {0}", queryObj["Availability"]);

Console.WriteLine("BytesPerSector: {0}", queryObj["BytesPerSector"]);

if(queryObj["Capabilities"] == null)

Console.WriteLine("Capabilities: {0}", queryObj["Capabilities"]);

else

{

UInt16[] arrCapabilities = (UInt16[])(queryObj["Capabilities"]);

foreach (UInt16 arrValue in arrCapabilities)

{

Console.WriteLine("Capabilities: {0}", arrValue);

}

}

if(queryObj["CapabilityDescriptions"] == null)

Console.WriteLine("CapabilityDescriptions: {0}", queryObj["CapabilityDescriptions"]);

else

{

String[] arrCapabilityDescriptions = (String[])(queryObj["CapabilityDescriptions"]);

foreach (String arrValue in arrCapabilityDescriptions)

{

Console.WriteLine("CapabilityDescriptions: {0}", arrValue);

}

}

Console.WriteLine("Caption: {0}", queryObj["Caption"]);

Console.WriteLine("CompressionMethod: {0}", queryObj["CompressionMethod"]);

Console.WriteLine("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]);

Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);

Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);

Console.WriteLine("DefaultBlockSize: {0}", queryObj["DefaultBlockSize"]);

Console.WriteLine("Description: {0}", queryObj["Description"]);

Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);

Console.WriteLine("ErrorCleared: {0}", queryObj["ErrorCleared"]);

Console.WriteLine("ErrorDescription: {0}", queryObj["ErrorDescription"]);

Console.WriteLine("ErrorMethodology: {0}", queryObj["ErrorMethodology"]);

Console.WriteLine("Index: {0}", queryObj["Index"]);

Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);

Console.WriteLine("InterfaceType: {0}", queryObj["InterfaceType"]);

Console.WriteLine("LastErrorCode: {0}", queryObj["LastErrorCode"]);

Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);

Console.WriteLine("MaxBlockSize: {0}", queryObj["MaxBlockSize"]);

Console.WriteLine("MaxMediaSize: {0}", queryObj["MaxMediaSize"]);

Console.WriteLine("MediaLoaded: {0}", queryObj["MediaLoaded"]);

Console.WriteLine("MediaType: {0}", queryObj["MediaType"]);

Console.WriteLine("MinBlockSize: {0}", queryObj["MinBlockSize"]);

Console.WriteLine("Model: {0}", queryObj["Model"]);

Console.WriteLine("Name: {0}", queryObj["Name"]);

Console.WriteLine("NeedsCleaning: {0}", queryObj["NeedsCleaning"]);

Console.WriteLine("NumberOfMediaSupported: {0}", queryObj["NumberOfMediaSupported"]);

Console.WriteLine("Partitions: {0}", queryObj["Partitions"]);

Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);

if(queryObj["PowerManagementCapabilities"] == null)

Console.WriteLine("PowerManagementCapabilities: {0}", queryObj["PowerManagementCapabilities"]);

else

{

UInt16[] arrPowerManagementCapabilities = (UInt16[])(queryObj["PowerManagementCapabilities"]);

foreach (UInt16 arrValue in arrPowerManagementCapabilities)

{

Console.WriteLine("PowerManagementCapabilities: {0}", arrValue);

}

}

Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]);

Console.WriteLine("SCSIBus: {0}", queryObj["SCSIBus"]);

Console.WriteLine("SCSILogicalUnit: {0}", queryObj["SCSILogicalUnit"]);

Console.WriteLine("SCSIPort: {0}", queryObj["SCSIPort"]);

Console.WriteLine("SCSITargetId: {0}", queryObj["SCSITargetId"]);

Console.WriteLine("SectorsPerTrack: {0}", queryObj["SectorsPerTrack"]);

Console.WriteLine("Signature: {0}", queryObj["Signature"]);

Console.WriteLine("Size: {0}", queryObj["Size"]);

Console.WriteLine("Status: {0}", queryObj["Status"]);

Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);

Console.WriteLine("SystemCreationClassName: {0}", queryObj["SystemCreationClassName"]);

Console.WriteLine("SystemName: {0}", queryObj["SystemName"]);

Console.WriteLine("TotalCylinders: {0}", queryObj["TotalCylinders"]);

Console.WriteLine("TotalHeads: {0}", queryObj["TotalHeads"]);

Console.WriteLine("TotalSectors: {0}", queryObj["TotalSectors"]);

Console.WriteLine("TotalTracks: {0}", queryObj["TotalTracks"]);

Console.WriteLine("TracksPerCylinder: {0}", queryObj["TracksPerCylinder"]);

}

Close();

}

catch(ManagementException err)

{

MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);

}

catch(System.UnauthorizedAccessException unauthorizedErr)

{

MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);

}

try

{

ConnectionOptions connection = new ConnectionOptions();

connection.Username = userNameBox.Text;

connection.Password = passwordBox.Text;

connection.Authority = "ntlmdomain:Finance123";

ManagementScope scope = new ManagementScope(

"\\dell417\root\CIMV2", connection);

scope.Connect();

ObjectQuery query= new ObjectQuery(

"SELECT * FROM Win32_DirectorySpecification");

ManagementObjectSearcher searcher =

new ManagementObjectSearcher(scope, query);

foreach (ManagementObject queryObj in searcher.Get())

{

Console.WriteLine("");

Console.WriteLine("Win32_DirectorySpecification instance");

Console.WriteLine("");

Console.WriteLine("Caption: {0}", queryObj["Caption"]);

Console.WriteLine("CheckID: {0}", queryObj["CheckID"]);

Console.WriteLine("CheckMode: {0}", queryObj["CheckMode"]);

Console.WriteLine("DefaultDir: {0}", queryObj["DefaultDir"]);

Console.WriteLine("Description: {0}", queryObj["Description"]);

Console.WriteLine("Directory: {0}", queryObj["Directory"]);

Console.WriteLine("DirectoryPath: {0}", queryObj["DirectoryPath"]);

Console.WriteLine("DirectoryType: {0}", queryObj["DirectoryType"]);

Console.WriteLine("Name: {0}", queryObj["Name"]);

Console.WriteLine("SoftwareElementID: {0}", queryObj["SoftwareElementID"]);

Console.WriteLine("SoftwareElementState: {0}", queryObj["SoftwareElementState"]);

Console.WriteLine("TargetOperatingSystem: {0}", queryObj["TargetOperatingSystem"]);

Console.WriteLine("Version: {0}", queryObj["Version"]);

}

Close();

}

catch(ManagementException err)

{

MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);

}

catch(System.UnauthorizedAccessException unauthorizedErr)

{

MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);

}

try

{

ConnectionOptions connection = new ConnectionOptions();

connection.Username = userNameBox.Text;

connection.Password = passwordBox.Text;

connection.Authority = "ntlmdomain:Finance123";

ManagementScope scope = new ManagementScope(

"\\dell417\root\CIMV2", connection);

scope.Connect();

ObjectQuery query= new ObjectQuery(

"SELECT * FROM Win32_Directory");

ManagementObjectSearcher searcher =

new ManagementObjectSearcher(scope, query);

foreach (ManagementObject queryObj in searcher.Get())

{

Console.WriteLine("");

Console.WriteLine("Win32_Directory instance");

Console.WriteLine("");

Console.WriteLine("AccessMask: {0}", queryObj["AccessMask"]);

Console.WriteLine("Archive: {0}", queryObj["Archive"]);

Console.WriteLine("Caption: {0}", queryObj["Caption"]);

Console.WriteLine("Compressed: {0}", queryObj["Compressed"]);

Console.WriteLine("CompressionMethod: {0}", queryObj["CompressionMethod"]);

Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);

Console.WriteLine("CreationDate: {0}", queryObj["CreationDate"]);

Console.WriteLine("CSCreationClassName: {0}", queryObj["CSCreationClassName"]);

Console.WriteLine("CSName: {0}", queryObj["CSName"]);

Console.WriteLine("Description: {0}", queryObj["Description"]);

Console.WriteLine("Drive: {0}", queryObj["Drive"]);

Console.WriteLine("EightDotThreeFileName: {0}", queryObj["EightDotThreeFileName"]);

Console.WriteLine("Encrypted: {0}", queryObj["Encrypted"]);

Console.WriteLine("EncryptionMethod: {0}", queryObj["EncryptionMethod"]);

Console.WriteLine("Extension: {0}", queryObj["Extension"]);

Console.WriteLine("FileName: {0}", queryObj["FileName"]);

Console.WriteLine("FileSize: {0}", queryObj["FileSize"]);

Console.WriteLine("FileType: {0}", queryObj["FileType"]);

Console.WriteLine("FSCreationClassName: {0}", queryObj["FSCreationClassName"]);

Console.WriteLine("FSName: {0}", queryObj["FSName"]);

Console.WriteLine("Hidden: {0}", queryObj["Hidden"]);

Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);

Console.WriteLine("InUseCount: {0}", queryObj["InUseCount"]);

Console.WriteLine("LastAccessed: {0}", queryObj["LastAccessed"]);

Console.WriteLine("LastModified: {0}", queryObj["LastModified"]);

Console.WriteLine("Name: {0}", queryObj["Name"]);

Console.WriteLine("Path: {0}", queryObj["Path"]);

Console.WriteLine("Readable: {0}", queryObj["Readable"]);

Console.WriteLine("Status: {0}", queryObj["Status"]);

Console.WriteLine("System: {0}", queryObj["System"]);

Console.WriteLine("Writeable: {0}", queryObj["Writeable"]);

}

Close();

}

catch(ManagementException err)

{

MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);

}

catch(System.UnauthorizedAccessException unauthorizedErr)

{

MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);

}

try

{

ConnectionOptions connection = new ConnectionOptions();

connection.Username = userNameBox.Text;

connection.Password = passwordBox.Text;

connection.Authority = "ntlmdomain:Finance123";

ManagementScope scope = new ManagementScope(

"\\dell417\root\CIMV2", connection);

scope.Connect();

ObjectQuery query= new ObjectQuery(

"SELECT * FROM Win32_DesktopMonitor");

ManagementObjectSearcher searcher =

new ManagementObjectSearcher(scope, query);

foreach (ManagementObject queryObj in searcher.Get())

{

Console.WriteLine("");

Console.WriteLine("Win32_DesktopMonitor instance");

Console.WriteLine("");

Console.WriteLine("Availability: {0}", queryObj["Availability"]);

Console.WriteLine("Bandwidth: {0}", queryObj["Bandwidth"]);

Console.WriteLine("Caption: {0}", queryObj["Caption"]);

Console.WriteLine("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]);

Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);

Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);

Console.WriteLine("Description: {0}", queryObj["Description"]);

Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);

Console.WriteLine("DisplayType: {0}", queryObj["DisplayType"]);

Console.WriteLine("ErrorCleared: {0}", queryObj["ErrorCleared"]);

Console.WriteLine("ErrorDescription: {0}", queryObj["ErrorDescription"]);

Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);

Console.WriteLine("IsLocked: {0}", queryObj["IsLocked"]);

Console.WriteLine("LastErrorCode: {0}", queryObj["LastErrorCode"]);

Console.WriteLine("MonitorManufacturer: {0}", queryObj["MonitorManufacturer"]);

Console.WriteLine("MonitorType: {0}", queryObj["MonitorType"]);

Console.WriteLine("Name: {0}", queryObj["Name"]);

Console.WriteLine("PixelsPerXLogicalInch: {0}", queryObj["PixelsPerXLogicalInch"]);

Console.WriteLine("PixelsPerYLogicalInch: {0}", queryObj["PixelsPerYLogicalInch"]);

Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);

if(queryObj["PowerManagementCapabilities"] == null)

Console.WriteLine("PowerManagementCapabilities: {0}", queryObj["PowerManagementCapabilities"]);

else

{

UInt16[] arrPowerManagementCapabilities = (UInt16[])(queryObj["PowerManagementCapabilities"]);

foreach (UInt16 arrValue in arrPowerManagementCapabilities)

{

Console.WriteLine("PowerManagementCapabilities: {0}", arrValue);

}

}

Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]);

Console.WriteLine("ScreenHeight: {0}", queryObj["ScreenHeight"]);

Console.WriteLine("ScreenWidth: {0}", queryObj["ScreenWidth"]);

Console.WriteLine("Status: {0}", queryObj["Status"]);

Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);

Console.WriteLine("SystemCreationClassName: {0}", queryObj["SystemCreationClassName"]);

Console.WriteLine("SystemName: {0}", queryObj["SystemName"]);

}

Close();

}

catch(ManagementException err)

{

MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);

}

catch(System.UnauthorizedAccessException unauthorizedErr)

{

MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);

}

try

{

ConnectionOptions connection = new ConnectionOptions();

connection.Username = userNameBox.Text;

connection.Password = passwordBox.Text;

connection.Authority = "ntlmdomain:Finance123";

ManagementScope scope = new ManagementScope(

"\\dell417\root\CIMV2", connection);

scope.Connect();

ObjectQuery query= new ObjectQuery(

"SELECT * FROM Win32_Desktop");

ManagementObjectSearcher searcher =

new ManagementObjectSearcher(scope, query);

foreach (ManagementObject queryObj in searcher.Get())

{

Console.WriteLine("");

Console.WriteLine("Win32_Desktop instance");

Console.WriteLine("");

Console.WriteLine("BorderWidth: {0}", queryObj["BorderWidth"]);

Console.WriteLine("Caption: {0}", queryObj["Caption"]);

Console.WriteLine("CoolSwitch: {0}", queryObj["CoolSwitch"]);

Console.WriteLine("CursorBlinkRate: {0}", queryObj["CursorBlinkRate"]);

Console.WriteLine("Description: {0}", queryObj["Description"]);

Console.WriteLine("DragFullWindows: {0}", queryObj["DragFullWindows"]);

Console.WriteLine("GridGranularity: {0}", queryObj["GridGranularity"]);

Console.WriteLine("IconSpacing: {0}", queryObj["IconSpacing"]);

Console.WriteLine("IconTitleFaceName: {0}", queryObj["IconTitleFaceName"]);

Console.WriteLine("IconTitleSize: {0}", queryObj["IconTitleSize"]);

Console.WriteLine("IconTitleWrap: {0}", queryObj["IconTitleWrap"]);

Console.WriteLine("Name: {0}", queryObj["Name"]);

Console.WriteLine("Pattern: {0}", queryObj["Pattern"]);

Console.WriteLine("ScreenSaverActive: {0}", queryObj["ScreenSaverActive"]);

Console.WriteLine("ScreenSaverExecutable: {0}", queryObj["ScreenSaverExecutable"]);

Console.WriteLine("ScreenSaverSecure: {0}", queryObj["ScreenSaverSecure"]);

Console.WriteLine("ScreenSaverTimeout: {0}", queryObj["ScreenSaverTimeout"]);

Console.WriteLine("SettingID: {0}", queryObj["SettingID"]);

Console.WriteLine("Wallpaper: {0}", queryObj["Wallpaper"]);

Console.WriteLine("WallpaperStretched: {0}", queryObj["WallpaperStretched"]);

Console.WriteLine("WallpaperTiled: {0}", queryObj["WallpaperTiled"]);

}

Close();

}

catch(ManagementException err)

{

MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);

}

catch(System.UnauthorizedAccessException unauthorizedErr)

{

MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);

}

« Previous PageNext Page »