如何在asp.net中将空的textox转换为NULL值

编程入门 行业动态 更新时间:2024-10-22 18:38:38
本文介绍了如何在asp中将空的textox转换为NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好; 如何将空文本框的值转换为NULL值? 我想将文本框中的数据(NULL)存储到数据库中. 代码:

Hello; How can we convert the value of empty textbox to NULL value? I want to store the data(NULL) from textbox to data base. CODE:

if (TextBox11.Text == String.Empty) { cmd.Parameters.Add("@p", SqlDbType.Float).Value = /* WHAT I WRITE HERE TO TAKE NULL FORM TEXTBOX TO STORE IN DATABASE */ }

这显示了一个错误: 无法将参数值从字符串转换为双精度." 请帮帮我.

This shows an error: "Failed to convert parameter value from a String to a Double." Please help me.

推荐答案

您能在这里解释您要尝试什么吗:Convert.DBNull.Equals(TextBox11.Text); 当然,它看起来与您想要的不一样. 顺便说一句,一个简单的VS调试器会告诉您错误行,错误本身是不言而喻的. 我想将文本框中的数据(NULL)存储到数据库中. 试试: Can you explain what are you trying here: Convert.DBNull.Equals(TextBox11.Text); Surely, it does not look like what you intend to. BTW, a simple VS Debugger would had told you the error line and error in itself is quite self-explanatory. I want to store the data(NULL) from textbox to data base. Try: cmd.Parameters.Add("@p", SqlDbType.Float).Value = System.DBNull.Value;

或者您也可以采用其他方式- Or you can also go the other way - <pre lang="sql">if (!String.IsNullorEmpty(TextBox11.Text))<br /> {<br /> cmd.Parameters.Add("@p", SqlDbType.Float).Value = TextBox11.Text.Trim();<br /> }</pre><br />

当textbox1为空时,根据您的需要,将不会简单地传递此参数,并且您的数据库将为NULL. HTH- 拉杰夫 如果有帮助,请投票并标记为接受.

When the textbox1 is empty, this parameter won''t be passed simply and your database will have NULL, as per your need. HTH - Rajeev Please vote and mark the answer as accepted if this helps you.

您可以尝试 You can try <code> cmd.Parameters.AddWithValue("@p", (TextBox11.Text.Trim() == string.Empty) ? DBNull.Value : (object)TextBox11.Text.Trim()); </code>

更多推荐

如何在asp.net中将空的textox转换为NULL值

本文发布于:2023-10-16 16:38:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   中将   如何在   asp   NULL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!