.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

How to check for null/empty strings on dynamic objects?

Posted By:      Posted Date: August 21, 2010    Points: 0   Category :ASP.Net

What is the proper way to check for null or empty strings on dynamic objects? This is the only solution I've found so far:

if (admin.PasswordHash.GetType() == typeof(DBNull) || admin.PasswordHash == string.Empty)

If the field is null, the object returned is DBNull.Value, but if the field is empty string, it's return as a System.String object. This makes it impossible to do regular "admin.PasswordHash == DBNull.Value" check, as that will fail whenever the field has been set to empty string. Is my example the right (and only) way to go?

View Complete Post

More Related Resource Links

Microsoft Matrix Framework DataGrid - Update Command - Convert empty strings to NULL



we have a AutoGenerateColumns-enabled WmxDataGrid in which we are able to update table rows in a DataBase connected with a SqlDataSource.

Each time we clear a field an empty string is written to the database where we want to write DBNull values.

At the moment we generate the update string via UPDATE... SET.. and the SQL Server function NULLIF(value, '').

Works proper but when I want to add the values to e.NewValues in BeginUpdate event I'm not able to read the values from the auto generated text boxes.

How do I do that? Is there an easier way to automatically convert empty strings to NULL values?


Clay: malleable C# dynamic objects - part 2

In the first part of this post , I explained what requirements we have for the view models in Orchard and why we think dynamic is a good fit for such an object model. This time, we're going to look at Louis ' Clay library and how you can use it to create object graphs and consume them. But before we do that, I want to address a couple of questions. 1. If we use dynamic, aren't we losing IntelliSense and compile-time checking and all the good things that come with statically-typed languages? And is C# becoming overloaded with concepts, and trying to be good at everything but becoming good at nothing? Hush, hush, everything is going to be all right. Relax. Now think of all the XML/DOM styles of APIs that you know in .NET (or Java for that matter...(read more)

Clay: malleable C# dynamic objects - part 1: why we need it

When trying to build the right data structure in Orchard to contain a view model to which multiple entities blindly contribute, it became obvious pretty fast that using a dynamic structure of sorts was a must. What we needed was a hierarchical structure: a page can have a list of blog posts and a few widgets, each blog post is the composition of a number of parts such as comments, comments have authors, which can have avatars, ratings, etc. That gets us to the second requirement, which is that multiple entities that don't know about each other must contribute to building that object graph. We don't know the shape of the graph in advance and every node you build is susceptible to being expanded with new nodes. The problem is that C# static types...(read more)

Get the value of check box having dynamic data


hi all.........

i wan to develop a web page to compare prices of  various items .............

i e for each item there is a check box.  u can click two or three check boxes and Click on a Button to Compare the price and features.........

on clicking Compare button the data has to be gathered in a data table n then bind it to grid view based on the values of the check boxes which are clicked...

values has to be called from the data base... so can any one help me to implement the same using vb..............

Dropdown menu - Null or empty string??


I have drop dowm menu as follow.

<asp:ListItem Value="">none</asp:ListItem>
<asp:ListItem Value="STO">Stock</asp:ListItem>
<asp:ListItem Value="ORD">Order</asp:ListItem>

If I chose none, it stores data as "Null"

If I Query the data as below, I don't get the data has value of "Null"

SELECT tabale From type Where type <> STO or type <> ORD

I get data that has empty string but not Null.

Here is my question.

1, How do I write dropdown menu value in oder to get empty string instead of Null?
I did <asp:ListItem Value="">none</asp:ListItem> but this stores Null.

2, Why this Query won't pick up Null?
SELECT mytabale FROM type WHERE type <> STO or type <> ORD

Thank you for your help.

Dynamic query for Null field checking

Hi I want dynamiclly find the table to null values finding.. Please give me quickly   Regards Ram

Dynamic query for Null field checking

Hi I want dynamically check the data Formation query in SQL server please  provide me the query

error in MVC2 on empty string form fields which are required: "This property cannot be set to a null

