Showing posts with label LinqDataSource. Show all posts
Showing posts with label LinqDataSource. Show all posts

Thursday, August 22, 2013

LINQDataSource Fix: No applicable method 'StartsWith' exists in type 'String' OR No applicable method 'Contains' exists

Symptom:
  • You have created a LinqDataSoruce control on the Visual Studio ASP.NET web page.
  • You have set up fields to be supplied from TextBox control (may be others.) 
  • You have customized the query to use StartsWith or Contains in the Query and the following type of puzzling error appears.
No applicable method 'StartsWith' exists in type 'String'  or
No applicable method 'Contains' exists in type 'String'

Root Cause:

Many people are very confused about this behavior and come to the conclusion to execute some special LINQ call. Apparently it has nothing to do with missing procedure but it's just the Text or control fields set up not correctly.

This is because the Control's ConvertEmptyStringToNull (for example TextBox)  property needs to be adjusted from true to false. Without it, when nothing is typed into the field, it will go out as a null object and then it cannot find the method.

Please note that it appears that this change has to be made to all of the fields in the query. 

Fix:
  • In your LinqDataSource "Where.." editor dialog box, select the (TextBox control(s)).
  • Next find "Show Advanced Properties" link. Click this.



 Then, you will find where to change ConvertEmptyStringToNull configuration.


 Or, you can edit the configuration straight in the page's source like this;


Friday, October 12, 2012

ASP.NET: 'GridView1' fired event Sorting which wasn't handled. Even with LinqDataSource in Use

 Symptom:

  • I have written an ASP.NET application with a GridView (any bound control) and enabled sorting on the control. 
  • I have manually "swapped" the LinqDataSource by another. In my case I have several that contain different Dynamic LINQ statements in them. Next thing you get the following error and the columns will not sort.

The GridView 'GridView1' fired event Sorting which wasn't handled.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:
System.Web.HttpException: The GridView 'GridView1' fired event Sorting which wasn't handled.


Root Cause: 

You are likely to have assigned a LinkDataSource to the DataSource properly. This will not work.

Fix:

If your situation is exactly like mine, like below:

       if (false==String.IsNullOrEmpty(like))
        {
            GridView1.DataSource = this.LinqDataSourceDateDesc;
        }
        else
        {
            GridView1.DataSource = this.LinqDataSource2;
        }
        GridView1.DataBind();


Then this will not work.

Fix this to use the ID of the Data Source, and the event for the sort will hook right back up again.

       if (false==String.IsNullOrEmpty(like))
        {
            GridView1.DataSourceID = "LinqDataSourceDateDesc";
        }
        else
        {
            GridView1.DataSourceID = "LinqDataSource2";
        }
        GridView1.DataBind();


Side Note:

Often when I get stuck with a problem like this, I write a simple test program that would demonstrate the situation. In this day and age, many application projects are already scaffoldded so it's just easy to come up with something quickly. In my case the grid was working initially when I built everything from the designer but as soon as I try to go fancy by assigning the data source, it broke. So this way I went back reproducing basically the original flavor, then looked at the source (in this case in the Default.aspx) and that worked, and quickly realized that it is the DataSourceID they use to bind, no other codes to hookup the events were in this simple code. 

I have seen many novice engineers getting stuck for hours or even days on an issue. If they simply wrote a case to demonstrate the issue then they could have spent much less time fixing the issue.

Saturday, June 16, 2012

LinqDataSource Control "Exception has been thrown by the target of an invocation."

Symptom


  • You are running Visual Studio 2010 SP1
  • You have an ASP.NET web page
  • You have dragged the LinqDataSource onto the design surface
  • You try to Configure Data Source and get the following error message
 Exception has been thrown by the target of an invocation

Root Cause

  • Appears to be Related to .NET Framework 4.5 present (as a result of installing Visual Studio 2012).

Fixes That Would Not Work

I have tried the following and none worked. So you may not want to go down the same path.
  • Use devenv /Log to find out what's happening.: It will not log anything useful.
  • Start devenv in /Safemode.: No effect
  • Repair Visual Studio 2010
  • Uninstall Visual Studio 2010 (note if you do this you will have to apply SP1 which takes a long time)
  • Remove or Repair Sliverlight SDKs
  • Remove or Repair .NET Framework 4.5

Possible Fix If You Do Not Need VS 2012

The following steps fixed my situation. You do not need to uninstall SQL 2012 Express
  • Uninstall VS 2012
  • Uninstall .NET Framework 4.5
  • Uninstall .NET 4.0 Multi-Traget Pack
  • Using VS 2010 Installer, Perform the Repair of VS 2010

Workaround If You Need VS 2012

  • Try another machine or a VM you have not installed anything "2012" on it, you should be able to continue to work on LinqDataSource. Once the LinqDataSource is configured, check the code in and it should work on your "corrupted" environment, though you won't be able to change anything in the data source. You can still work with the Data Grid to which the data source is connected and do add/remove columns and such. You can manually edit the web page source code to change minor things like some query changes.
  • If you can safely move up to VS 2012, then the LinqDataSource will work perfectly fine there.







Saturday, April 07, 2012

ASP.NET GridView and LinqDataSource Stuff...

The DataGrid is one of the components I use quite a bit in my line of work, and they are convenient.

I have also been using LINQ quite a bit. It is a significant time saver as well as making my code more easier to understand. But until today I never bothered to use LinqDataSource just because I did not spend time to learn a bit about it. Instead I always built the DataSets using the designer. The problem with the Datasets for me is that it does not show me exactly what I am querying easily.

Before I get into that, I have to tell you also that if your requirement is very simple, then you can feed the result of the Linq query directly to the GridView (or other data binding controls.) This might be a great way to bind the pull down menu or a list view with small number of selections.

For example,

var customers = from c in context.customers select c;
gridview1.DataSource = c;

And that really all it takes to feed a GridView. For more information on this, learn from this MSDN page.

But this won't allow you to sort or page the grid. For that purpose you need to attach a LinqDataSource. You can easily do that by:

  • Defining the LinqToSQL object in your project, or pull one in from other assembly
  • Dragging and dropping the LinqDataSource component on to of the GridView. The wizard will show you which of the LinqToSQL database context object to use.

Well that's great, but I really do not want to use the connection string that's built into Web.config. In my case, I pull the connection string common to all my applications from my own company's registry set. But at any rate, you would assign a newly created data context to e.ObjectInstance of the event argument.

So here is another trick.
  • Define an event processing method for ContextCreating event.
  • Re-instantiate the LinqDataContext class object with your connection string. Yes, just the Context is all you need here. The rest will be handled with the LinqDataSource you've created with the designer. (Yes, it is the Data Context object, not the Linq Select result.)

       protected void LinqDataSource1_ContextCreating(object sender,
           LinqDataSourceContextEventArgs e)
        {
            string myConnectionString="your connection string";
            e.ObjectInstance = new MyDataConext(myConnectionString);
        }

Here is another bonus thing you can do. If you do this type of connection string swapping, it is best to create a new context factory class of your own and then call that. This factory class can do the connection string swapping for you. This will centralize the connection string swapping.

In my case, the connection information is stored locally in a registry and many of my own applications pull that information from there at run-time. This way, all I need to do is to set the connection strings once and then all other applications I write will not need separate connection configurations. There are of course many other ways of doing it.