HP’s Blog

System.Transactions.TransactionManagerCommunicationException: Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool. —> System.Runtime.InteropServices.COMException (0×8004D024): The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0×8004D024)

You can resolve this by following the steps below

Control Panel – Administrative Tools – Component Services – My Computer properties – MSDTC tab – Security Configuration tab – Network DTC Access (checked) / Allow Remote Clients (checked) / Allow Inbound (checked) / Allow Outbound (checked) / Enable TIP Transactions (checked)

REBOOT computer

When you deal with the javascript and when you are using any form elements of the page in the script then you better make sure that your whole page gets load first. The form elements that you are using into the script will only get used until the page loads completely.

I have faced one situation where I was using popup window and was transferring the data of parent window to the opened popup. I have created one function into the popup window which will set some values into the text elements of the popup. I am calling this function from the parent window by passing some arguments to that function.

Now what happen is, when I am calling this function by putting some ‘alert()’ messages into the caller function and called function then the script is working fine for me. And when I am removing the ‘alert()’ message box from the script, the script stop working.

When I have analysis the script and debug it, I found that when I am calling a popup function from my parent window, the function did not set values into the text elements of the popup. This is because the popup window is not loaded completely. When I am putting an ‘alert()’ message box, the popup window gets sufficient time to load while I am busy clicking on the OK button for the alert message.

Thats it… I found that I was writing the script for the form elements which are not yet loaded into the popup and thats why I am getting this issue. I have resolved this issue by calling the javascript function of the popup only after it gets loaded completely.

If you found any similar issue then you can discuss here, I will try to solve it out.

Happy Programing!

javascript: Word Counter

Posted by: hspinfo on: December 19, 2008

Few months back I have posted one article on “Javascript: Maximum character validation and character counter for a textbox”, now here I have given one example on how to count words out of any textarea or textbox.

The counter of word will get incremented as you type in each word (one spell) into the textbox/textarea. This script will also work when anyone copy/paste the text into the textbox (copy/paste should not through mouse). Check the following script:

<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" TextMode="multiLine" MaxLength="100"
runat="server" onkeyup="javascript:WordCounter(this.id,'Label1');"
onblur="javascript:WordCounter(this.id,'Label1');"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="100 chars
remaining"></asp:Label>
</div>
<script language="javascript" type="text/javascript">
function WordCounter(textId,lblID)
{
var this_field = document.getElementById(textId);
var label = document.getElementById(lblID);
var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;
if (fullStr.length <2)
{
word_count = 0;
}
if (word_count == 1)
{
wordOrWords = " word";
}
else
{
wordOrWords = " words";
}
if (char_count == 1)
{
charOrChars = " character";
}
else
{
charOrChars = " characters";
}
label.innerHTML = word_count + wordOrWords;
}
</script>
</form>
</body>

Happy Coding!!

Merge two or more existing database tables/columns into one

Posted by: hspinfo on: December 1, 2008

I have two tables for e.g. tblStudents and tblSchools. Both are having different columns. For a reason I need to merge the two tables into one temporary table.

For example “tblStudents” contains the following design.

StudentID FirstName LastName Address DOB

Where as “tblSchools” contains the following design.

SchoolID Name Address IsSecodarySchool

Now, you would like to merge this two table columns into one temporary table then you can use the following sql script.

SELECT * INTO #tbl_Temp
FROM tblStudent, tblSchool where 1>2

Here, I have mentioned the “where” clause because I wanted to copy only the columns and not the data. You can ignore this clause if you want to copy the entire data too.

You can add as many tables you want to merge by separating with comma (,) between the table names.

You can add specific columns from specific tables by using table alias and column name combination.
For example
SELECT tblStudent.FirstName,tblStudent.LastName,tblSchool.Name INTO #tbl_Temp
FROM tblStudent, tblSchool where 1>2

Happy Programming!

So….. I have used the .First() method in many of my LINQ queries to make sure that my query only returns one record.  I thought this would be a great way to insure isolation of one record.  Very similar to a TOP 1 statement in SQL.  However, there is an issue with this concept.  If your TOP 1 SQL statement doesn’t find any records it returns nothing.  I guess I sort of hoped that the .First() method would do something similar – say return NULL or something like that.  It doesn’t!  Instead it throws an error stating “InvalidOperationException : sequence contains no elements”.  Not what I had hoped for.

