C # SQL Query for public DataTable

I have searched the archives for questions but cannot find a suitable solution. I am sorry if it really 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 populate the 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 in the fill command. Error message as below:

System.ArgumentNullException: {"The value cannot be null. \ R \ nParameter name: dataTable"}

Can anyone help me stop this exception?

+3


source to share


1 answer


Try assigning an empty table before using the table variable in SqlDataAdapter

. Something 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 it helps ...

+2


source







All Articles