It is simple and silly.
This is the code:
static void Main(string[] args)
{
using (var connection = new SqliteConnection("" +
new SqliteConnectionStringBuilder
{
DataSource = "hello.db"
}))
{
connection.Open();
}
}
It does not work. It can't be simpler, but it still doesn't work. The Internet seems quiet about that problem.
It throws an exception on Open(); Exception is this:
Exception has occurred: CLR/System.NullReferenceException An unhandled
exception of type 'System.NullReferenceException' occurred in
Microsoft.Data.Sqlite.dll: 'Object reference not set to an instance of
an object.' at Microsoft.Data.Sqlite.SqliteConnection.Open() at
archive.Program.Main(String[] args) in
D:\projects\test\Program.cs:line 55
What is going on?
Please note - I am working on a piece of code that was using System.Data.Sqlite and old, 'normal' .net
When I moved to Microsoft.Data.Sqlite (and .net core), it protests.
You need to reference Microsoft.EntityFrameworkCore.SqlLite NuGet package for this to work, if you don't and you only have reference to Microsoft.Data.Sqlite.Core you will get this error.
Related
I am using VS 2022, .Net 6.0, and trying to build my first app using System.CommandLine.
Problem: when I build it, I get an error
The name 'CommandHandler' does not exist in the current context
The code I'm trying to build is the sample app from the GitHub site: https://github.com/dotnet/command-line-api/blob/main/docs/Your-first-app-with-System-CommandLine.md , without alteration (I think).
It looks like this:
using System;
using System.CommandLine;
using System.IO;
static int Main(string[] args)
{
// Create a root command with some options
var rootCommand = new RootCommand
{
new Option<int>(
"--int-option",
getDefaultValue: () => 42,
description: "An option whose argument is parsed as an int"),
new Option<bool>(
"--bool-option",
"An option whose argument is parsed as a bool"),
new Option<FileInfo>(
"--file-option",
"An option whose argument is parsed as a FileInfo")
};
rootCommand.Description = "My sample app";
// Note that the parameters of the handler method are matched according to the names of the options
rootCommand.Handler = CommandHandler.Create<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
{
Console.WriteLine($"The value for --int-option is: {intOption}");
Console.WriteLine($"The value for --bool-option is: {boolOption}");
Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}");
});
// Parse the incoming args and invoke the handler
return rootCommand.InvokeAsync(args).Result;
}
I have installed the latest version of System.Commandline: 2.0.0-beta2.21617.1
SURELY I am just being a big fat idiot in some respect. But I don't see it.
Any insight would be welcomed.
This issue is caused by updating the CommandLine 2.0 Beta 2 package. Add the reference System.CommandLine.NamingConventionBinder to the references to fix the problem. Follow the announcements on command-line-api's GitHub account:
In your project, add a reference to System.CommandLine.NamingConventionBinder.
In your code, change references to the System.CommandLine.Invocation namespace to
use System.CommandLine.NamingConventionBinder, where the CommandHandler.Create
methods are now found. (There’s no longer a CommandHandler type in
System.CommandLine, so after you update you’ll get compilation errors until you
reference System.CommandLine.NamingConventionBinder.)
If you want to continue with the old habits, try using older versions of the System.CommandLine package.
References
Announcing System.CommandLine 2.0 Beta 2 and the road to GA
Think you are missing a using line:
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
I can't swear that's it, but it looks like CommandHandler is defined in a namespace not referenced by a using (in your current code), so System.CommandLine.Invocation may be the key!
I'm working in a team project (MVC aplication). After the i made a update, the application starts to give the HttpException "Type casting is failed!" and i don't know why.
The code:
public static class UrlHelper
{
private static System.Web.Mvc.UrlHelper _urlHelper;
public static System.Web.Mvc.UrlHelper GetFromContext()
{
if (_urlHelper == null)
{
if (HttpContext.Current == null)
{
throw new HttpException("Current httpcontext is null!");
}
if (!(HttpContext.Current.CurrentHandler is System.Web.Mvc.MvcHandler))
{
throw new HttpException("Type casting is failed!");
}
_urlHelper = new System.Web.Mvc.UrlHelper(((System.Web.Mvc.MvcHandler)HttpContext.Current.CurrentHandler).RequestContext);
}
return _urlHelper;
}
}
The error ocorre in the following line of code and "The type 'System.Web.Mvc.MvcHandler' exists in both 'System.Web.Mvc.dll' and 'System.Web.Mvc.dll'"
if (!(HttpContext.Current.CurrentHandler is System.Web.Mvc.MvcHandler))
Any Idea?
[EDIT WITH MORE INFORMATION]
I try the following things without any success.
I clean the solution.
I delete the bin of my project and the bin of all dependencies of
them.
I check for changes that i have mad (in the main project and
dependencies) in other trying to see if they have impact of error.
Util information:
Using Visual Studio Ultimate 2013 with Update 3, version
12.0.30723.00
Using Microsoft .NET FrameWork, version 4.5.51650
Using System.Web, version 4.0.30319
Using MVC 5, version 5.1.0.0
Try this:
if (!(System.Web.HttpContext.Current.CurrentHandler is System.Web.Mvc.MvcHandler))
{
throw new HttpException("Type casting is failed!");
}
The problem was in web.config file. In their, some of the configurations wasn't right. After put all the configurations correctly, the problem was solve. I don't know exactly what cause this specific error, i just know the problem was the web.config file
I have a C# application and I modified it to show a new window using the lines:
private void button1_Click(object sender, EventArgs e)
{
WelcomeScreen channelBar = new WelcomeScreen(true, "http://www.trade-ideas.com/cms_static/ChannelBar/channelbar.html");
}
It compiles just fine, but when I run the app and click on the button, I get this error:
An unhandled exception of type 'System.TypeLoadException' occurred in WindowsFormsApplication1.exe
Additional information: Could not load type 'TradeIdeas.TIProData.OddsMakerColumnConfiguration' from assembly 'TIProData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
What doesn't make any sense is that WelcomeScreen comes from TIProGUI.dll not TIProData. I have included both dll's in the project along with a 3rd dll:
using TradeIdeas.TIProData;
using TradeIdeas.TIProGUI;
using TradeIdeas.TIProData.Configuration;
Also, when I run the project initially, I see a very strange message. It says:
Loading symbols from TIProData.dll from:
\\MissionControl\Users\KLewis2\Documents\CVSRoot\C_Sharp\TIProData\Obj\Release
Needless to say, there is no path to that from my machine.
Any clues as to what is causing this would be greatly appreciated.
There are many reasons and one of common once is exceptions in static initializers.
To diagnose - debug program with exceptions set to "break when thrown" and disabled "my code only". When exception happen take note of call stack and check all exceptions thrown from static initializers.
Location of options:
Tools-> Options -> Debug -> My Code only (uncheck)
Debug -> Exceptions -> Common Language Runtime Exceptions (check "thrown")
Sample code that will cause that error:
class MyClass
{
static int value = ReadFromConifg();
static int ReadFromConifg()
{...
throw new ConfigMissingException();
}
}
EDIT: Ok, I noticed I changed the output folder of the project and put the DLL into the old output folder. After putting it in the right output folder Open() works! I get another exception later, but I guess I can fix that...
I am working on a program that is running on .NET on Windows and on Mono on Linux/Mac. I'm trying to add a very simple SQLite logger to it. I added the Mono.Data.Sqlite.dll (of Mono 3.10) to the project references in Visual Studio 2013 and copied a sqlite3.dll (http://www.sqlite.org/2014/sqlite-dll-win32-x86-3080701.zip) into the debug folder (where the program exe is created).
This is my test code:
using Mono.Data.Sqlite;
using System.Data;
namespace Program.Logging
{
class MyLogger
{
public void TestMethod()
{
string connectionString = "URI=file:SqliteTest.db";
IDbConnection dbcon;
dbcon = (IDbConnection)new SqliteConnection(connectionString);
dbcon.Open();
}
}
}
But when I try to run the code I get this error at dbcon.Open():
An unhandled exception of type 'System.EntryPointNotFoundException'
occurred in Mono.Data.Sqlite.dll
Additional information: Der Einstiegspunkt "sqlite3_next_stmt" wurde
nicht in der DLL "sqlite3" gefunden. (The entry point
"sqlite3_next_stmt" was not found in the DLL "sqlite3".)
What am I doing wrong?
EDIT:
string connectionString = "Data Source=file:SqliteTest.db";
An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll. Additional information: URI-Formate werden nicht
unterstützt. (URI formats aren't supported.)
string connectionString = "URI=file:SqliteTest.db,version3";
An unhandled exception of type 'System.ArgumentException' occurred in
Mono.Data.Sqlite.dll. Additional information: Invalid ConnectionString
format for parameter "version3"
It appears you are using the wrong connection string format. The documentation specifies the connection string formats:
[1.1 profile and the old assembly]
URI=file:/path/to/file
[2.0 profile in the new assembly]
Data Source=file:/path/to/file
Since you are targeting sqlite3, I guess you should be using the 2.0 profile format.
string connectionString = "Data Source=file:SqliteTest.db";
They also mention that in the 1.1 profile, sqlite version 2 is used by default, you would have to specify the version if you want to use if you use the old format.
string connectionString = "URI=file:SqliteTest.db,version=3";
Recently in my WinForm project, I installed MiniProfiler.Windows and write following decorator for my QueryHandlers(I'm using CQRS):
public class MiniProfilerQueryHandlerDecorator<TQuery,TResult>:IQueryHandler<TQuery,TResult> where TQuery : IQueryParameter<TResult>
{
private readonly IQueryHandler<TQuery, TResult> _decoratee;
public MiniProfilerQueryHandlerDecorator(IQueryHandler<TQuery, TResult> decoratee)
{
_decoratee = decoratee;
}
public TResult Handle(TQuery request)
{
TResult result;
using (StackExchange.Profiling.MiniProfiler.Current.Step("Call QueryHandler"))
{
result =_decoratee.Handle(request); //call some Linq to entity queries
}
var friendlyString = ConsoleProfiling.StopAndGetConsoleFriendlyOutputStringWithSqlTimings();
Debug.WriteLine(friendlyString);
return result;
}
}
I get following error at var friendlyString=ConsoleProfiling.StopAndGetConsoleFriendlyOutputStringWithSqlTimings()
line.
An unhandled exception of type 'System.MissingMethodException' occurred in IASCo.Application.Core.dll
Additional information: Method not found: 'Boolean StackExchange.Profiling.MiniProfiler.get_HasSqlTimings()'.
Does anyone know where is the problem?
MissingMethodException = an attempt is made to dynamically access a deleted or renamed method of an assembly that is not referenced by its strong name (msdn).
Or as this answer puts it:
This is a problem which can occur when there is an old version of a DLL still lingering somewhere around
I notice that the MiniProfiler.Windows library is using a very old (over 2 years) version of MiniProfiler. That version of the code did indeed have a MiniProfiler.HasSqlTimings property. However, the current version (3.0.11) no longer has this property.
I am guessing that you are using the code from the MiniProfiler.Windows library that you linked above, but instead of using the v2 MiniProfiler dll that they have saved in /packages, you are using a v3 MiniProfiler dll (maybe downloaded from nuget). This would explain the exception that you are getting.
If this is indeed the case, then you can solve this by either downloading the version 2.0.2 nuget (Install-Package MiniProfiler -Version 2.0.2) or by upgrading the code in ConsoleProfiling to be compatible with MiniProfiler v3.