Using .NET Assembly (DLL) in ASP page

1. Create a Assembly file using following:
        File >> New >> Project >> Class Library >> Name = MyLibCSharp
 
2. Add a function HelloWorld. Code will look as follows:

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

namespace MyLibCsharp

{

[Guid(“FE1EB79E-3513-487e-ADFF-2B61C6CB4730”)]

[ComVisible(true)]

public class Class1

{

public String HelloWorld(String name)

{

return (“Hellow World; Welcome “ + name);

}

}

}

3. Sign the Assembly (To enable it to be used by multiple Applications):

Go to Project >> MyLibCSharp Properties >> Signing >> Sign the Assembly >> New >> Key.snk

4. Enable for COM Interop:

Go to Project >> MyLibCSharp Properties >> Build >> Output >> Check the “Register for COM interop.

5. Build the Assembly.

6. Add to GAC using “Visual Studio Command Prompt” the assembly is located in DEBUG/RELEASE folder.

gacutil -I MyLibCSharp.dll

7. Execute the following:

Regasm /tlb /codebase MyLibCSharp.dll

8. ASP code:

<%

Dim foo

set foo = Server.CreateObject(“MyLibCSharp.Class1”)

Response.Write (foo.HelloWorld(“test”))

%>

9. To unregister the assembly

Regasm /unregister MyLibCSharp.dll

10. And to remove the same from GAC (Global assembly cache)

Go to

C:\WINDOWS\assembly

find the assembly you want to remove

select it , right click it and select uninstall

Bye

 

Unable to start debugging. The Machine Debug Manager is disabled

Occasionally I used to receive this error

“Unable to start debugging. The Machine Debug Manager is disabled” when I try to run the solution from visual studio in debug mode.

The reason for this was Machine Debug Manager was in disabled state or not started.

So the solution for this is

 

1) Go to control panel.

2) Then Administrative Tools

3) Then services.

4) In services find the service named Machine debug manager.

 

Enable the service and start it if disabled or stopped.

 

Bye

Reflector -Nice tool for .NET developer

Reflector is a nice tool developed by Lutz Roeder.

It is somewhat similar to .Net ILDASM (Intermediate Language Dissembler).

The best thing about it is that we can even view the source code that also in different languages

C#, VB.NET, Delphi even the IL code itself.

 

This is the link

http://www.aisto.com/roeder/dotnet/

 

There are other good tools as well.

 

Do check it.

Nice website for C# developer

Hi,
Do check out this very very useful web-site for c# developer for understanding
multi threading and it has got some wonderful tools like

QueryExpression – which has an user interface similar to query analyzer using which we can query oracle, sql server and other databases. It is also very light weight.

And
LinqPad for practicing the Linq syntax.

http://www.albahari.com/index.html

Creating custom application exception C#

Hi,

There are times when we are creating a custom class and the class needs to have it’s own application specific exception which can be thrown so that calling program can be aware of the error condition.

Say we have a class named BillingUpdate which has a condition that the billing amount should never be less than 10,000.

Say it has a function which accepts billing amount in one of it’s methods as a parameter and we need to make the user of this function aware of the condition that it can’t take billing amount less than 10000.

In this case what we can do is

create a custom class BillingException which inherits SystemException class

// creating a custom class that inherits from SystemException.
class BillingException :SystemException
{
// overloading the constructor for passing the message associated with the exception
public BillingException(string message)
: base(message)
{
}
}

Now to use this exception class we can do the following

public void GetBillingAmount(decimal billingAmount)
{
if(billingAmount < 10000M)
{
throw new BillingException(“Billing amount can’t be less than 10000”);
}
}

And the calling code can do something like this

BillingUpdate billingUpdate = new BillingUpdate();
try
{
billingUpdate.GetBillingAmount(9000M);
}
catch(
BillingException ex)
{
MessageBox.Show(ex.Message);
}

Bye

Using Transaction In ADO.NET

Hi,

In most of the cases we need to execute a number of statements together.

Best example would be a bank transaction in which we are debiting certain amount from one of the user’s account and crediting the same in someone else’s account. So in this case the entire thing should run or nothing should run.

We have in our .NET framework various transaction class one for each .NET-managed provider i.e.

OracleTransaction, OleDbTransaction, SqlTransaction etc.

The most basic way we can use the transaction class is the following way

