In ASP.Net 4.0, I'm trying to utilise the DevExpress ASPxCheckBox's ability to have "three states": checked, unchecked and indeterminate. I'd like to set this based on the value of a boolean: "Checked" when the boolean is true, "Unchecked" when false, and "Indeterminate" when null. To this end, I'm trying to use the following code in my page:
<dx:ASPxCheckBox ID="cb_located" runat="server" AllowGrayed="true" AllowGrayedByClick="true" CheckState='<%# Eval("located") == DBNull.Value ? "Indeterminate" : (bool)Eval("PropertyLocated") == true ? "Checked" : "Unchecked" %>' />
However, this gives me the error
CS0030: Cannot convert type 'string' to
'DevExpress.Web.ASPxClasses.CheckState'
Can anyone help me with the correct syntax please?
You need to use CheckState enumeration. So instead of string value "Indeterminate" use CheckState.Indeterminate etc.
Related
i have column in database with Int data type, but textbox don't allows null. it gives error "Input string was not in a correct format".
objinsert.VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
Use TryParse instead.
int GrpID = 0;
int.TryParse(txtVehGroupID.Text, out GrpID)
if(GrpID > 0)
objinsert.VehGrpID = GrpID;
You can just special-case out empty values. I am, of course, assuming here that an empty value should map to null.
objinsert.VehGrpID = string.IsNullOrWhiteSpace(txtVehGroupID.Text) ? null : (int?)Convert.ToInt32(txtVehGroupID.Text);
You'll also want to adjust your code to set the textbox's text accordingly.
Your question is also a bit unclear, because you say the textbox won't allow nulls, but you haven't shown us any code that adjusts the textbox text.
You can also use DBNull.Value Field. If a database field has missing data, you can use the DBNull.Value property.
check this link http://msdn.microsoft.com/en-us/library/system.dbnull.value%28v=vs.110%29.aspx
try this
int VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
OR
int VehGrpIDtxtVehGroupID.Text = int.Parse(txtVehGroupID.Text);
The Text property of your textbox is a String type, so you have to perform the conversion in the code.
I am trying to limit the characters displayed in ASP.NET Label controls that have their Text values databound to a SQL Database. If the field in the database is not NULL, I do not have any trouble. If they are NULL, I get the exception "Object reference not set to an instance of an object."
My code is as follows:
<asp:Label ID="LabelDescriptionLabel" runat="server" Text='<%# IIf(Not IsDBNull(Item.LabelDescription), IIf(Item.LabelDescription.ToString.Length > 30, Left(Item.LabelDescription, 30) & "...", Item.LabelDescription), Item.LabelDescription)%>' />
I have also tried the following, without any luck:
<asp:Label ID="LabelDescriptionLabel" runat="server" Text='<%# IIf(Not IsNothing(Item.LabelDescription), IIf(Item.LabelDescription.ToString.Length > 30, Left(Item.LabelDescription, 30) & "...", Item.LabelDescription), Item.LabelDescription)%>' />
I am using ASP.NET with VB.NET. I have tried using the CSS "text-overflow: ellipsis" method as well, but I had some table formatting issues that I have not resolved.
Is there a way to handle these NULL values and prevent run-time exceptions?
Thanks!
One problem you are having is that the old IIF is a function which evaluates all the parts before it decides what to return.
IIF(Not IsDBNull(Item.LabelDescription), TruePart, FalsePart)
It does not matter if the expression is true or not, the TruePart and
FalsePart statements will always be executed. Since they include references to something which may be DBNull/Nothing/null, you get an error.
Something else though is that Nothing is not the same as DBNull, and if Item is an Object which can be Nothing/null, then testing the description of Nothing/null will be problematic.
The If Operator has been available since abut 2008. The structure is the same but it only evaluates the test condition and the True or False expression which applies:
If( Condition, TruePart, FalsePart )
The "short circuiting" prevents the expression/Part which does not apply from being evaluated. As long as the rest of the code is valid, you should be able to just clip one of the Is from the IIF functions.
Since you want to the object Nothing I would use the Nothing keyword rather than the VB IsNothing function:
If(Item IsNot Nothing),
If(Item.LabelDescription.ToString.Length > 30, x, y), y)
The If operator is not the same as the If/Then/Else statement block.
I'm checking to see if DBNull occurs.
C# Code:
tbDuration.Text = (string)MyReader["TDuration"]==DBNull?"":(string)MyReader["TDuration"];
But I get an error on DBNull:
Error 4 'System.DBNull' is a 'type' but is used like a 'variable' C:\Visual Studio
How to check for DBNull?
Regards
Tea
DBNull is a type. What you're looking for is a static member on that type: DBNull.Value. Something like this:
tbDuration.Text = MyReader["TDuration"] == DBNull.Value ? "" : (string)MyReader["TDuration"];
Note: You also don't want to cast to a string for the purpose of the comparison. DBNull.Value will work for the actual result from MyReader[], but not if you cast it. After validating that it's not null, then you cast it.
Use DBNull.Value for checking for null values.
tbDuration.Text =
MyReader["TDuration"]==DBNull.Value?"":(string)MyReader["TDuration"]
Or, you can use:
tbDuration.Text =
DBNull.Value.Equals(MyReader["TDuration"]) ?"":(string)MyReader["TDuration"]
You should be checking for DBNull.Value, not DBNull directly.
System.DbNull.Value, is a valid reference to an instance of System.DbNull.
So you can check against the static value property of DBNull.
So It should be DbNull.Value not only DbNull
(string)MyReader["TDuration"]==DBNull.Value?"":(string)MyReader["TDuration"];
I have a gridview and the OnRowDataBound event is linked to this function:
if (e.Row.RowType == DataControlRowType.DataRow)
{
ThisRow = e.Row.DataItem as MyObjectModel;
if (ThisRow.Property1 == null) { e.Row.Cells[5].Text = "-"; }
This code looks at the value of a property of the object in the data source and if it's null, converts the null to display "-" in column 5. The problem I'm having is that if I change the order of the columns of the gridview, then I need to change the index of every other modification.
What I'd like to do is change the statement "e.Row.Cells[5].Text" to something that says "the cell whose column header is xyz".
Any suggestions?
Thanks.
I think you might be better off handling this in your gridview code rather than your code behind, especially if you are shuffling columns around.
Here's an example with ponies. Feel free to edit as needed.
<asp:TemplateField HeaderText="Likes Ponies?">
<ItemTemplate>
<asp:Label ID="uxLikesPoniesLabel" runat="server" Text=’<%#
DataBinder.Eval(Container.DataItem, "FavoritePonies").ToString() == "" ?
"I like " + DataBinder.Eval(Container.DataItem, "FavoritePonies") + " Ponies!" :
"-"
%>’ />
</ItemTemplate>
</asp:TemplateField>
I'm using the C# (boolean condition) ? "value if true" : "value if false" here.
So, this code would check to see if the FavoritePonies column in the particular row is "" (because NULL values are displayed as empty string) and if so, it displays "-" instead. If it's not null, then it displays what was originally in the database with DataBinder.Eval(Container.DataItem, "FavoritePonies") as "I like Ponies!".
hiii everyone....
i have small problem with my code in vb.net that i want to use (Eval) in my project
so i write this code :
<asp:Label ID="Label1" runat="server"
Text='<%#Eval("PAG_PAGES") == null ? "" : ((PostAgenciesModel.PAG_PAGES)(Eval("PAG_PAGES"))).PAGE_TITLE_AR %>' />
and this code i used in my C# project .... all want to show the (Label1) in inside my GridView....
("PAG_PAGES") is the name of table..
PostAgenciesModel is the edmx...
PAGE_TITLE_AR is the colum in ("PAG_PAGES") that i want to show it
can anyone help plzzz
thanxx
The issue is that you are using C# features in a VB.NET web application.
The null keyword and the ?: and == operators are C# constructs
In VB.NET, null is Nothing, == is IS and ?: is the IIf function.
<%# If(Eval("PAG_PAGES") Is Nothing,
"",
DirectCast(Eval("PAG_PAGES"), PostAgenciesModel.PAG_PAGES).PAGE_TITLE_AR) %>
Elaborating on what Oded wrote:
expr == null ---> expr Is Nothing
a ? b : c ---> If(a, b, c)
(Type)expr ---> DirectCast(expr, Type)