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)
Related
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.
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()
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'
I have a stored procedure which updates a database using the parameters I supply but I'm having trouble passing a NULL to the stored procedure
The field I need to make NULL is a DateTime field
DB.Parameters.AddWithValue("#date", NULL)
This gives me the error
'NULL' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead
So I tried
DB.Parameters.AddWithValue("#date", DBNull.Value.ToString())
But this produces the value 1900-01-01 00:00:00.000 in the column as it's passing a "" to the field
I also tried
DB.Parameters.AddWithValue("#date", DBNull.Value)
But it produces this error
Value of type 'System.DBNull' cannot be converted to 'String'.
Has anybody got any ideas?
Or you can add your parameter like this, which gives it a null value in the database if your variable is null:
DB.Parameters.AddWithValue("#date", myDateTime ?? (object)DBNull.Value);
you need to set it as a nullable type as Amit mentioned.
More details and background available at http://evonet.com.au/overview-of-c-nullable-types/
Try something like this, using Add rather than AddWithValue:
DB.Parameters.Add("#date", SqlDbType.DateTime).Value = DBNull.Value;
Try this
If you are using class and its property and those property values are used as parameter then you can do this
chnage the signature of the property to
public DateTime? myDateTime = null;// Here ? is used to make the property nullable.
Also if your date column is of type varchar then correct it to Date (Sql2008) / DateTime(2005).
//change parameter to this
#SomeDate datetime = null (as suggested by chris)
Allow the field to accept null and then pass the value as
DB.Parameters.Add("#date", DBNull.Value)
This is an example. It's work for me
if(obj.myDate == DateTime.MinValue)
{
aCommand.Parameters.Add("dateParameter", SqlDbType.Date).Value = DBNull.Value;
}
else
{
aCommand.Parameters.Add("dateParameter", SqlDbType.Date).Value = obj.myDate ;
}
I have a dataset and it has a InsertQuery(String Name,String Surname,DateTime BDate)
Now I can wirte code like this,
_t.InsertQuer("Alper","AYDIN", null);
it can record data OK,
But I want to do like this,
_t.InsertQuery("Alper","AYDIN", dtBDate.IsEmpty==true?null:dtBDate.Value);
But when I Depoly it is give error like this;
Type of conditional expression cannot be determined because there is no implicit conversion between '' and 'System.DateTime'
How Can I set null ?
The conditional operator needs to be able to return a single data type. Cast the null value to the null version of the other type:
_t.InsertQuery("Alper","AYDIN", dtBDate.IsEmpty?(DateTime?)null:dtBDate.Value);
Have you tried like this:
_t.InsertQuery("Alper","AYDIN", dtBDate);
where dtBDate is Nullable<DateTime>.
Also notice that you cannot pass null if the InsertQuery method takes DateTime instead of Nullable<DateTime> as last parameter.