sephtian
sephtian

Reputation: 455

Edit value in datatable variable in asp.net(vb)

I have a variable that contains a datatable, and I have condition if the value is 0 which I want to change into a "No" string.

Here is my original code

dt = BrowseServiceTypeMaster(ServiceTypeName)
lvServiceType.DataSource = dt

For i As Integer = 0 To dt.Rows.Count - 1
    If dt(i)("exception").ToString = "0" Then
        dt(i)("exception") = "No"
    End If
Next

and here's what I've changed it to:

For i As Integer = 0 To dt.Rows.Count - 1
    If dt(i)("exception").ToString = "0" Then
        dt(i)("exception").Fill = "No"
    End If
Next

It still gives me an error, can anybody help me?

Upvotes: 1

Views: 895

Answers (1)

Jay Riggs
Jay Riggs

Reputation: 53595

If you only want to conditionally change the value of a field in row in a DataTable then use something like this:

For Each row As DataRow In dt.Rows
    If row("exception") = "0" Then
        row("exception") = "No"
    End If
Next

Edit

I'm afraid what you're trying to do is not possible. The exception you show in your comment tells why:

Input string was not in a correct format. Couldn't store in exception Column. Expected type is Int64.

This exception is telling you that you're trying to store a String value in a DataTable column that is set to accept Int64's (Long). You can't do that.

There are a number of controls you can use that offer customizable templates that allow you to specify at runtime what text to display to the user based on the value bound to an element of that control.

For example if you were binding your DataTable to a ListView you would bind your 'exception' column to an ItemTemplate and use code like this to display 'No' when a record's value was 0 and display the field's value in all other cases:

<%#IIf(Eval("exception") = 0, "No", Eval("exception").ToString())%>

Upvotes: 2

Related Questions