Converting DataView to DataSet

I have a Dataview that is populated with 3 rows of data from a stored procedure. I want to place this table in a dataset. I read, but I could be wrong, this is to go from DataView to DataTable To DataSet.

Below is my code. When I click the if stat it shows my DT2.Rows.Count = 3, so it starts DS.Tables.Add (DT2); then throws the following error. "NullReferenceException is not handled by user code" An object reference is not set on an object instance.

I was wondering how to get the DataView in the dataset? Thank you for any suggestions.

DataSet DS;
DataView DV = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataTable DT2 = DV.ToTable();

if (DT2 != null & DT2.Rows.Count > 0)
{
       DS.Tables.Add(DT2);
}

      

+3


source to share


2 answers


Your DataSet null

. Just initiate it and you should be good to go:

DataSet DS = New DataSet();

      



The rest of the code is correct.

+4


source


DataSet DS=new DataSet();
DataView DV = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DS.Tables.Add(DV.ToTable());

      



+1


source







All Articles