Dataset Select Statement? In Asp.net - asp.net

In web application, I fetch the data from back end and the data is now availbale in dataset. I have 5 columns in that dataset, but i need only one column, so how can i get only one column out of 5 columns. I use dataview but it is not getting.
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter= "empid";
but i am not getting. can you helpme

With the help of dataview you can create another table which has only required columns which you need
DataView view = new DataView(ds.tables[0]);
DataTable newTable = view.ToTable(tabblename, false, params string[] columnNames
);//Add name of required
You can get full example from the below link
Click
or
You can remove extra columns
ds.Tables[string].Columns.Remove(string)
to remove individual columns which you feel are extra one.
Don't forget to ds.AcceptChanges() after deleting rows or they will still be in the dataset.

You can set the dv.AutoGenerateMembers to false then create on column inside yourDataView (using designer approach) that matches the names of that column you want to show on the DataView

Related

Merging two columns in a single collection

I'm having a table in Collection1 and it is having 8 columns in it. Now I want to merge Column1, Column3, Column5 and Column7. Is there any solution for those columns using blueprism
You simply need to copy the collection with a new column, then loop through each row to create the merged values...
Something like that:
Add Column Field9 contains the standard object Utility - Collection Manipulation, with page 'Append Field (Text)', as inputs I have [Coll1] for the input collection, "Field9" for the new Field Name and "" for the Value, as output I have Coll2.
The loop is on Coll2
Merge Columns is simply [Coll2.Field1]&[Coll2.Field3]&[Coll2.Field5]&[Coll2.Field7] with result being stored in Coll2.Field9.
Coll1 contains 8 columns, each column named Field1 through Field8. Coll2 is the new collection, it is empty before the merge.
You can choose to delete the columns you don't need afterwards, with the page 'Delete Field' from the same object (Utility - Collection Manipulation).

asp.net data table add row doesn't and no error returned in vb.net

I have a data table that is loaded from an excel spreadsheet, which works fine. I then want to remove the duplicates from the table, which I can also accomplish. Next I want to add new rows which is where the whole thing goes wonky. The code runs and no errors are returned. (I have Try/Catch statements in place and they never get triggered).
Here is the code that tries to add the new row to the data table.
newDRow = dtTemp.NewRow
newDRow("QTY") = Convert.ToInt32(dupList(1))
newDRow("GRATING-ID") = newMarkNum
newDRow("GRATING SPAN DECIMAL INCHES") = Convert.ToDecimal(dupList(2))
newDRow("GRATING WIDTH DECIMAL INCHES") = Convert.ToDecimal(dupList(3))
Try
dtTemp.Rows.Add(newDRow)
Catch ex As Exception
message = HttpUtility.JavaScriptStringEncode(ex.Message)
End Try
The data table is called dtTemp. I have put break points in and can verify that each of the new row columns are getting the correct data.
The original data table is being passed to this function as follows:
Private Function RemoveDuplicate(ByVal dtInput As DataTable) As DataTable
I have a line that copies the dtInput table to the dtTemp table like so:
Dim dtTemp As DataTable = dtInput.Copy()
I just can't seem to find out why the row insert is not working. When the function runs and the data grid view is populated I see the results of removing the duplicate lines but the new lines are not in the table.
Any ideas?
Update #1--------------
I checked the table contents after the inserts were supposed to have happened but they do not show up. there should be a line that says:
2|1121-G31|32.875|42
And a line that says
7|1185-G33|22|22
But they are not there.
So I figured out what stupid mistake I was doing.
I had my new data row statement OUTSIDE of the For loop that was adding the new rows to the data table.
Moved it inside the for loop and it works great now!

Adding an empty row to a specific row index of a datatable

I want to add an empty row in some place of my datatable. for example in row number five!
i searched and tried these codes but they didn't work. and the row was added at the first line of my gridview.
DataRow dr2 = dt.NewRow();
dt.Rows.InsertAt(dr2, 5);
and this:
dt.Rows.InsertAt(dt.NewRow(),5);
i use asp.net webforms. Regards
I think your approach is correct. This code will add a new row to row index 5. But it is adding row to first line of your gridview that's because your datatable may be do not have any other rows. If your datatable do not have at least 0-6 index rows it will not work.
The problem was about the sorting! i add this to my code after the sorting and fixed!
dt = dt.DefaultView.ToTable();
thanks for suggests!

