1

I have proyect that use linq to postgresql, I have the library and Its three dependencies.

But when I try to connect to de DB I get this error:

First chance of Exception of type ' System.NullReferenceException ' in linq2db.dll. Additional information: Object reference not set to an instance of an object. (Translated).

Stack trace:

in LinqToDB.Data.DataConnection..ctor(String configurationString) in i:\linq2db\Source\Data\DataConnection.cs:line 41 in LinqToDB.Data.DataConnection..ctor() in i:\linq2db\Source\Data\DataConnection.cs:line 24 in modelo.BDGestion.bdgestionDB..ctor() in d:\Proyectos\Proyectos VisualStudio\TimeSheets\TimeSheets\modelo\BDGestion.PostgreSQL.generated.cs:línea 44 en TimeSheets.SeleccionPersonal..ctor() in d:\Proyectos\Proyectos VisualStudio\TimeSheets\TimeSheets\SeleccionPersonal.cs:line 23

public partial class SeleccionPersonal : Form
    {
        public SeleccionPersonal()
        {
            InitializeComponent();
            try
            {
                using (var db = new bdgestionDB())
                {

                }
            }catch(Exception ex){
                MessageBox.Show(ex.ToString());
            }
        }
    }

I generated the classes using the template, the connection String is well formed and properly added to de connectionStrings. Checked it by me in this solution and by a friend in other computer by its own. If you need any information just ask.

Generated class:

public partial class bdgestionDB : LinqToDB.Data.DataConnection
{
    tables...

    public bdgestionDB()
    {
        InitDataContext();
    }

    public bdgestionDB(string configuration)
        : base(configuration)
    {
        InitDataContext();
    }

    partial void InitDataContext();
}

The App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="Postgres" connectionString="Server=thot-test;Port=5432;Database=bdgestion;UserId=test;Password=T15Pmc;Pooling=true;MinPoolSize=10;MaxPoolSize=100;Protocol=3;"/>
  </connectionStrings>
</configuration>
5
  • 1
    possible duplicate of What is a NullReferenceException and how do I fix it? Commented Apr 21, 2015 at 6:41
  • The problem it's not the NulReferenceException, i don't use an object by my self! Please, read the code before say that it's duplicated. Commented Apr 21, 2015 at 6:48
  • The exception indicates that your connectionString is incorrect and this leads to a Null reference. Commented Apr 21, 2015 at 6:50
  • What is configuration? Commented Apr 21, 2015 at 6:51
  • The connectionString is: Server=thot-test;Port=5432;Database=bdgestion;UserId=test;Password=test;Pooling=true;MinPoolSize=10;MaxPoolSize=100;Protocol=3; and works when i use it with Npgsql directly, my friend uses a different one and same problem Commented Apr 21, 2015 at 6:52

1 Answer 1

2

I googled the source snipped and got pointed in the direction of this file.

Looking at the line 41, it tries to access MappingSchema from DataProvider. From this it looks like the DataProvider is null. Googling some more I found this page. Looks like you're missing provider name in your connection string:

<add name="Northwind" 
connectionString = "Server=.\;Database=Northwind;Trusted_Connection=True;Enlist=False;" 
providerName     = "PostgreSQL" />

Here you can find an example of using Linq2db, connecting to the PostgreSQL database.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, that was the error. I didn't search the source but is a good tip to follow. C# have a good documentation when things work but nothing when goes wrong.
I used this template, but the one that Nuget download doesn't have the providerName att. Please, watch it carefully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.