No worries though!  There apparently was a reason for this method to find 1 or flip out as the LINQ team also provides the FirstOrDefault() method which will return NULL if no records were found.  Their documentation on the 101 LINQ Samples page doesn’t state anything about throwing an error.  It doesn’t really state anything at all about what would happen if the First() item wasn’t found.  It does state however that the FirstOrDefault() method will return the default value for the requested type – IE NULL!

You can face above errors on using .First(), .Last(), or .Single() method to make sure the query returns only single record. You can use .FirstOrDefault(), .LastOrDefault(), or .SingleOrDefault() instead.

Happy Programming!!

To retrieve the values from a DataKeyNames collection, you can use the following things. You can use the following code to get the DataKeys.

foreach (GridViewRow gvr in gvMyGridView.Rows)
{
string PrimaryKey = gvMyGridView.DataKeys[gvr.RowIndex].Values[0].ToString();
}

You can use this code while doing an iteration with “foreach” or for any GridView event like OnRowDataBound.

Here, You can input multiple values for DataKeyNames by separating with comma (’,'). For example, DataKeyNames="ProductID,ItemID,OrderID"

You can now access each of DataKeys by providing its index like below:

string ProductID = gvMyGridView.DataKeys[gvr.RowIndex].Values[0].ToString();
string ItemID = gvMyGridView.DataKeys[gvr.RowIndex].Values[1].ToString();
string OrderID = gvMyGridView.DataKeys[gvr.RowIndex].Values[2].ToString();

You can also use Key Name instead of its index to get the values from DataKeyNames collection like below:

string ProductID = gvMyGridView.DataKeys[gvr.RowIndex].Values["ProductID"].ToString();
string ItemID = gvMyGridView.DataKeys[gvr.RowIndex].Values["ItemID"].ToString();
string OrderID = gvMyGridView.DataKeys[gvr.RowIndex].Values["OrderID"].ToString();

You can also check a similar kind of article here

Happy programing!!

Today I came to know how to search for a specific keyword or any object like tables or columns of any tables used in a procedure. This is really useful when you have a bunch of procedures and you need to findout the procedure that have some specific tables, columns or even any keyword you wanted to search.

Here is the SQL Query that will be useful to get appropriate result set of stored procedures which contains your provided search keywords.

SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%your keyword%'

ViewState Compression and AJAX, ASP.NET

Posted by: hspinfo on: September 17, 2008

I have found many articles and blog posts out there that talk about ViewState and how to handle it when it becomes too large. A large ViewState slows down your user’s browsing experience due to larger page sizes. There are many ways to address this issue: decreasing your use of ViewState, storing the ViewState in Session or elsewhere, HTTP compression, and the topic of my post, compressing the ViewState hidden field value.

At my last project we had been using ViewState compression, and it had worked just fine for them for a couple of years. The client was using the same solution that I’m sure 90% of the community was, where you override the two Page methods:

LoadPageStateFromPersistenceMedium
SavePageStateToPersistenceMedium

and store the compressed ViewState in a hidden form field. Here’s an example from one such article:

public partial class MyPage : System.Web.UI.Page
{
protected override object LoadPageStateFromPersistenceMedium()
{
string viewState = Request.Form["__VSTATE"];
byte[] bytes = Convert.FromBase64String(viewState);
bytes = Compressor.Decompress(bytes);
LosFormatter formatter = new LosFormatter();
return formatter.Deserialize(Convert.ToBase64String(bytes));
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, viewState);
string viewStateString = writer.ToString();
byte[] bytes = Convert.FromBase64String(viewStateString);
bytes = Compressor.Compress(bytes);
ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
}
}

Note that Compressor is a custom class that you would write to do the compression/decompression. See the article above for sample code using Microsoft’s built-in compression libraries.

On our new endeavor of using third-party controls that used AJAX we noticed a problem. It seems these controls do not like the ViewState being ‘moved’ to a different form field (here it was moved to __VSTATE instead of the standard __VIEWSTATE). There must be some hard-coded references to this field name, and they cannot properly track their own state. The controls loaded fine but most of the AJAX calls would not run all of the correct events on the server.

