error when tried to delete empty rows from datatable - asp.net

I tried this LINQ code to delete empty rows from datatable
NewDt = NewDt.Rows.Cast(Of DataRow)().
Where(Function(row) Not row.ItemArray.All(Function(field) TypeOf field Is System.DBNull OrElse String.Compare(TryCast(field, String).Trim(), String.Empty) = 0)).CopyToDataTable()
But it showing error
Lambda parameter 'row' hides a variable in an enclosing block, a previously defined range variable, or an implicitly declared variable in a query expression.
Any idea?

Yes - read the error message. You've already got a variable called row in your method, so you need to choose a different name for the parameter in the lambda expression. For example:
NewDt = NewDt.Rows.Cast(Of DataRow)().
Where(Function(x) Not x.ItemArray.All(
Function(field) TypeOf field Is System.DBNull _
OrElse TryCast(field, String).Trim() = "").
CopyToDataTable()

Related

How to have a value of NULL for DateTime?

In my SQL database, I've declared DpsDate and AdmDate as DATE, also I've set them to allow nulls. When I run my application and leave these columns blank, I get this error:
Failed to convert parameter value from a String to a DateTime.
This is where I'm a bit confused because I've set them to allow nulls so shouldn't the database accept no value? The application works if I set both dates to "01/01/1900". I've tried setting them to "00/00/0000" but I get the same error.
Here's what I have:
If tbNotifyDate.Text = "" Then
cmd.Parameters.Add("#DpsDate", SqlDbType.Date, 50).Value = "01/01/1900"
Else
cmd.Parameters.Add("#DpsDate", SqlDbType.Date, 50).Value = tbNotifyDate.Text
End If
If tbAdmDate.Text = "" Then
cmd.Parameters.Add("#AdmDate", SqlDbType.Date, 50).Value = "01/01/1900"
Else
cmd.Parameters.Add("#AdmDate", SqlDbType.Date, 50).Value = tbAdmDate.Text
End If
You need to use DBNull.Value to represent NULL in ADO.NET. Things like table adapters and Entity Framework, which are built on top of ADO.NET, can support nullable value types and thus use Nothing to represent NULL but ADO.NET itself predates nullable value types, so Microsoft had to invent a type specifically for the purpose of representing NULL.
I would suggest using the If operator to make the code more concise:
Dim value As Date
cmd.Parameters.Add("#AdmDate", SqlDbType.Date).Value = If(Date.TryParse(tbAdmDate.Text, value),
value,
CObj(DBNull.Value))
The CObj is required because the two possible results of If must be the same type and Date and DBNull are not the same type. By casting one possible result as type Object, they are both interpreted as type Object and the compiler is happy.
Note that, as I have written that example, this will save NULL if the TextBox contains anything that isn't a valid representation of a Date. You can use whatever validation is appropriate in your specific case or, if you've already validated, just check for an empty TextBox and use CDate.

Option Strict disallows late binding using IEnumerable

Dim distinctJoints As IEnumerable
distinctJoints = From row In spotsTable.AsEnumerable()
Group row By Key = row("FUNCJOINTCODE") Into Group
Select Key, Group
_evaluatedJointsCount = (From row In spotsTable.AsEnumerable()
Group row By Key = row("FUNCJOINTCODE") Into Group
Select Key, Group).Count()
'Process each joint
For Each currentJoint In distinctJoints
Dim currentJointKey As String = currentJoint.Key
For the above code currentJoint.Key is showing error of late binding after option strict is on.
Could you please help me out of this.
First, let me congratulate your for moving your code towards Option Strict On! It might be some work in the beginning, but it pays off in the long run since a lot of errors will be found at compile-time rather than at run-time.
That said, let's look at your problem. Here:
Dim distinctJoints As IEnumerable
you declare distinctJoints as a non-generic IEnumerable. A non-generic IEnumerable returns items of type Object when iterated over. The type Object does not contain a Key method. This is why you get a compile-time error.
Since your LINQ query returns a generic IEnumerable of an anonymous type, the solution is to use type inference instead. Activate Option Infer On (if you have not already done so) in your project properties and let the compiler infer the correct data type:
' Dim distinctJoints As IEnumerable <-- remove this
Dim distinctJoints = From row In spotsTable.AsEnumerable()
Group row By Key = row("FUNCJOINTCODE") Into Group
Select Key, Group

Returns value of checkbox

In ASP.net is the following code right ?
Dim r1 As Bollean = rd1.checked
checkbox returns what type of value?
then when I put this following code---
If Request.QueryString("r3") Then
myReportDocument.Load(Server.MapPath("Gradewise.rpt"))
End If'
It gives following error--
Conversion from string "" to type 'Boolean' is not valid.
.checked property returns a boolean value.
And regarding error that you are getting - you are trying to put a string in a if statement while a condition which returns either true or false is expected.
So it should be
'If Request.QueryString("r3")="some string to compare" Then ...
as Request.QueryString("r3") returns a String!
You need to use Checked property of checkbox to get the checked status of checkbox. rd1.checkbox would return object of type CheckBox
Dim r1 As Bollean = rd1.checkbox.Checked
Edit as OP is edited. You have to give expression that results in boolean in if statement.
If Request.QueryString("r3") == "somevalue" Then
myReportDocument.Load(Server.MapPath("Gradewise.rpt"))
End If'

ASP.NET Error with session key strings

Hey I am trying to convert the following code to VB.NET from this webpage
http://msdn.microsoft.com/en-us/magazine/cc163730.aspx
And have a method converted as such
' Get the order from the session on demand
Private Shared Function GetOrderFromSession(ByVal i As Integer) As ShoppingCartOrder
Dim session As HttpSessionState = HttpContext.Current.Session
Dim ID As Integer = 0
Dim quantity As Integer = 0
' note: For simplicity session key strings are dynamically
' created——for performance reasons they should be precreated.
ID = CInt(session(ShoppingCartItemIDKeyBase + i))
quantity = CInt(session(ShoppingCartOrderQuantityKeyBase + i))
Dim item As ShoppingCartItem = ShoppingCartItem.GetItem(ID)
Return New ShoppingCartOrder(item, quantity)
End Function
But getting the error around the lines
ID = CInt(session(ShoppingCartItemIDKeyBase + i))
quantity = CInt(session(ShoppingCartOrderQuantityKeyBase + i))
Error 1 Overload resolution failed because no accessible 'Item' can be called without a narrowing conversion:
'Public Default Property Item(index As Integer) As Object': Argument matching parameter 'index' narrows from 'Double' to 'Integer'.
'Public Default Property Item(name As String) As Object': Argument matching parameter 'name' narrows from 'Double' to 'String'.
The + operator can be used for string concatenation as well as the & operator in VB.NET, but the ampersand is the preferred operator.
Just to be sure, can you switch them to ampersands and see if the error still occurs? ShoppingCarItemIDKeyBase is a string, and I'm not sure if the + operator forces the strings to convert to numbers since the user has the option of using &.
ID = CInt(session(ShoppingCartItemIDKeyBase & i))
quantity = CInt(session(ShoppingCartOrderQuantityKeyBase & i))
One or both of these variables is a decimal type rather than an integer:
ShoppingCartItemIDKeyBase
ShoppingCartOrderQuantityKeyBase

check for null with HtmlAgilityPack

How do you check for null with HtmlAgilityPack? I'm getting "Property or indexer 'HtmlAgilityPack.HtmlNode.HasChildNodes' cannot be assigned to -- it is read only" with the following.
if (Node.Element("TD").HasChildNodes = DBNull.Value)
I"m getting "Object reference not set to an instance of an object. " with
if (Node.Element("TD").HasChildNodes)
First, the = operator is the assignment operator, not the comparison operator (==). In your first example you are trying to assign DBNull.Value to HasChildeNodes, a read-only property, not compare it to a value.
Second, you don't test against DBNull.Value, but against null. DBNull.Value is to be used when testing values of items returned by the database using ADO.NET. For all other cases, you should use null.
So, the test should be:
if (Node.Element("TD").HasChildNodes == null)

Resources