IsNumeric function extremly slow - asp.net

Have gridview that I have populate from a database, and now I would like right align all the numeric items in the gridview, but If IsNumeric(row.Cells(i).Text) statment takes extremely long time is there any other way to fix this?
Code:
Takes long time any other way align number to the right
For Each row As GridViewRow In Me.gwResult.Rows
For i As Integer = 0 To headCell - 1
If IsNumeric(row.Cells(i).Text) Then
row.Cells(i).HorizontalAlign = HorizontalAlign.Right
End If
Next
Next

You are using this code to identify the numeric values after binding DataSource, which will add extra time to analyse the grid data. Try using the same code on RowDataBound event of the gridview.

Your best option would be to find out which columns are numeric before you bind the grid to the datasource. In the RowDataBound event of the GridView you can then just check if the current column being databound is in the numericColumns collection or not.
'Find numeric properties of the type beehing databound
Dim numericColumns = New HashSet(GetType(MyDataType).GetProperties() _
.Where(Function(x) IsNumericType(x.PropertyType)))
.Select(Function(x) x.Name))
'And for DataTable it would look like this
Dim numericColumns = New HashSet(dt.Columns.Cast(Of DataColumn)() _
.Where(Function(x) IsNumericType(x.DataType))) _
.Select(Function(x) x.ColumnName))
Private Shared Function IsNumericType(dataType As Type) As Boolean
Dim code = CInt(Type.GetTypeCode(dataType ))
Return code >= 4 AndAlso code <= 15
End Function

Related

How to avoid exception There is no row at position in VB.NET

I have this condition:
If IsNothing(ds.Tables(0).Rows(rowData).Item("DATE")) Then
Else
txtDATE.Text = ds.Tables(0).Rows(rowData).Item("DATE")
End If
What ever I do it is the same error.
How can I resolve this?
I have a loop that is going until 10 but my data set can sometimes has
less than 10 rows. I want to create TextBox control dinamically and to
set it text from data set but if for 10 row data set doesn't have data
just to leave text of the TextBox empty
Then use an If to check that condition:
For rowData As Int32 = 0 To 10
' .....
If ds.Tables(0).Rows.Count > rowData Then
Dim dt As Date? = ds.Tables(0).Rows(rowData).Field(Of Date?)("DATE")
txtDATE.Text = If(dt.HasValue, dt.Value.ToString(), "")
Else
txtDATE.Text = ""
End If
Next
I think the column is a nullable date-column(because you're using IsNothing). Then i'd prefer using a Date?, therefore you can use DataRowExtensions.Field which supports nullable types.
Instead of 10 textboxes you can use only one DataGridView control.
Add one column which represent your Date value (this can be done in designer too)
Dim column As New DataGridViewTextBoxColumn With
{
Header = "Date"
DataPropertyName = "DATE"
}
yourDataGridView.Columns.Add(column)
yourDataGridView.AutoGenerateColumns = False
Then use your DataTable as DataSource
yourDataGridView.DataSource = ds.Tables(0)
DataGridView will generate rows only for existed data in DataTable.
You can customize "outfit" of DataGridView your needs.

ASP VB NET List(of T) find method failing in a for each loop

Good morning stackers!
I'm designing a massive update page. Here are the general steps:
I have a class called item which has two properties: Equipment number and new due date.
I have a textbox where i paste values from Excel, the values consist of two columns divided by a vbtab character: the columns are an equipment number and a new due date
A button is clicked and the values from textbox are parsed into a list(of item) and the equipment master builds a string for an SQL criteria for a commandtext.
The command fills a dataset from a database which gets equipment number and current due date.
I add manually a column to the dataset (new due date)
I iterate over the rows of the dataset, and i use the list(of item) find method matching equipment from the list and from the database to get a new due date from the textbox parsed values.
Everything is going well, except that when using the find method for more than 1 row in the dataset, the method fails:
Here is the code from point 5 and 6:
da.Fill(ds, "Equipments")
dt = ds.Tables(0)
ds.Tables(0).Columns.Add("column_1", Type.GetType("System.DateTime"))
Dim rw As DataRow
For Each rw In ds.Tables(0).Rows
Dim strsearch As String = rw(0).ToString
Dim fequnumb As item = myItemList.Find(Function(p) p.EquipNumber = strsearch)
rw(2) = fequnumb.DueDate <- Error occurs here
Next
Again, if instead of ftechid.DueDate I put a static value like Today()the code runs fine for the loop and fills correctly the gridview, but if i leave the ftechid.DueDate then an error is thrown after the first row:
Object reference not set to an instance of an object.
Any help is very much appreciated as to how to use the find method inside a for..each loop
If the Find function cannot match the string requested it returns Nothing and then you cannot set the due date from a variable that is Nothing. If this is a condition expected from the input then you need to protect the assignment to the new column with something like this.
For Each rw In ds.Tables(0).Rows
Dim strsearch As String = rw(0).ToString
Dim fequnumb As item = myItemList.FirstOrDefault(Function(p) p.EquipNumber = strsearch)
if fequnumb IsNot Nothing Then
rw(2) = fequnumb.DueDate <- Error occurs here
End if
Next
If this is not supposed to happen then you need to check your inputs

Insert separator row in Gridview

I'm new to ASP.NET VB, and i'm having trouble and dont really understand the code of putting in row seperators in a gridview. I have 5 columns and the fifth columns rows field text is what i would like inserted into the new seperator row. There may be more than one per question. To populate my gridview i'm calling upon a stored procedure "Select_Problems" and carrying a persons name from another page through "SvarRecord" and matching it up with the SP #Name. The fifth column in the SP has four possibilities "1. Person upset?", "2. Is Person is resisting?", and so on. My code is as follows and i would like to have an easy way to do this but to make it look nice along with a color to divide each question. Thank you
Public Sub LoadGrid2()
Dim MyConn As New SqlConnection((ConfigurationManager.ConnectionStrings("Customer_Service").ConnectionString))
Dim MyCmd As New SqlDataAdapter("Select_Problems", MyConn)
MyCmd.SelectCommand.CommandType = CommandType.StoredProcedure
MyCmd.SelectCommand.Parameters.Add(New SqlParameter("#Name", SqlDbType.VarChar))
MyCmd.SelectCommand.Parameters("#Name").Value = Session("SvarRecord")
Dim ds As New DataSet
MyConn.Open()
MyCmd.Fill(ds, "Overview")
GridView2.DataSource = ds
GridView2.DataBind()
End Sub
I know your supposed to do this in the Rowdatabound of the Gridview but I dont understand the code behind it to do this which would be great to do.
In the RowDataBound event, you could check the index of the row currently being bound and determine if it is the last row in a set of 5 and then apply styling to the row to give a visual cue of separation without actually generating a blank separator row, like this:
Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)
' Add a visual separator via CSS border property to every 5th row
If e.Row.RowIndex Mod 5 = 0 Then
' Apply style to each cell in the row
For Each cell As TableCell In e.Row.Cells
cell.Attributes.Add("Style", "border-bottom: red 5px solid")
Next
End If
End Sub
Note: My math might be off by one so you may need to change the styling to the top of the row (border-top instead of border-bottom), but the idea is the same.

