繁体   English   中英

使用c#在asp.net中添加空字段

[英]Adding empty fields in asp.net using c#

// Add product data to DB.
 AddProducts products = new AddProducts();
 bool addSuccess = products.AddProduct(AddProductName.Text, AddProductDescription.Text, AddSubHeading1.Text, AddSubInfo1.Text, AddSubHeading2.Text, AddSubInfo2.Text, AddSubHeading3.Text, AddSubInfo3.Text, AddSubHeading4.Text, AddSubInfo4.Text, AddSubHeading5.Text, AddSubInfo5.Text, DropDownAddCategory.SelectedValue, ProductImage.FileName);
 if (addSuccess)
 {
    // Reload the page.
    string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count());
                Response.Redirect(pageUrl + "?ProductAction=add");
 }
 else
 {
     LabelAddStatus.Text = "Unable to add new product to database.";
 }
}
else
{
    LabelAddStatus.Text = "Unable to accept file type.";
}

我正在为我的雇主创建一个网站,其中有一个允许用户添加产品的管理页面,我现在尝试了一些方法,但是我无法通过此页面将空字段输入数据库。 有人可以帮忙吗?

可以检查字符串是否为null并使用给出''值

AddProductName?.Text?? " " //in C#6.0

或者你可以使用

if(string.IsNullOrEmpty(AddproductName.Text))
{
  AddproductName.Text=" ";
}

然后尝试插入数据库

如果为null,还要检查是否允许数据库采用空值或​​空值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM