C#中如何检查和替换空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10613363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check and replace null value in C#
提问by codemanapt
Anyone can guide me a way to check and replace null values in strList with String message "NO DATA" or allow them in dtList. Because when I met a null value in this List,it show an error like "There is no row at position..."
任何人都可以指导我使用字符串消息“NO DATA”检查和替换 strList 中的空值或在 dtList 中允许它们。因为当我在这个 List 中遇到一个空值时,它会显示一个错误,比如“位置没有行......”
private DataTable getSomething1(String text){
DataTable dtb = new DataTable();
...
...
return dtb;
}
...
protected void buttonCheck_Click(object sender, ImageClickEventArgs e){
List<DataTable> strList = new List<DataTable>(){
getSomething(txtInput.Text),
getSomething2(txtInput.Text),
getSomething3(txtInput.Text),
...
};
}
I used C# in Webform. Plz, help me to fix this code or guide me a way to solve it. Thanks in advance.
我在 Webform 中使用了 C#。请帮助我修复此代码或指导我解决它的方法。提前致谢。
@tech, @ riffnl: Finally I get all of your ideas and it really work now. Anw, thanks all pro here for advices. So, this is my solution ^^
@tech,@ riffnl:我终于明白了你的所有想法,现在它真的奏效了。Anw,在这里感谢所有专业人士的建议。所以,这是我的解决方案^^
private String ConvertNullToEmptyString(DataTable element)
{
if (element.Rows.Count > 0) //just add this condition here
{
if (!DBNull.Value.Equals(element.Rows[0]["FullName"]))
return (string)element.Rows[0]["FullName"] + " ";
else
return String.Empty;
}
return "NO DATA";
}
protected void ....(){
List<DataTable> strList = new List<DataTable>(){
GetItem1(txtName.Text), //private DataTable GetItem1(String t)
GetItem2(txtName.Text), //...
};
txtGrD_D.Text = ConvertNullToEmptyString(strList[0]);
txtGrM_D.Text = ConvertNullToEmptyString(strList[1]);
}
采纳答案by riffnl
Since you're using a datatable I assume you're not talking about NULL (or nill), but DBNull.. Change your function ConvertNullToEmptyString like this:
由于您使用的是数据表,因此我假设您不是在谈论 NULL(或 nill),而是在谈论 DBNull .. 像这样更改您的函数 ConvertNullToEmptyString:
private String ConvertNullToEmptyString(DataTable element){
if (element.Rows[0]["Fullname"] == DBNull.Value || element.Rows[0]["Fullname"] == null) {
return "NO DATA";
} else {
return element.Rows[0]["Fullname"].ToString();
}
}
as requested: new sample;
根据要求:新样品;
// a list of datatables each containing 1 row, wasn't that the point of datatables
// anyway -- I think you don't have any rows, so let's try this:
private String ConvertNullToEmptyString(DataTable element)
{
if (element.Rows.Count == 0)
{
return "NO DATA";
}
if (element.Rows[0]["Fullname"] == DBNull.Value || element.Rows[0]["Fullname"] == null)
{
return "NO DATA";
}
else
{
return element.Rows[0]["Fullname"].ToString();
}
}
protected void Test()
{
List<DataTable> strList = new List<DataTable>(){
GetItem("test1"), //private DataTable GetItem1(String t)
GetItem("test2") //...
};
txtGrD_D.Text = ConvertNullToEmptyString(strList[0]);
}
回答by McGarnagle
You can use C#'s null coalescing operator??:
您可以使用 C# 的空合并运算符?? :
string s = null;
s = s ?? "NO DATA";
// s now equals "NO DATA";
回答by Mitja Bonca
Try it this way by using String.IsNullOrEmpty. I double a cell in dataTable can be null.
通过使用 String.IsNullOrEmpty 以这种方式尝试。我在 dataTable 中的一个单元格可以为空。
//creating an example table with one row (no data inisde):
DataTable table = new DataTable();
table.Columns.Add("FullName", typeof(string));
table.Rows.Add("");
string str = ConvertNullToEmptyString(table);
//method to check for string:
private String ConvertNullToEmptyString(DataTable element)
{
String s = element.Rows[0]["FullName"].ToString();
return (string.IsNullOrEmpty(s) ? "NO DATA" : s);
}
回答by tech
If the error is "There is no row at position...", then I guess the probelm is not that a string is null, but it is, instead, that you are trying to access a row that does not exist in the datatable.
如果错误是“位置没有行...”,那么我猜问题不是字符串为空,而是您试图访问数据表中不存在的行.
In particular, in the line :
特别是,在该行中:
String s = element.Rows[0]["FullName"].ToString();
You try to access the first row of the datatable. What if the datatable is empty ? You get an error! So you should check if the datatable contains rows with something like this:
您尝试访问数据表的第一行。如果数据表为空怎么办?你得到一个错误!所以你应该检查数据表是否包含这样的行:
if (element.Rows.Count >0)
Answer to you comment: yes we can. modify your:
回答你的评论:是的,我们可以。修改您的:
foreach (DataTable element in strList)
into something like:
变成类似的东西:
foreach (DataTable element in strList)
{
if (element.Rows.Count>0)
{
if(strList.Any(item => item == null))
{
ConvertNullToEmptyStringelement);
}
txtItem1.Text = mytext ;
.....
}
else
{
txtItem1.Text = "No Data";
}
}
This is as far as I can go without modifying too much your program structure.
这是我在不修改太多程序结构的情况下所能做到的。