In model I have:[DisplayName("ISBN")] [Required(ErrorMessage = "ISBN is required.")] [StringLength(50, ErrorMessage = "ISBN book must have max {1} chars.")] [DisplayFormat(ConvertEmptyStringToNull = false)] public string isbn { get; set; }But when I submit form with empty field ISBN I have an error - ConstraintException: This property cannot be set to a null value.I have find that other people have the same problem and they say that debbugger in VS2010 works wrong - yes ?http://p2p.wrox.com/book-professional-asp-net-mvc-2/79788-constraintexception-unhandled-user-code.html#post259245http://efreedom.com/Question/1-3129080/Server-Side-Validation-REQUIRED-String-Property-MVC2-Entity-Framework-Workhttp://mvcmusicstore.codeplex.com/workitem/6604

how to check if a Dataview is empty?

I am having trouble with the syntax to check if my Dataview is empty.  If it is empty then the next line when I try draw information from it generates an error. How can I fix this?Dim ProjectDV As DataViewDim arg As New DataSourceSelectArgumentsProjectDV = SqlDataSource1.Select(arg)   If ProjectDV.Count <> 0 ThenpicAd.Text = LCase(ProjectDV(0)("Ad"))End If  

Display Null if Column is empty

is it possible to display Nullif my sql data is null while binding it with gridview in asp.net<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Height="185px" Width="244px"> <RowStyle BackColor="#EEEEEE" ForeColor="Black" /> <FooterStyle BackColor="#CCCCCC" ForeColor="Black" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="Gainsboro" /> </asp:GridView> string command="select * from user1"; DataSet ds1 = new DataSet(); ds1 = ob.getall(command); GridView1.DataSource = ds1; GridView1.DataBind(); for example like this

SSRS 2005 + Null check box + change Label 'NULL'

Hi, My management asked me if I can change the label of NULL check box for report parameters. They said that users may not understand 'NULL' label means. Is it possible to do. When I searched, some said it can be done by creating custom control and registering it in GAC etc. I appreciate your response. Thanks, Spunny

how to insert null or empty string to datetime var ?

hi i get data from xml file and sometime the date is empty. i have this code:     try { TimeTo = Convert.ToDateTime(R[15].ToString()); }         catch { TimeTo = null ; } but i got error because i cant insert null to datetime var what i can do ? thak's in advance

if NULL in database table - display empty string?


I have a table in an Sql Server Database from which I want to fetch data to a form. 

[Headline] [varchar](max) NULL,
[Text] [varchar](max) NULL,

[ID] [int] IDENTITY(1,1) NOT NULL,

[Headline] [varchar](max) NULL,

[Text] [varchar](max) NULL,

I want to check both Headline and Text to see if they are null, and if so display an empty string each for them. I've looked at some tutorials but don't yet understand really how to use it. DBNull? DataRow.IsNull? Something else? Can I check both Headline and Text at the same time or do I need do check them separately?

How can you check how much heap memory your objects are using?


Is there a way to check how much heap memory your objects are using??  (during debug mode of course) .  I am using Visual Studio 2005 with C# ASP.net.

Secondly, is there a way to check how much memory session variables are using as well?


Including NULL columns as empty elements in SELECT FOR XML



Hi everyone,


I was wondering if it is possible for a SELECT FOR XML statement to map a row with a NULL value in a column to an empty element in XML?


For example, let's say I have the following table:


CREATE TABLE NetworkAdapter



MacAddress char(17)



The table has one row with the values (10, NULL). Can I use the SELECT FOR XML statement to return the following XML:




<MacAddress /> -- Or <MacAddress></MacAddress>, doesn't matter



Is it possible to do this without using ISNULL on the MacAddress column? Or if not, how would yo

SQL CE 3.5 Null Parameter Check



I cannot find a definitive answer for a very simple question. In SQL CE 3.5, I am attempting to execute the following:

            using (var connection = new SqlConnection(""))

                using (var command = connection.CreateCommand())

                    command.CommandType = CommandType.Text;
                    command.CommandText = "Select * From County Where CountyId = Coalesce(@Id,3)";
                    var dbParameter = command.CreateParameter();
                    dbParameter.ParameterName = "@

How to check if DataTable row is empty



Im using "string user = Database.GetMostRecentReply(id).Rows[0][2].ToString();" to get the username from the most recent reply in my forum.

But how do I check if the row is empty? Im using a try catch at the moment, but there must be a better way to do this.


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend