asp.net null in a little bit of a trick to read from the database _ Practical Tips

Source: Internet
Author: User
Its function is very simple, that is, first look in the database as the name of the field, and then to judge, if its value is empty, then the value of number plus 1.

For example, the addition of black is what we need to pay attention to.
Copy Code code as follows:

int number = 0;
string connstr = @ "Data source=.\sqlexpress; ATTACHDBFILENAME=C:\USERS\LANDO\DESKTOP\UML extension\mypratices\webservices\app_data\database1.mdf;integrated Security=true; User Instance=true ";
using (SqlConnection conn = new SqlConnection (CONNSTR))
{
Conn. Open ();
String str = "SELECT * from Test";
using (SqlCommand cmd = conn. CreateCommand ())
{
Cmd.commandtext = str;
SqlDataReader dr = cmd. ExecuteReader ();
while (Dr. Read ())
{
String name = dr["Name"]. ToString ();
DateTime dt = Dr. GetDateTime (Dr. GetOrdinal ("Inputdate"));
<strong>if (dr["Name"] = = null) {number = 1;} </STRONG>}
}
}
if (number = = 0)
Label1.Text = "No value with name is null";
Else
Label1.Text = "Name is a null value of:" + number;

And then, looking at the data in my data:

In our test table, there are 3 data, and the value of the Name field of the third data is null, just beginning, I take it for granted that the number of the name is null: 1.
A lot of stories tell us that it's easy to make a mistake by jumping to conclusions at random. For example, I am now making this conclusion.
The result is not surprising. Null is not found a value of name?
What's wrong? I'm just starting to think I don't know why. Then, very accustomed to set up breakpoints, and then start F5 start Debugging ...
Image above:

Did you see it? It is not recognized. So what if it's in the database (at least in SQL?). In Oracle I do not know that the null value is actually NOT NULL, it uses the surface phenomenon to deceive you.
However, we can see from the screenshot above that its type: object{system.dbnull}, we can find a clue from here.
Yes, from the type, then I read some dbnull on the internet. Concrete, reader can own under Baidu.
Know is the type of problem, correct it is OK, the core only need to change as follows:
Copy Code code as follows:

if (dr["Name"] = = DBNull.Value)
{
Number = 1;
}

OK, then it will be able to correctly identify.
There is another way, in fact, are almost the same:
Copy Code code as follows:

if (Convert.isdbnull (dr["Name"))
{
Number = 1;
}

It's OK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.