Method to remove empty rows from DataTable?

I parsed through an Excel spreadsheet and returned the entire result as a DataTable. However, this Excel spreadsheet has several empty rows that I would like to eliminate from the resulting DataTable. In particular, each empty row begins with an empty cell. So, I know that the entire row is empty if the value of the cell at the first index is empty. Note that I cannot simply modify the Excel spreadsheet because I have to work with exactly what the client has sent to me. Based on this information, I assumed that I could perform the following function to remove empty rows:
' DataTable dt has already been populated with the data
For Each row As DataRow In dt.Rows
If dt.Rows.Item(0).ToString = "" Then
dt.Rows.Remove(row)
ElseIf dt.Rows.Item(0) Is Nothing Then
dt.Rows.Remove(row)
End If
Next
However, after crafting this solution, I am greeted with the following error:
Collection was modified; enumeration operation might not execute.
I now realize that I cannot alter the collection as I access it. How can I get around this? I'm wondering if I should create a new DataTable with the rows that aren't empty. Is this the best approach or are there better options?
EDIT: I have also tried iterating over the rows backwards:
For i = dt.Rows.Count() - 1 To 0
If dt.Rows.Item(i).Item(0).ToString = "" Then
dt.Rows.RemoveAt(i)
End If
Next
You can't modify the collection while you're enumerating it with For Each, but a simple For loop will work. You'll need to loop backwards to avoid skipping the row after a removed row.
You've also got your tests the wrong way round; if Item(0) returns Nothing, then Item(0).ToString will throw a NullReferenceException.
I'm assuming the dt.Rows.Item(0) is a typo, and should read row.Item(0) instead.
For i As Integer = dt.Rows.Count - 1 To 0 Step -1
Dim row As DataRow = dt.Rows(i)
If row.Item(0) Is Nothing Then
dt.Rows.Remove(row)
ElseIf row.Item(0).ToString = "" Then
dt.Rows.Remove(row)
End If
Next
Vb.Net using linq
Dtset.Tables(0).AsEnumerable().Where(Function(row) row.ItemArray.All(Function(field) field Is Nothing Or field Is DBNull.Value Or field.Equals(""))).ToList().ForEach(Sub(row) row.Delete())
Dtset.Tables(0).AcceptChanges()

Chart doesn't update when adding new rows to an existing Excel table (not without having to use named ranges)

I'm really new to the use of closedXMl and Excel too(at least for this purpose) so sorry if I'm asking silly questions.
I know that closedXML doesn't support charts yet so the only thing that came to mind to get around this was to create my chart using an excel table . That way I thought ClosedXML would update the ranges when I inserted new rows and the chart would pick up on it. Well , it didn't. At least not when I add the rows from code using the closedXML library.
What is curious is that adding new rows from inside excel automatically updates the chart but if I want to get that same result from code, I have to use OFFSET formula along with named ranges and then set the chart source data to these named ranges.
That's why I'd like to know if if there is anything wrong with the code I use to insert new rows:
Dim ruta As String = Server.MapPath("~/Templates/MyTemplate.xlsx")
Dim wb As New XLWorkbook(ruta)
Dim ws = wb.Worksheet(1)
Dim tblData = ws.Table("Table1")
Dim year As Integer = 2000
For i As Integer = 1 To 13
With tblData.DataRange.LastRow()
.Field("Year").SetValue(year)
.Field("Sales").SetValue(CInt(Math.Floor((2000 - 500 + 1) * Rnd())) + 500)
End With
tblData.DataRange.InsertRowsBelow(1)
year = year + 1
Next
tblData.LastRow.Delete()
As you can see the code is very simple and so is the template , that consists of only two columns : "Year"(table1[Year]) and "Sales"(Table1[Sales]
I don't think this has anything to do with my template because as I told you, adding new rows directly from excel works as expected and it is only when I generate the table from code that the chart series doesn't include the new row that were added
Being necessary to manually add the new ranges(Sheet1!Table1[Sales] and Sheet1!Table1[Year]) as it only includes the first row(the one added by default when you insert a table)
Any help would be much appreciated
P.S. Here is a link to a rar containing the full code as well as the excel template(\Templates\MyTemplate.xlsx)
If the problem is that your table doesn't recognise the additional rows, try adding this after the last row delete:
tblData.Resize tblData.Range(1, 1).CurrentRegion
That should resize the table. Then hopefully your table operations should work.

Resources