I have a Dataview that is being populated with 3 rows of data from a stored procedure. I want to put that table in a Dataset. I read, but I may be wrong, is to go from DataView to DataTable To DataSet.
Below is my code. When I hit the if statment, it shows that my DT2.Rows.Count = 3, so it runs DS.Tables.Add(DT2); then throws the following Error. "NullReferenceException was unhandled by user code" Object reference not set to an instance of an object.
I was wondering how does one get a DataView to a 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);
}
Your DataSet is null. Just initiate it and you should be good to go:
DataSet DS = New DataSet();
The rest of your code is correct.
DataSet DS=new DataSet();
DataView DV = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DS.Tables.Add(DV.ToTable());
Related
Below is my code.
DataSet ds = new DataSet();
ds = dsComplaints;
DataTable dt = new DataTable();
dt = ds.Tables[0];
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] {"--Select Complaint--" };
dt.Rows.InsertAt(dr, 0);
dsComplaints is globally declared which has the main data. I am copying it to a local DataSet ds. Then I assign the table from ds to a datatable. I am adding a row(select complaint) in data table. But when the insertion happens, my main dataset i.e. dsComplaints is also added with a row. How can i avoid that. I don't know why this is happening.
You can use the Copy method to create a new copy of same DataSet instead of using the reference only of the original one to avoid this :
DataSet ds = dsComplaints.Copy();
From MSDN DOCS this is what Copy does:
Copies both the structure and data for this DataSet.
Now you can do whatever is needed with the copy and it would not effect the original version of DataSet.
I have a problem binding datagridview to dataset, when I debug code I can see that dataset is filled ok with data from database, but it wont show on datagridview...
Here is my code:
MySqlConnection conn = new MySqlConnection(connectionString);
string sql = #"select artikli.idArtikla, artikli.NazivArtikla
from artikli";
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Artikli");
// Bind the data table to the data grid
dataGridView1.DataSource = ds;
EDIT: Thx for answers, now when I can see my data in grid, what is the easiest way to allow inserting, deleting and editing in my grid and forwarding those cnages to my database table?
DataGridView doesn't know how to bind DataSet, you must bind a DataTable.
dataGridView1.DataSource = ds.Tables["Artikli"];
or
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Artikli";
You need to provide the which DataTable you want to bind to DataGridView.
Try:
dataGridView1.DataSource = ds.Tables[0];
You forgot to bind the dataset to gridview.
At the end of your code add the following line.. after datasource.
dataGridView1.DataBind();
I am setting my gridview.datasource into a datatable variable as below:
DataTable dt = gvPaymentHistory.DataSource as DataTable;
The gvPaymentHistory.DataSource has a record, however, the dt is null after that line has executed. How can I pass the Datasource records to dt?
EDIT
DataSource is List collection of a class object. It's not a DataSet
Simple way is to store the data source of the grid in view source when u r binding the data to the grid and then retrieve it from the view source every time you need it.
Gridview.Datasource = yourdatasource;
ViewState["mydatasource"] = yourdatasource;
While retrieving
DataTable dt = ViewState["mydatasource"] as DataTable;
Hope this solves your problem.
Try This Way: Use BindingSource
BindingSource bs = (BindingSource)gvPaymentHistory.DataSource;
DataTable dt =((YourDataSetType) (bs.DataSource)).Tables[0];
EDIT
if the bidning type is list than try out
List<CodeEntity> data= gvPaymentHistory.DataSource as List<CodeEntity>;
or
List<CodeEntity> codes = (List<CodeEntity>)gvPaymentHistory.DataSource;
check this
if(gvPaymentHistory.DataSource is DataTable)
DataTable dt = gvPaymentHistory.DataSource as DataTable;
if(gvPaymentHistory.DataSource is DataView )
DataView dv = gvPaymentHistory.DataSource as DataView;
I want a user to have different "views" of a TableAdapter. These do not need to be committed back to the database.
I have tried something like the following
mytba.DataView = someOtherDataView;
with no success.
Is it possible to do this?
You need to create a new DataView from the underlying DataTable.
It's been a while since I used TableAdapters, but you can fill a data table with it
DataTable dt = new DataTable();
MyTableAdapter.Fill(dt);
DataView dv1 = new DataView(dt);
DataView dv2 = new DataView(dt);
I am using a gridview with sqldatasource. How to get back the datasource in the codebehind as a datatable?
Use System.Data.DataTable dt = (System.Data.DataTable)gview.DataSource; if you are binding a DataTable.
You can even extract the DataTable out of DataSet if you are binding DataSet as
System.Data.DataTable dt2 = (System.Data.DataTable)((System.Data.DataSet)gvValidDA.DataSource).Tables[0]; you will have to check the index of your table or table name as you prefer.
Happy coding.
Edited
Use SqlDataSource.Select Method and assign it to a dataview
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
Then extract the datatable using
DataTable dt = (DataTable)dv.ToTable();
The actual data is not stored in the Gridview properties as the above answers pertain.
Using direct casting to the Gridview.DataSource is not enough, as it's actually NULL when the gridview has rendered!
You have to either reload the data directly from the SQL datasource...
DataSourceSelectArguments dss = new DataSourceSelectArguments();
DataView dvS = sdsADDorREMstudentData.Select(dss) as DataView;
DataTable dtS = dvS.ToTable() as DataTable;
if (dtS != null)
{
... etc...
}
Or you can use the ViewState to retain the data before the Gridview has rendered.
DataView dvClasses = (DataView)sdsClasses.Select(DataSourceSelectArguments.Empty);
gvStudents.DataSourceID = "sdsClasses";
gvStudents.DataSource = null; // Null out the source, as we have a SourceID instead
gvStudents.DataBind();
//save the data in a viewstate for later use (to control adding and removing students, without doing a postback! See ADD & REM methods below)
DataView dv = (DataView)dvClasses;
DataTable dt = new DataTable();
if (dv != null)
{
dt = dv.ToTable();
ViewState["gv"] = dt;
}
And use the ViewState to turn it back into a DataTable, when you need to use the data AFTER the Gridview has rendered...
DataTable dt = (DataTable)ViewState["gv"];