Using SELECT DISTINCT on an already created DataTable object? - asp.net

I have an already created DataTable object which i am using for my girdview (asp.net) i also need to bind a column of that object to a DropDownList. The datatable has the correct details in the column but the column contains more that 1 of the same name in the column - hence I would love to just do some kind of SELECT DISTINCT on the datatable and copy it to a new datatable for use with binding the dropdown.
This would allow me to save resources by making another trip to the database.
Here is an example, the current datatable has a column called items and in this column has the following entries
1
1
1
1
5
5
6
And of course i need only unique items for binding to my dropdown, hence i need the following data
1
5
6
Of course i don't want to change the original datatable object but rather make a copy of it will the new details
Any ideas if this is possible ? Or do i need to make another trip to the db?
thanks in advance

DataTable dt = new DataTable();
dt = dsMobileInfo.Tables[0].DefaultView.ToTable(true, "ColumnName");
//Applying the dvResult data set to the Grid
for(int i=0;i
Hope This will work for you.

You should use ToTable() on your default view (or any other view you are using) and provide true, to indicate you want distinct records:
DataTable distinctTable = originalTable.DefaultView.ToTable(true);

Related

DataTable's Default View is different from Actual Table Data

I'm using a huge datatable for a survey analysis. Instead of calling SQL server frequently am using row filtering on this datatable using DataView.
But the problem is once I filter the DataTable by
dv= dt.defaultview
dv.rowfilter="id=10"
that works fine. Dv having a filtered rows
But when we assign the same dt to another view, the Main datatables Default view being changed based on the filteration done for DV which is ID=10
So in the immediate window I checked this
dt.defaultview.totable().rows.count
and its 20 (WHich was because of the filteration done for DV)
but
dt.rows.count
is 1500 which is the correct count.
But my question is how this happened since I didnt touched the Datatable and I assigned the filter for DV only. Even then how the defaultview of the datatable changed?
"Even then how the defaultview of the datatable changed?" Isn't that obvious if you look at the first two lines of code you have posted?
dv = dt.DefaultView
dv.rowfilter = "id=10"
If you want to remove that filter on the DefaultView you have to assign empty string or Nothing:
dt.DefaultView.RowFilter = ""

asp.net how to store more than 2 columns from database in checkboxlist

When I connect some data source to checkboxlist I can define attributes:
DataTextField="column1" DataValueField="column2"
Then I can use in cs file .Text and .Value Properties respectively.
Columns 1 and 2 are taken from some sql select instruction.
Is there any way to store more than that 2 columns from database.
Something like CustomDataField="column3" ?
Thanks
As per the builtin properies of DropDownList there is no way to store more than 2 columns from database.The properties like DataTextField,DataValueField of DDl are defined in ListControl class of System.Web.dll.

how to update specific table inside dataset in asp.net

i have a dataset which contains three tables dt1, dt2, and dt3 (in the sequence), now i am maintaining the viewstate for every table, to retain the table across postback, (i.e i have latest DataTable inside viewstate), now the problem is i want to apply ViewState of dt3 to the respective table in DataSet, i.e dt3, i am doing below:
dataSet.Tables[(it will be dynamically fetched, say for here its) 0] = ViewState["DataTable" + (it will be dynamically fetched, say for here its) 0]
but here i am getting error saying dataSet.Tables[] is read-only.
can any one help how shall i go about at. Let me know if anyone needs more information about my question.
You should use
DataSet ds = new DataSet();
ds.Tables.Add(YourDataTable);
OR
ds.Tables.AddRange(YourDataTableArray);
To update an existing table in ds from ViewState you can try two approaches.
Remove Table from DataSet by using DataSet.Tables.Remove("Table"); method, Note that you should not rely on index approach to fetch Tables from DataSet instead you should provide your Tables a unique name so that you can fetch and remove them when you want to.
You can use loop approach to fetch rows from your ViewState DataTable and then insert them into DataSet's Table one by one, Something like this.
ds.Tables[0].Rows.Clear();
DataTable dtFromViewState = ViewState["dt"] as DataTable;
foreach (DataRow row in dtFromViewState.Rows)
ds.Tables[0].Rows.Add(row.ItemArray);

How can I store and retrieve data from a checkboxlist?

SQL Tables
Listing
ID, Title.....
ListingType
ID, Name
ListingMatrix
ListingID, ListingTypeID
Basically a listing can be more than 1 type and I want that to be able to be shown using the ListingMatrix table. However, I'm having a lot of issues populating the checkboxlist because I have it being sorted by Title to keep it user friendly. I'm using VB.Net, LINQ and MS SQL.
Dim readListingMatrix = (From ListingCategories In db.ListingTypeMatrixes _
Where ListingCategories.ListingID = ListingID)
For Each row In readListingMatrix
CheckBoxListListingCategories.Items(row.ListingTypeID - 1).Selected = True
Next
My issue is storing the checklistbox and editing it. Storing I think I could hack, but editing it is becoming a pain since I can't get the checkboxlist to check the correct boxes since their location changes due to the ORDER BY in my SQL Statement that populates the list.
Assuming that the value field of your checkboxes is filled with a ListingTypeID, do this:
Dim readListingMatrix = (From ListingCategories In db.ListingTypeMatrixes _
Where ListingCategories.ListingID = ListingID)
For Each row In readListingMatrix
CheckBoxListListingCategories.Items.FindByValue(row.ListingTypeID).Selected = True
Next

Sort a GridView Column related to other Table

i have a GridView bound to a DataView.
Some columns in the DataView's table are foreignkeys to related tables(f.e. Customer). I want to enable sorting for these columns too, but all i can do is sorting the foreignkey(fiCustomer) and not the CustomerName.
I have tried this without success(" Cannot find column ERP_Customer.CustomerName "):
<asp:TemplateField HeaderText="Customer" SortExpression="ERP_Customer.CustomerName" >
A tried also the DataViewManager, but i've a problem to detect the table to sort:
Dim daCharge As New ERPModel.dsERPTableAdapters.ERP_ChargeTableAdapter
daCharge.Fill(dsERP.ERP_Charge)
Dim viewManager As New DataViewManager(Me.dsERP)
viewManager.DataViewSettings(dsERP.ERP_Charge).RowFilter = filter
viewManager.DataViewSettings(dsERP.ERP_Charge).Sort = sort 'sort is the GridView's SortExpression
Me.GrdCharge.DataSource = viewManager.CreateDataView(dsERP.ERP_Charge)
I have to apply the sort on a distinct table of the DataViewManager, but this table would differ on the related tables.
I have bound the TemplateColumns in Codebehind in RowDataBound-Event f.e.:
Dim LblCustomer As Label = DirectCast(e.Row.FindControl("LblCustomer"), Label)
LblCustomer.Text = drCharge.ERP_CustomerRow.CustomerName 'drCharge inherits DataRow
What is the recommended way to sort a GridView on columns related to other tables? I could build a custom datatable with the customername instead of the foreignkey and bind this column to the TemplateField. But then my huge dataset in the model makes no sense anymore.
EDIT:
It seems that my question was not clear or too special.
Perhaps i can rephrase it in a more general term.
I have a model with a Dataset. I'm binding one Datatable(ERP_Charge) from it to my GridView(actually i take a Dataview from that Table). In this Datatable are columns that are related to other Datatables in the Dataset(relations are defined). When i want to make the grid sortable its no problem on the columns that belong to ERP_Charge. But the columns with foreign keys to other table could not be sorted because the Gridview shows f.e. not the CustomerID but the Customername. I get the Customername in RowDataBound.
Normally i would join the tables and add a Datacolumn for the Customername. Then i set this "virtual" Datatable as Datasource from the Gridview and i'm able to sort it. But i didnt want to create the datatables in the Page on the fly(it belongs into the model). Do i have to define it in the dataset-Designer? I thought it would be sufficient to define the relationships in the dataset.
UPDATE: i had solved my sorting problems as below.
Regards
UPDATE: i had solved my sorting problems in the following way: I changed my DataSet in the model and joined the related table to the Main-Table(ERP_Charge). I added the columns to the Datatable(f.e. CustomerName) that i want to sort. But i did that without saving it in the configuration wizard of the DataAdapter, because that would delete all Update-,Delete- and InsertCommands. Hence i changed only the SelectCommand with the one that was generated from the Wizard. Therefore i kept the benefit of my Datamodel with the ability to sort for columns of related tables. Maybe queries are a little slower now because of the joins but it works for me.

Resources