Tuesday, March 31, 2009

System.Data.SqlClient.SqlException: Login failed for user 'MachineName\ASPNET'

        Yesterday I was trying to deploy my ASP.Net website in the IIS for my another application to refer.
        After successfully deployed in IIS when I ran the application I got an error saying Login failed for...
        Pretty much after struggling a lot time I came to know that I need to add ASPNET user in my Database user's list.
        But when I tried to add the user in the SQL management studio ( SQL Express ) it failed.
        Finally I got the solution from a website to add the ASPNET user account for SQL from a command prompt command. I thought sharing this will help.
        You need to execute the SQL to add ASPNET as a login to your DB and setup their DA permissions, etc. (use VS Command Prompt) So launch "osql -S . -E" then
        paste in this SQL and it should start working for you; don't forget to put GO as the last statement when executing in the command prompt.
        
        exec sp_grantlogin N'THOR\ASPNET'
        exec sp_defaultdb N'THOR\ASPNET', N'pubs'
        exec pubs
        exec sp_grantdbaccess N'THOR\ASPNET', N'ASPNET'
        exec sp_addrolemember N'db_owner', N'ASPNET'
        
        
        Replace THOR with your machine name and replace pubs with your database name. One note is that this SQL adds ASPNET as dbo, so the ASPNET user has total control over that DB. This is the fastest way to get it working, but I'd suggest doing some research on SqlServer DB security to know how to scale back the permissions for this user.

        
        Note: Make sure you had run the aspnet_regiis command in the Visual Studio Command Prompt
    

Wednesday, December 10, 2008

.Net Tips


You can add tasks and comments to your solution. We can view the comments from the View menu --> Task List (Ctrl + W, T) which helps us to keep track of the TODO changes we left behind.

//TODO Remove the code after testing
System.Threading.Thread.Sleep(5000);

The comments also gives the filename and linenumber for locating the code and remove it. It's a cool feature to have a follow ups.


Monday, December 8, 2008

Disable button after clicked once

We were fond of people(testing) who tries to click the button twice or thrice when It was performing some operation which may yield the wrong result or does unnecessary postpack to the page.
So we can achieve this by disabling the button in the OnClientClick event of the button using javascript. But the problem is after disabling the button, it doesn't invoke the server click event even though we return true in the client event.

function Disable(t) {
        if( t != null)
        {
            t.disabled = true;
            t.value = 'Processing...';
            return true;
        }
        return false;
    }

        asp:Button ID="Button1" runat="server" OnClientClick="return Disable(this)" onclick="Button1_Click" Text="Button" 

Therefore ASP.Net 2.0 has a property called "UseSubmitBehavior". 

Gets or sets a value indicating whether the Button control uses the client browser's submit mechanism or the ASP.NET postback mechanism. Default value is true. False appends the code for _doPostBack(this). Check with ViewSource after setting the value to False.

Therefore setting this value to false triggers the server event. The below code works fine.

        asp:Button ID="Button1" runat="server" UseSubmitBehavior="false" OnClientClick="this.disabled='true';this.value='processing...';" onclick="Button1_Click" Text="Button" 

        



Sunday, November 30, 2008

Wanna to Learn the Microsoft Interview Proc?

This site provides more informations on how to prepare for Microsoft Interview which usually differs from other company.

Take It!

Sunday, November 23, 2008

Good Morning - Wonderful Story! Don't Miss It

Monica married Hitesh this day. At the end of the wedding party,

Monica's mother gave her a newly opened bank saving passbook.

With Rs.1000 deposit amount.

Mother: 'Monica, take this passbook. Keep it as a record of your marriage

life. When there's something happy and memorable happened in your new

life, put some money in. Write down what it's about next to the line. The

more memorable the event is, the more money you can put in. I've done the

first one for you today. Do the others with Hitesh.When you look back

after years, you can know how much happiness you've had.'

Monica shared this with Hitesh when getting home. They both thought it

was a great idea and were anxious to know when the second deposit can be

made.

This was what they did after certain time:

- 7 Feb: Rs.100, first birthday celebration for Hitesh after marriage

- 1 Mar: Rs.300, salary raise for Monica

