Thursday, January 06, 2011

Asking to Install Silverlight 4 when it’s already installed with Visual Studio 2010

Asking to Install Silverlight 4 when it's already installed with Visual Studio 2010
Check one thing in your system


1) Make sure Microsoft Silverlight is enabled on internet explorer. 
Go to IE à Tools à Internet optionsà on the programs tab click on "manage add ons" button and check for Microsoft Silverlight 4 is Enable.
Still if you are not able to solve this please uninstall all Silverlight components and install in proper way, follow the installation instruction in http://www.microsoft.com/getsilverlight/Get-Started/Install/Default.aspx
2) If you don't have time to do this so, still you can test your application by using Silverlight enable browser.
  • Go to the Project à right click à go to Property page
  • Go to the Silverlight page and click on the "Enable running application out of the Browser
  • Now run your application using Visual Studio 2010
3) Make sure you are running silverlight App on 32 bit Browser, Because  Silverlight App does not works on 64 Bit IE Browsers.
  
I Solve this issue in following way, It is not the solution but its works for me


When I create application first time and run, it giving me pop up for Silverlight installation..
Now go to project properties --> Silverlight Tab and just Check and Uncheck again (so no change) on the 
"Enable running application out of browser" check box and close this window with Save or without saving now run the application (press F5), it works fine. 


Make sure your startup project set as SilverlightApplication NOT SilverlightApplication.Web


I tested this with
  • SilverLight 4 
  • IE Browser 8/9 32bit (this issue  not comes in other browser like Chrome, Mozilla etc.)
  • Visual Studio 2010
  • Window 7 as OS
Finally Finally I got the solution for this .....................
  1. Make sure you are not running IE 64 bit, because silverlight 4.0 and less  does not support IE 64 bit, Silverlight 5.0 and above support this
  2. Now if you are using IE 32 bit then Add your application URL as a Trusted site, try following option

Go to IE --> Internet Options -->  Security --> Trusted sites --> Sites --> Add

  • Add you site URL
  • Uncheck "Require server verification (https:) for all sites in this zone"

That's it, you are good to go.

Attempting to access a service in a cross-domain way without a proper cross-domain policy in place......

Error: Attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute.
I resolved this issue by placing "ClientAccessPolicy.xml" into Web service location "C:/inetpup/MyService" folder.
Go ahead and create one "ClientAccessPolicy.xml" file with following content and save into your service location folder in "C:/inetpup/
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
       <cross-domain-access>
              <policy>
                     <allow-from http-request-headers="SOAPAction">
                           <domain uri="*"/>
                     </allow-from>
                     <grant-to>
                           <resource path="/" include-subpaths="true" />
                     </grant-to>
              </policy>
       </cross-domain-access>
</access-policy>

In some cases you may have place one more file "crossdomain.xml"

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

Monday, January 03, 2011

CS0135: 'Model' conflicts with the declaration 'System.Web.Mvc.ViewPage.Model'

CS0135: 'Model' conflicts with the declarationSystem.Web.Mvc.ViewPage<TModel>.Model
After spending couple of hours I found the solution, before my try I tried all the option suggested on internet like: add the namespace with the class which you are using
Inherits="System.Web.Mvc.ViewPage<Attachment>"  and add a
<%@ Import namespace="MyProject.Models" %> Or
Inherits="System.Web.Mvc.ViewPage< MyProject.Models.Attachment>" 

But for me problem was different, the problem was
Cause
I used validation control (ValidationMessageFor) to and passed wrong parameter with "=>" expression like:
Line -1: <%= Html.ValidationMessageFor(Model => Model.Employee)%></div>

Line -2: <% Html.RenderPartial("Attachment", Model ); %> 
Because of Line -1: mistake Line -2: was giving error "CS0135: 'Model' conflicts with the declaration 'System.Web.Mvc.ViewPage<TModel>.Model"
Solution
I correct above line like below
<%= Html.ValidationMessageFor(Employee=> Model.Employee)%></div>

<% Html.RenderPartial("Attachment", Model ); %>
It was very stupid mistake but its took longer time to analyzed, Now my page is working fine.

Saturday, January 01, 2011

ASP.NET MVC 2 common problems while develope a View

