基本上我有一个下拉列表,其中填充了文本和值,具体取决于单选按钮列表的选定索引。代码如下:
protected void RBLGender_SelectedIndexChanged(object sender, EventArgs e)
{
DDLTrous.Items.Clear();
DDLShoes.Items.Clear();
if (RBLGender.SelectedValue.Equals("Male"))
{
String[] MaleTrouserText = {"[N/A]", "28\"", "30\"", "32\"", "34\"", "36\"", "38\"", "40\"", "42\"", "44\"", "48\""};
String[] MaleTrouserValue = { null, "28\"", "30\"", "32\"", "34\"", "36\"", "38\"", "40\"", "42\"", "44\"", "48\"" };
String[] MaleShoeText = { "[N/A]", "38M", "39M", "40M", "41M", "42M", "43M", "44M", "45M", "46M", "47M" };
String[] MaleShoeValue = { null, "38M", "39M", "40M", "41M", "42M", "43M", "44M", "45M", "46M", "47M" };
for (int i = 0; i < MaleTrouserText.Length; i++)
{
ListItem MaleList = new ListItem(MaleTrouserText[i], MaleTrouserValue[i]);
DDLTrous.Items.Add(MaleList);
}
for (int i = 0; i < MaleShoeText.Length; i++)
{
ListItem MaleShoeList = new ListItem(MaleShoeText[i], MaleShoeValue[i]);
DDLShoes.Items.Add(MaleShoeList);
}
}
else if (RBLGender.SelectedValue.Equals("Female"))`
当选择文本'[N / A]'时,我希望将值NULL插入数据库。目前,当选择'[N / A]'时,输入数据库的值是[N / A]有人知道为什么吗?
这是我的数据库插入代码:
protected void UpdateWorkWear()
{
try
{
string connectionString = Common.GetConnectionString("w34to4tmConnectionString");
SqlConnection conn = new SqlConnection(connectionString);
StringBuilder sbSQL = new StringBuilder();
string str = DDLSurname.SelectedValue;
if (str.Contains("'"))
{
str = str.Replace("'", " ");
}
sbSQL.Append("IF EXISTS (select * from TO4_WORKWEAR_DISTRIBUTION where EmpID = @EmpID)");
sbSQL.Append(" UPDATE TO4_WORKWEAR_DISTRIBUTION SET ");
sbSQL.Append("costCentre = coalesce(@Cost_Centre, CostCentre), EmployeeType = coalesce(@EmployeeType, EmployeeType), Initials = coalesce(@Initials, Initials), Surname = coalesce(@Surname, Surname), ");
sbSQL.Append("IssueQuarter = coalesce(@IssueQuarter, IssueQuarter), TrouserSize = coalesce(@Trouser_S, TrouserSize), TrouserLength = coalesce(@Trouser_L, TrouserLength), ");
sbSQL.Append("PoloSize = coalesce(@Polo_S, PoloSize), SweatSize = coalesce(@Sweat_S,SweatSize), ShortSize = coalesce(@Short_S, ShortSize), ShoeSize = coalesce(@Shoe_S, ShoeSize) WHERE EmpID = @EmpID ");
sbSQL.Append(" else INSERT INTO TO4_WORKWEAR_DISTRIBUTION (EmpID,CostCentre, EmployeeType, Initials, Surname, IssueQuarter, TrouserSize, TrouserLength, PoloSize, SweatSize, ShortSize, ShoeSize) ");
sbSQL.Append("VALUES (@EmpID, @Cost_Centre, @EmployeeType, @Initials, @Surname, @IssueQuarter, @Trouser_S, @Trouser_L, @Polo_S, @Sweat_S, @Short_S, @Shoe_S)");
sbSQL.Replace("@EmpID", "'" + DDLEmpID.SelectedValue + "'");
sbSQL.Replace("@Cost_Centre", "'" + DDLCostCentre.SelectedValue + "'");
sbSQL.Replace("@EmployeeType", "'" + RBLAssociate.SelectedValue + "'");
sbSQL.Replace("@Initials", "'" + DDLInitials.SelectedValue + "'");
sbSQL.Replace("@Surname", "'" + str + "'");
sbSQL.Replace("@Trouser_S", "'" + DDLTrous.SelectedValue + "'");
sbSQL.Replace("@Trouser_L", "'" + DDLTrouserLnh.SelectedValue + "'");
sbSQL.Replace("@Trouser_Q", "'" + DDLTrouserQty.SelectedValue + "'");
sbSQL.Replace("@Polo_S", "'" + DDLPolo.SelectedValue + "'");
sbSQL.Replace("@Polo_Q", "'" + DDLPoloQty.SelectedValue + "'");
sbSQL.Replace("@Sweat_S", "'" + DDLSweat.SelectedValue + "'");
sbSQL.Replace("@Sweat_Q", "'" + DDLSweatQty.SelectedValue + "'");
sbSQL.Replace("@Shoe_S", "'" + DDLShoes.SelectedValue + "'");
sbSQL.Replace("@Short_S", "'" + DDLShor.SelectedValue + "'");
sbSQL.Replace("@InputDate", "'" + System.DateTime.Now.Date.ToString("MM/dd/yyyy") + "'");
sbSQL.Replace("@IssueQuarter", "'" + RBLQuarter.SelectedValue + "'");
SqlCommand cmd = new SqlCommand(sbSQL.ToString(), conn);
cmd.CommandType = CommandType.Text;
conn.Open();
int result = cmd.ExecuteNonQuery(); //execute the SQL command on the database
LiteralControl lit = new LiteralControl();
lit.Text = String.Format("Data Uploaded Successfully.");
感谢您的帮助。 我决定在数据库中插入空白字符串“”而不是null,因为当我从中运行报表时,它会让我的生活变得更轻松。
答案 0 :(得分:3)
问题是如果ListItem
的值设置为null,它将使用Text属性作为值。例如这段代码:
ListItem l = new ListItem("Cats", null);
string s = l.Value;
s的值是“Cats”。
解决方案不是存储null,而是存储空字符串,或-1或类似的东西,然后在代码中检查这一点。
答案 1 :(得分:0)
C#null与数据库NULL不同。
在插入代码中,您可能需要获取值为“N / A”/ null然后插入DBNull。您的数据库插入代码是什么样的?
答案 2 :(得分:0)
根据我对C#ListItems的解释,如果未定义Value,那么当您查找Value时,您将获得Text。这可能是一个简单的if (Value == null) return Text;
检查,因此如果您故意将Value设置为null,它将返回Text而不是null值。
另一种看待它的方法是你调用new ListItem(string, string)
构造函数,但是为第二个参数传递一个空值。这基本上调用new ListItem(string)
,它同样将值设置为Text。也许不是字面意思,但最终结果是一样的。
修改强>
我在一个简单的情况下做了一些测试。如果初始化ListItem对象的值为null
,则ListItem.Value和DropDownList.SelectedValue(指向该列表项时)将获得与Text相同的值。因此,可以推断初始化具有null
值的ListItem将Value设置为与Text相同。即使稍后通过null
将其设置为ListItem.Value = null;
,仍会将其设置为文本。
您必须将Value设置为某个值,而不是直接输入SelectedValue,请检查SelectedValue是否等于此预设值。我个人使用_|NULL|_
。
代码段
ListItem li;
li = new ListItem("Test", null);
Console.WriteLine(li.Value.ToString());
li.Value = null;
Console.WriteLine(li.Value.ToString());
DropDownList ddl = new DropDownList();
ddl.Items.Add(li);
ddl.SelectedIndex = 0;
Console.WriteLine(ddl.SelectedValue);
Console.ReadLine();
输出
Test
Test
Test
答案 3 :(得分:0)
使用DBNull.Value而不是null。
答案 4 :(得分:0)
你可以这么做:
sbSQL.Replace("@Trouser_S", (DDLTrous.SelectedValue == "[N/A]" ? "NULL" : "'" + DDLTrous.SelectedValue + "'"));
答案 5 :(得分:-1)
您需要显示如何从下拉列表中提取数据并将其放入数据库中。使用SelectedItem属性获取ListItem(您可能必须强制转换它),然后显式调用ListItem.Value进行数据库插入/更新。
编辑...为代码做
而不是
sbSQL.Replace("@Trouser_S", "'" + DDLTrous.SelectedValue + "'");
DO
ItemList il = (ItemList)DDLTrous.SelectedItem;
string s = il.Value;
if ( s != null )
s = "'" + s + "'";
else
s = "null";
sbSQL.Replace("@Trouser_S", s);
您也可以尝试在值中使用“null”替换null。但关键是你不能在空值周围加上“'”+ x +“'”,否则它会以字符串形式出现。
再次编辑..
如果ItemList.Value不能为null,则将值设置为“null”并更改if语句。
ItemList il = (ItemList)DDLTrous.SelectedItem;
string s = il.Value;
if ( s != "null" )
s = "'" + s + "'";
sbSQL.Replace("@Trouser_S", s);