Search code examples
c#sql-servervisual-studiosqldataadapter

SqlDataAdapter not passing data to datatable using fill method


I come to you today with an error that has me completely stumped, as this code is literally reused code from elsewhere in my application and works fine in other areas where its used. The only real difference being that this is the first time anyone has tried connecting to this particular SQL server in this manner.

It is worth noting that this exact code WORKED a couple of times, then just decided to die.

CODE GENERATOR: Visual Studio 2012, C#

REQUIREMENT: Form that connects to the SQL server database, extracts data from table, saves it to DB2 database to be reused later.

ISSUE: When the data table is to be filled via the SqlDataAdapter.Fill method, no data is passed to the data table, thus triggering the catch portion.

ATTEMPTED SOLUTIONS:

Changed platform targets between x86 and x64; both fail (application is x86 native).

Restarted visual studio, fail.

Closed sql connection before SqlDataAdapter in case it opens its own connection, fail.

EXCEPTION MESSAGE:

System.ArgumentNullException: El valor no puede ser nulo. //Value cannot be null

Nombre del parámetro: dataTable //Parameter name

en System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) en System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) en PROJECT.FORM.loadSQLData() en c:\FORM.cs:línea 101

CODE SNIPPET:

string connectionString = @"Data Source=SQLSERVER;Initial Catalog=DATABASE;user id=USER;password=PASSWORD;";
string querySelect = @"SELECT TOP 100 COLUMN1 FROM dbo.TABLE";
SqlConnection cnn;

//Datatable definition
public Datatable table = new Datatable();

public form_Load(object sender, EventArgs e)
{
    loadSQLData();
}

private void loadSQLData()
{
    try
    {
        cnn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(querySelect, cnn);
        if (cnn.State == ConnectionState.Open) cnn.Close(); //Just in case there was an open zombie connection to the server
        cnn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(table); //Dies right here
    }
    catch
    {
        //Catch stuff
    }
    finally
    {
        cnn.Close();
    }
}```





Solution

  • Many thanks for all your answers and advice. I've managed to resolve the issue, which turned out to be a little less intuitive than I expected.

    As it turns out, I was incorrectly instantiating my datatables as follows:

    public DataTable table1, table2 = new DataTable();

    Visual studio was not picking this up as an error and allowing normal compilation. The error popped at runtime, since table1 wasn't properly instantiated. It was resolved by instantiating each datatable individually, such as follows:

    public Datatable table1 = new Datatable();

    public DataTable table2 = new DataTable();

    I remember doing this as a way to save line space, but alas it really just blew up on me.

    Cheers!