ASP.NET MVC 2 common problems and solutions
While working with MVC2 with ASP.Net, I faced couples of problem while developing the View form, some of those are below
1)      Multiple submit button on the same page
2)      How to make Default Button
3)      How to add Image on the Submit Button
4)      Validation Control on ASP.Net MVC 2
5)      How to add Image with the validation message
After spending couple of hours I got good links where you get all these solution
How to put multiple submit button on the same page
When you have one web page having couples of action then you need multiple buttons on the same page and each button has different ResultAction, Find solution here
How to make Default Button in ASP.Net MVC View
When you have multiple button and out of that you want to make one button as default, Find solution here
How Image button works like page Submit button in ASP.Net MVC View
When you have a requirement to put Image on the page submit button and have multiple submit button on same page. The main problem is Image button will not work like Submit button then how to put image on the submit button. I tied following solution and it works for me , Find solution here
Validation Control on ASP.Net MVC 2
When you have to implements validation control in your ASP.Net MVC 2 web form then I found very good article about this, How to use validation control in ASP.Net MVC 2, Find Solution here
Add image with validation message
You have validation message and with the validation message your requirement to add Image (like: alert, explanation image etc.), then you can find the solution from here

Thursday, December 30, 2010

WCF Errors with IIS7 with .Net Framework 4.0

WCF Errors with IIS7 with .Net Framework 4.0 

When I start hosting WCF service on the IIS7 then I got following errors and I spend enough time to resolve this, following error could be caused by different reason but I described the reason and solution which I faced. I was using Visual Studio 2010 with .Net framework 4.0 and IIS7.  

Error-1: HTTP Error 404.17 - Not Found
The requested content appears to be script and will not be served by the static file handler.

Cause
WCF service developed on different .NET framework (V4.0) and Application Pool set in different version (V2.0).
Solution
Go to the IIS 7 and select Application Pools à right click on your application and change the .NET Framework version to V4.0. (Change the v2.0 to v4.0 in Application Pool)

Error-2: HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.


Cause
This error occurs when you application deployed on the different version of .NET and Web.config has different version mentioned.
Like: Application Pool running on .Net 4.0 and Web.Config includes .Net 3.5
Solution
Go to the application project à right click and go to "Property Pages"à Build tab and Set the application Target Framework to .Net Framework 4.0.
Another quick fix, change application pool set to 2.0 as default.
Error-3: No protocol binding matches the given address 'http://localhost/IISHostedService/MyService.svc'. Protocol bindings are configured at the Site level in IIS or WAS configuration.

 <endpoint address="http://localhost/IISHostedService/MyService.svc" binding="wsHttpBinding" contract="IMyService">
Solution
Remove this address URL from Web.config, because this is already configured in IIS.
Go to the web.config and change in the Services section like below
<endpoint address="" binding="wsHttpBinding" contract="IMyService">


Error-4: Metadata publishing for this service is currently disabled.


There are couples of help available on internet and on the site page where this error displays. But for that was not the problem

Cause

Normally this error occurs when "httpGetEnabled" is false in Web.config 
check your web.config <serviceMetadata httpGetEnabled="true">


But for me the problem was, I created one folder explicitly in “C:/inetpub” called ‘MyFolder’ and then from the visual studio I created WCF service on that folder choosing

Web location: File System and File created on “C:/inetpub/MyFolder”.


Now when I deploy my WCF service and try to access then its throws following error “
Metadata publishing for this service is currently disabled.

Solution

DONOT create any folder explicitly on “C:/inetpub” and while creating new WCF service choose following option

Web location: HTTP and File created on “http://localhost/MyFolder”.

Wednesday, October 13, 2010

How to pass Credential to Web Service with Basic Authentication

How to pass Credential to Web Service with Basic Authentication
 You can pass credential to web service in different ways
Ø  Manual:  Passing user Id and Password manually
Ø  Default: Get the System Credential of the application.
Ø  Network: Get the network Credential of the current security.
C#.NET code Sample for Manual
using System.Net;

// MyWebService is the web service reference to your project
MyWebService myService = new MyWebService();
myService.Timeout = -1;
myService.PreAuthenticate = true;
System.Net.CredentialCache userCredentials = new System.Net.CredentialCache();
NetworkCredential netCred = new NetworkCredential("User Id", "Password");
userCredentials.Add(new Uri(myService.Url), "Basic", netCred);
myService.Credentials = userCredentials;

You can also use Default or Network Credentials like below
C#.NET code Sample for Default Credential
// MyWebService is the web service reference to your project
MyWebService myService = new MyWebService();
myService.Timeout = -1;
myService.PreAuthenticate = true;
myService.Credentials = System.Net.CredentialCache.DefaultCredentials;

C#.NET code Sample for Default Network Credential
// MyWebService is the web service reference to your project
MyWebService myService = new MyWebService();
myService.Timeout = -1;
myService.PreAuthenticate = true;
myService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

Now you can call methods on the myService to use.

Friday, October 01, 2010

Table Value Parameter with LINQ, (LINQ for Stored Procedure that has datatable as an input parameter)