After some searching I came across a solution that still compresses the ViewState but does so in an AJAX-friendly manner by leaving it in the __VIEWSTATE field. Since it was a hard-to-find forum post I thought it warranted a little more visibility and discussion.

In ASP.NET 2.0, Microsoft added a class called PageStatePersister, which is also exposed a property on the Page class itself. The page events mentioned above are still there and used, but now they hand off the dirty work to the PageStatePersister. The PageStatePersister is responsible for loading and saving ViewState.

There are a couple of ways you can implement ViewState compression using these events and/or PageStatePersister. One way is that you could you write your own persister and override the PageStatePersister property on the Page to use your class instead.

In my case I decided to override the two Page events above and now work with the PageStatePersister within the events. First, let’s look at the LoadPageStateFromPersistenceMedium event:


protected override object LoadPageStateFromPersistenceMedium()
{
String alteredViewState;
byte[] bytes;
Object rawViewState;
LosFormatter fomatter = new LosFormatter();
this.PageStatePersister.Load();
alteredViewState = this.PageStatePersister.ViewState.ToString();
bytes = Convert.FromBase64String(alteredViewState);
bytes = Compressor.Decompress(bytes);
rawViewState = fomatter.Deserialize(Convert.ToBase64String(bytes));
return new Pair(this.PageStatePersister.ControlState, rawViewState);
}

You see we first tell the PageStatePersister to Load itself with the ViewState. We then decode it as a base64 string and decompress. It is then deserialized and placed into a Pair object with current control state. The Pair is what is returned.

And now for the SavePageStateToPersistenceMedium event:

protected override void SavePageStateToPersistenceMedium(object viewStateIn)
{
LosFormatter fomatter = new LosFormatter();
StringWriter writer = new StringWriter();
Pair rawPair;
Object rawViewState;
String rawViewStateStr;
String alteredViewState;
byte[] bytes;
if (viewStateIn is Pair)
{
rawPair = ((Pair)viewStateIn);
this.PageStatePersister.ControlState = rawPair.First;
rawViewState = rawPair.Second;
}
else
{
rawViewState = viewStateIn;
}
fomatter.Serialize(writer, rawViewState);
rawViewStateStr = writer.ToString();
bytes = Convert.FromBase64String(rawViewStateStr);
bytes = Compressor.Compress(bytes);
alteredViewState = Convert.ToBase64String(bytes);
this.PageStatePersister.ViewState = alteredViewState;
this.PageStatePersister.Save();
}

Here we take the viewStateIn, which is the normal ViewState and instead compress it before saving to the PageStatePersister.

Now our ViewState is compressed and stored in the standard __VIEWSTATE field and our AJAX-enabled controls should be happy!

Today, I was searching for a solution to enable/disable DIV element using javascript. I have tried so many code, but all are working in IE only and not supported by FF (firefox). At last I found some solution which is helpful. I thought this might also useful to you.

Copy/Paste following javascript code snippet.
<script type="text/javascript">
function toggleAlert() {
toggleDisabled(document.getElementById("content"));
}
function toggleDisabled(el) {
try {
el.disabled = el.disabled ? false : true;
}
catch(E){
}
if (el.childNodes && el.childNodes.length > 0) {
for (var x = 0; x < el.childNodes.length; x++) {
toggleDisabled(el.childNodes[x]);
}
}
}
</script>

Copy/Paste following HTML code in BODY tag:

<div id="content">
<table>
<tr>
<td><input type="text" name="foo" /></td>
</tr>
<tr>
<td>
<select name="bar">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
</td>
</tr>
</table>
</div>
<input type="checkbox" value="toggleAlert()" onclick="toggleAlert()" />

Here, I have used one checkbox, by clicking which you can enable/disable child controls of a DIV. However, you can use any element other than checkbox (e.g button, radio button, etc…)

Javascript: innerText not working in FireFox and Safari

Posted by: hspinfo on: September 1, 2008

Today, I came to know that innerText property of any web control is not supported by FireFox and Safari browser. Here, I found the alternate solution for innerText property.

Use the following code in javascript to acquire the same functionality as innerText.
var myText = document.getElementById(’divText’).textContent; // same as innertext.

Blog Stats

  • 133,561 Readers