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

C# 使用c在asp.net中添加空字段#,c#,asp.net,database,C#,Asp.net,Database,我正在为我的雇主创建一个网站,该网站有一个允许用户添加产品的管理页面,我现在尝试了一些方法,但我无法通过该页面将空字段输入数据库。有人能帮忙吗?可以检查字符串是否为空,并使用 // Add product data to DB. AddProducts products = new AddProducts(); bool addSuccess = products.AddProduct(AddProductName.Text, AddProductDescription.Text, AddS

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

可以检查字符串是否为空,并使用

// 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.";
}
或者你可以使用

AddProductName?.Text?? " " //in C#6.0
然后尝试插入到数据库中


如果为null,还应检查是否允许数据库获取空值或null值。

可以检查字符串是否为null,并使用

// 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.";
}
或者你可以使用

AddProductName?.Text?? " " //in C#6.0
然后尝试插入到数据库中


如果为空,还应检查数据库是否允许获取空值或空值。

请显示
产品
模型实际上发生了什么?你有例外吗?我假设您在
addSuccess
中得到了false。在这种情况下,我们希望看到
AddProduct
的代码;myProduct.ProductName=ProductName;myProduct.Description=ProductDesc;myProduct.子目1=子目1;myProduct.SubInfo1=SubInfo1;myProduct.子目2=子目2;myProduct.SubInfo2=SubInfo2;myProduct.子目3=子目3;myProduct.SubInfo3=SubInfo3;myProduct.子目4=子目4;myProduct.SubInfo4=SubInfo4;myProduct.子目5=子目5;myProduct.SubInfo5=SubInfo5;myProduct.ImagePath=ProductImagePath;myProduct.CategoryID=Convert.ToInt32(ProductCategory);请显示
产品
型号实际上发生了什么?你有例外吗?我假设您在
addSuccess
中得到了false。在这种情况下,我们希望看到
AddProduct
的代码;myProduct.ProductName=ProductName;myProduct.Description=ProductDesc;myProduct.子目1=子目1;myProduct.SubInfo1=SubInfo1;myProduct.子目2=子目2;myProduct.SubInfo2=SubInfo2;myProduct.子目3=子目3;myProduct.SubInfo3=SubInfo3;myProduct.子目4=子目4;myProduct.SubInfo4=SubInfo4;myProduct.子目5=子目5;myProduct.SubInfo5=SubInfo5;myProduct.ImagePath=ProductImagePath;myProduct.CategoryID=Convert.ToInt32(ProductCategory);您好,谢谢您的帮助,数据库确实允许空值。但是,在尝试此解决方案时,我始终会遇到相同的异常。您好,谢谢您的帮助,数据库确实允许空值。但是,在尝试此解决方案时,我始终会遇到相同的异常。