Table Value Parameter with LINQ, (LINQ for Stored Procedure that has datatable as an input parameter)
If you are using LINQ to SQL with SQL server database then Visual Studio will not allow you to drag your SP into dbml file like you do for other simple SP and also Visual Studio will not able to generate the code for this. There is no other ways to generate the code for this situation (SP has input parameter as a datatable) in the DataContext.
You have to write the code into partial class (given with your .dbml file), now assume you have SP signature like below

Stored Procedure

CREATE PROCEDURE [dbo].[GetResource]
( @ResourceID dbo.Resource READONLY) 

Resource: is datatable type

CREATE TYPE [dbo].[Resource] AS TABLE
( [ResourceId] [int] NOT NULL )

While writing the code in the DataContext we have to manually execute the SP and return as a List, see below code

NameSpace

using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;

DataContext Code

public partial class YourDataContext : System.Data.Linq.DataContext
{
public IEnumerable<ResourceData> GetResource(List<int> resourceId)
{
DataTable resourceIdTable = new DataTable();
resourceIdTable.Columns.Add("Resource", typeof(int));
// Fill the datatable from the input List
foreach (int r in resourceId)
resourceIdTable.Rows.Add(r);
// "GetSqlCommand()" is a method will open the connection and return sqlCommand Object
SqlCommand cmd = ConnectionManager.GetSqlCommand("GetResource", connectionString);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = cmd.Parameters.AddWithValue("@ResourceID", resourceIdTable);
p1.SqlDbType = SqlDbType.Structured;
p1.TypeName = "Resource";
SqlDataReader reader = cmd.ExecuteReader();
return this.Translate<ResourceData>(reader).ToList();
}
}

Call DataContext Method

YourDataContext rdc = new YourDataContext();
IEnumerable<ResourceData> result = rdc.GetResource(resourceId);

foreach (ResourceData res in result)
{ MessageBox.Show("ResourceId : " + res.Id + " Resource Name : " + res.Name);
}

ResourceData: is Entity class like below

public sealed class ResourceData
{
private int _id;
private string _name;
public int Id
{
get { return _id; }
private set { _id = value; }
}
public string Name
{
get { return _name; }
private set { _name = value; }
}
}

LINQ for Stored procedure returning Multiple ResultSets

LINQ for Stored procedure returning Multiple ResultSets

When you have SP which is returning multiple recordset and when you try to drag into dbml file then Visual Studio will not able to generate the code for this in the DataContext class.

For this you have to manually write the code into Partial class (present with the dbml file), I usually drag my SP into dbml file and then copy past generated code into partial class and modify, just replace "ISingleResult" with "IMultipleResults".

Let say you have one SP called "GerResources" which is returning two RecordSets

Stored Procedure

CREATE PROCEDURE [dbo].[GetResource]

( @ResourceID int

)

AS

Begin

Select Id, Value from Employee where ResourceId = @ResourceID

Select Name , Value from Content where ResourceId = @ResourceID

End

NameSpace

using System.Data.Linq;

using System.Data.Linq.Mapping;

using System.Linq;

using System.Linq.Expressions;

DataContext Code

public partial class YourDataContext : System.Data.Linq.DataContext

{

[FunctionAttribute(Name = "dbo.GetResource")]

[ResultType(typeof(Employee))]

[ResultType(typeof(Content))]

public IMultipleResults GetResource(int resourceID)

{

IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), resourceID);

return ((IMultipleResults)(result.ReturnValue));

}

}

Content and Employee is Entity class.

public sealed class Employee

{

private int _id;

private string _value;

public int Id

{

get { return _id; }

private set { _id = value; }

}

public string Value

{

get { return _value; }

private set { _value = value; }

}

}

public sealed class Content

{

private int _name;

private string _value;

public int Name

{

get { return _name; }

private set { _name = value; }

}

public string Value

{

get { return _value; }

private set { _value = value; }

}

}

Call DataContext Method

// Create and open the connection

YourDataContext rdc = new YourDataContext();

//Get multiple records from resource (Employee)

List<Employee> emp = result.GetResult<Employee>().ToList();

//Get multiple records from resource (content)

List<Content> cont = result.GetResult<Content>().ToList();

//Read the Data from Employee Object

foreach (Employee e in emp)

{

MessageBox.Show(e.Id + e.Value);

}

//Read the Data from Content Object

foreach (Content c in cont)

{

MessageBox.Show(c.Name + c.Value);

}

To read only Single records from LINQ

Note: If you have single Row returning from SP then use "Single()" method instead of "foreach" loop

//Create and open the connection

YourDataContext rdc = new YourDataContext();

//Get multiple records from resource (Employee)

List<Employee> emp = result.GetResult<Employee>().Single();

//Display the value

MessageBox.Show(e.Id + e.Value);