SqlTransaction myTransaction;
SqlCommand myCommand1 = new SqlCommand();
myCommand1.CommandText = “some command”;
SqlCommand myCommand2 = new SqlCommand();
myCommand2.CommandText = “some command”;
using(SqlConnection myConn = new SqlConnection(connectionString))
{
myConn.Open();

myTransaction = myConn.BeginTransaction();

myCommand1.Connection = myConn;
myCommand1.Transaction = myTransaction;

myCommand2.Connection = myConn;
myCommand2.Transaction = myTransaction;

try
{
myCommand1.ExecuteNonQuery();
myCommand2.ExecuteNonQuery();
myTransaction.Commit();

}
catch
{
myTransaction.Rollback();
}
finally
{
myConn.Close();
}
}

Bye

Using Forms Authentication in Asp.NET

Hi All,

To use form authentication in asp.net application

1- Create a new website

2- Change the name of the default.aspx to login.aspx

3- Add 2 labels one for username and password and two textboxes name txtUserName and txtPassword

and a button named btnLogin with label Login.

4 -Put the following in the web.config page

<authentication mode=”Forms”>
<forms loginUrl=”login.aspx”>
<credentials passwordFormat=”Clear”>
<user name=”Mickey” password=”Mouse”/>
</credentials>
</forms>
</authentication>
<authorization>
<deny users=”?”/>
</authorization>

5-Put this code in the btnLogin click’s event handler.

protected void btnLogin_Click(object sender, EventArgs e)
{
if(FormsAuthentication.Authenticate(txtUserName.Text,txtPassword.Text))
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);
}
else
{
lblInfo.Text = “you are not authorized to view the page”; // add one more label lblInfo }
}

6- That’s it your page should work now

We can add more users within the credential section of web.config

We can even encrypt the password we are saving in the web.config

We can even make use of cookie for the same.

Looking forward to include the code for the same

Bye

JavaScript and Microsoft Dynamics CRM

I was thinking that it would have been nice if some expert would have written some article or post about how to use JavaScript within CRM.

And today only i came to know about such an article , it is written by none other than MichaelHohne, the CRM guru, the creator of stunnware site( the most helpful site for Microsoft CRM Developer)

For different things we can do by making use of JavaScript in Microsoft Dynamics CRM,

Plzzzz check and bookmark this url

http://www.stunnware.com/crm2/topic.aspx?id=JS13

Bye

Operation failed due to a SQL integrity violation

Hi,

Today i was suppose to write a callout which would be sending email to owner’s manager for our opportunity entity.

I thought before trying out with an callout(which is really hard to debug) i should try it first in a windows application. Finally after some time i was able to get it working.

So than i decided to put the same code in the callout as well. But to my surprise i found that the same code was not working in the callout.

This was the error i was getting as

ex.detail.innerxml // for (SoapException ex)

<error>
<code>0x80040237</code>
<description>Operation failed due to a SQL integrity violation.</description>
<type>Platform</type>
</error>

After searching on the internet for the same i was able to find the cause for the error in my code

For my windows application this piece of code was sufficient

CrmService service=new CrmService();
service.Credentials=System.Net.CredentialCache.DefaultCredentials;
service.Url=”http://d-2927:5555/mscrmservices/2006/crmservice.asmx&#8221;;

But when it came to callout it had to be

CrmService service=new CrmService();
service.Credentials=System.Net.CredentialCache.DefaultCredentials;
service.Url=”http://d-2927:5555/mscrmservices/2006/crmservice.asmx&#8221;;
service.CallerIdValue = new CallerId();
service.CallerIdValue.CallerGuid =userContext.UserId;

This helped me to solve the error.

Check this link as well

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1768766&SiteID=1

Bye

Creating a simple Hello World ASP.NET AJAX web page.

Hi,

Here we will create a simple ASP.Net Ajax webpage having a button and textbox control in it.
On the click of button we will fill the textbox with “Hello world” string returned from the server without refreshing our webpage.

1) Create a new ASP.NET Ajax web site(Visual Studio 2005)or ASP.NET web site (version 3.5) if we are using Orcas(Visual studio 2008)

2) Add a ScriptManager control in the page( if it isn’t there)

3) Add button and textbox server side control.

<asp:TextBox ID=”TextBox1″ runat=”server” />

<asp:Button ID=”Button1″ runat=”server” Text=”Button” />

4) Add this event Handler for button click event

protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text=”Hello World”;
}

4) Now to make our page AJAX enabled the only thing we need to add is

<asp:UpdatePanel id=up1 runat=server>
<ContentTemplate>

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Button” />

</ContentTemplate>
</asp:UpdatePanel>

Just wrap our server side controls inside update panel control’s content template.

5) That’s it. Now run the application and click on the button the textbox should get filled with Hello World string without any refresh of the page.

Right now what we saw was a server-centric approach using updatePanel control.

We can do the same thing using client-centric approach using ASP.NET AJAX client library. Check this post

https://nishantrana.wordpress.com/2007/11/06/creating-and-calling-aspnet-ajax-web-service/

Bye.