Returns value of checkbox - asp.net

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'

Related

error when tried to delete empty rows from datatable

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()

Database result is passed as null unless it's read

Set rslistings = my_conn.Execute(strSQL)
Do while NOT rslistings.Eof
description = strip(rslistings("description"))
rslistings.MoveNext
loop
In strip - NULL is being passed. However, if I attach a debugger and inspect the contents of rslistings("description"), then the actual Field object is passed through
It's quite old asp code, but it works on IIS6, just not IIS7
EDIT This only happens on the "description" field with is a text type (MySQL database)
strip doesn't do a lot:
If NOT IsNull(passedinvalue) Then
// do something
Else
// do something else
If I call strip like strip(rs("description")), it is never null as the Field object is passed in. If I assign it to another value, then pass it in (like strip(mynewvar)) then the correct value is passed in.
Edit - database bits as requested below
Set my_conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
my_conn.Open "DSN=mydb"
SQL
Set rs = my_conn.Execute("SELECT description FROM table")
the Field Collection is the default member of the Recordset object.
so is the value property for the Field object.
so the following two code statements are equivalent.
Debug.Print objRs.Fields.Item(0) ' Both statements print
Debug.Print objRs(0) ' the Value of Item(0).
it is a difference if you assign a value to a variable or use it as a parameter in a function.
#Paul: If strip doesn't check if description is NULL before working on it, you could do this --
Do while NOT rslistings.Eof
description = rslistings("description")
If NOT IsNull(description) Then
description = strip(description)
Else
description = "" ' or you could have description = " "
' if you output to screen later on
End If
rslistings.MoveNext
loop

Pass a NULL in a parameter to a DateTime field in a stored procedure

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 ;
}

Set Null DateTime in InsertQuery

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.

vb.net, option strict, enums and overriding ToString

I have the following code:
Public Enum Country
Canada = 1
USA = 2
End Enum
When I want to see if the user has selected a value, I do:
ddl.SelectedValue = Country.Canada
Which works fine. However, if I turn on a warning for implicit conversion, this gives a warning. Changing it to
ddl.SelectedValue = Country.Canada.ToString()
fails, since the ToString() method returns "Canada" not "1".
What's the best way to get rid of the warning?
You can explicitly cast the SelectedValue to an int, or the Country as a string.
If CInt(ddl.SelectedValue) = Country.Canada
or
If ddl.SelectedValue = CStr(Country.Canada)
If you take the first option, you might need to explicitly declare your enum as Integer
Public Enum Country As Integer
The warning occurs because SelectedValue is a string, and Country is an Integer, so an implicit conversion occurs - just like it says!!
If you want the value '1' rather than 'Canada', you can explicitly cast it as an integer first, and then call .ToString() on the result of that.
ddl.SelectedValue = DirectCast(Country.Canada, Integer).ToString()

Resources