Get cell value from specified column in a Gridview upon "RowUpdating" Event ASP.NET/VB

What i want to do is te following... i have a gridview that loads the data from a SQL server, i have the e.NewValue and e.oldValue to determine which data was being modified/updated but i need to get the unique record ID which is store in "ID_ControlCharts" column
So, can you provide a code snippet/brief explanation in how to get the value from "ID_controlCharts" already filled by the gridview when i updated 1 cell (e.new/old)???
Note: The index is not what i needed since the row can be deleted and the reference to that row is now lost, so since the "ID_controlCharts" is filled automatically by SQL server key, its perfect for my situation.
codebehind
If e.Equals("Control_SeqRef_CH") = False Then
Dim oldvalue As String = e.OldValues("Control_SeqRef_CH")
Dim newvalue As String = e.NewValues("Control_SeqRef_CH")
Dim rowindex As Integer = e.RowIndex
Dim IDfromcontrol as integer = ???
MsgBox(oldvalue)
MsgBox(newvalue)
MsgBox(rowindex)
Msgbox(IDfromcontrol)
Else
MsgBox("same")
End If
You have a couple of options:
If the ID_ControlCharts column is in the record, couldn't you fetch it from e.OldValues?
Dim IDfromcontrol = e.OldValues("ID_ControlCharts")
Failing that, if you set ID_ControlCharts as a column for the DataKeyNames property, you should be able to fetch it using the ItemIndex:
Dim IDfromcontrol = GridView1.DataKeys(e.ItemIndex).Values(0)
(This is all from memory, so the syntax may be a little off.)

Search data from dataset

I want to search record based on following fields
P_num ,P_name, P_dob,P_code
I am using more than 4 tables to retrive data,
I have created dataset called P_search and i want to fill data into dataset and thsn display it according to which field is selected for search purpose.
Suggest me to write code by creating own function in which all fields and a boolean variable will be taken as parameters
Please help me to write code using VB.net.
I am very new to VB.net. Is ther anyone who can help me giving above code.
Thank You In advance
You use DataTable.Select Method
Private Sub GetRowsByFilter()
Dim table As DataTable = DataSet1.Tables("Orders")
' Presuming the DataTable has a column named Date.
Dim expression As String
expression = "Date > #1/1/00#"
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression)
Dim i As Integer
' Print column 0 of each returned row.
For i = 0 to foundRows.GetUpperBound(0)
Console.WriteLine(foundRows(i)(0))
Next i
End Sub

Resources