- 20 Mar: Rs.200, vacation trip to Bali

- 15 Apr: Rs.2000, Monica got pregnant

- 1 Jun: Rs.1000, Hitesh got promoted

..... and so on...

However, after years, they started fighting and arguing for trivial

things.They didn't talk much. They regretted that they had married the

most nasty people in the world.... no more love...Kind of typical

nowadays, huh?

One day Monica talked to her Mother:

'Mom, we can't stand it anymore. We agree to divorce. I can't imagine how

I decided to marry this guy!!!'

Mother: 'Sure, girl, that's no big deal. Just do whatever you want if you

really can't stand it. But before that, do one thing first. Remember the

saving passbook I gave you on your wedding day? Take out all money and

spend it first. You shouldn't keep any record of such a poor marriage.'

Monica thought it was true. So she went to the bank, waiting at the queue

and planning to cancel the account.

While she was waiting, she took a look at the passbook record. She looked,

and looked, and looked. Then the memory of all the previous joy and

happiness just came up her mind. Her eyes were then filled with tears. She

left and went home.

When she was home, she handed the passbook to Hitesh, asked him to spend

the money before getting divorce.

The next day, Hitesh gave the passbook back to Monica. She found a new

deposit of Rs.5000. And a line next to the record: 'This is the day I

notice

how much I've loved you thru out all these years. How much happiness

you've brought me.'

They hugged and cried, putting the passbook back to the safe.

Do you know how much money they had saved when they retired? I did not

ask.I believe the money did not matter any more after they had gone thru

all the good years in their life.

"When you fall, in any way,

Don't see the place where you fell, Instead see the place from where you

slipped.

Life is about correcting mistakes!"

ADO .net Sync service on VS using Sql Server 2005

I am going to furnish you a set of steps to follow in order to configure the SQL Server 2005 in VS for ADO. Net synchronization.

If you would have read my previous blog on  What is Synchronization and How they work, it will be a piece of cake for you to follow these steps.

I am going a head to describe the steps thinking that you have basic knowledge on ado.net synchronization.

 

  1. Create a new project in Visual Studio, preferably Windows application
  2. Add a new item called “Local Data Cache” ( This item will be displayed in the dialog only if you have installed the SQL Server Compact Edition 1.0 or higher)
  3. The filename will have a extension of .sync
  4. Once the new item is added you will have a screen to configure items.

a)     You can select the server connection, if you have any of those in the dropdown or click the New button to create a new Sql server 2005 connection

b)     Same way you can select the client connection or click the New button to create a New SQL Compact Edition ie, only Compact edition of sql server can be used as client and not SQL express

c)      Click the Add button below the Cached Tables to add the table which you want to be synchronized with the Server

d)     Click the “Show Code Example” link in the right most bottom of the screen and copy the code to the clipboard( a button will be there to do the operation). I will tell you were to use the this code.

e)     Click OK, so that at the first time the data will be downloaded from the server to the client database.

f)       It will also try to create a dataset, just click cancel.

g)     For synchronization the schema of the table in the server and client will be modified and the script for this will be place in the current solution in a folder called “SQL Scripts” and “SQL Undo scripts”. This scripts does nothing but add two columns to your table for keeping track of the changes happened in offline and to sync with the Server tables. Column name “CreationDate” and “LastEditDate”

 

 

  1. Now you can open your compact database .sdf and verify the table which will have all the data that are in the server with two additional columns
  2. Go to the view code of the .sync file and specify the Sync direction for your local database cache item. You will have different enum for Sync Direction. Since here we are going to synchronize both the client and server, specified as Bidirectional

Ex. this.DataElement.SyncDirection = Microsoft.Synchronization.Data.SyncDirection.Bidirectional;

 

  1. Now just create a form to update you client database table with values which are not updated in the server.
  2. Put a button in the form and paste the code you have in the clipboard in the click events.
  3. Now do some updates in the screen and see if the data is updated in the client and not in the server.
  4. Then click the button(sync button) which will execute the two line code and update the server and same can be done vice versa by updating the data in the server table and doing a sync, which will update in you local database.

 

I hope you got a glimpse on how to work on ado.net sync. I will explain in my next article on how to do the same with SQL Server 2008