Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# SQL Query for public DataTable

I have searched through the archive questions but can't find a suitable solution. I am sorry if one actually exists.

I am using Visual Studio 2013, .NET Runtime 4.5, MS-SQL 2008.

My code is simple:

public static class Global
{
    public static DataTable CityTable;
}

To fill my data table I call:

SqlCommand SC1 = new SqlComman("SELECT DISTINCT City FROM Final WHERE City !='' AND City IS NOT null AND Published LIKE '%/%/" + DateTime.Now.ToString("yyyy") + "'", conn);

SqlDataAdapter SDA1 = new SqlDataAdapter(SC1);

SDA1.Fill(Golbal.CityTable);

Every time the call is made I get an error on the fill command. Error message as below:

System.ArgumentNullException : {"Value cannot be null.\r\nParameter name: dataTable"}

Can anyone help me to stop this exception?

like image 212
user3244026 Avatar asked Jun 06 '26 16:06

user3244026


1 Answers

Try assigning an empty table before using the table variable in SqlDataAdapter. Something as like,

 SqlCommand SC1 = new SqlCommand("select distinct City from Final " +
                                 "where City!='' and city is not null and " +
                                 "Published like'%/%/" + 
                                  DateTime.Now.ToString("yyyy") + "'", conn);
 SqlDataAdapter SDA1 = new SqlDataAdapter(SC1);
 Global.CityTable = new DataTable();
 SDA1.Fill(Global.CityTable);

Hope this helps...

like image 189
Vanest Avatar answered Jun 08 '26 05:06

Vanest