如何通过maskedtextbox在C#中插入空值 [英] how to insert null value in C# through maskedtextbox

查看:74
本文介绍了如何通过maskedtextbox在C#中插入空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<big>  if(maskedTextBox1.Text == "")
                {
                   
                    maskedTextBox1.Text = "##/##/#####";

                }</big> //[i do it like that]
                    string sc = ConfigurationManager.ConnectionStrings["dcon2"].ConnectionString;
                    OdbcConnection con = new OdbcConnection();
                    con.ConnectionString = sc;
                    con.Open();
                    string query = "insert into fee_payments_mst(c_code,c_class,c_amount,c_cash,c_cheque,c_date,c_cheque_date,c_chequeno,c_bankname,c_branch_name,c_user) values(?,?,?,?,?,?,?,?,?,?,?)";
                    cmd = new OdbcCommand(query, con);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@C_code", s );
                    cmd.Parameters.AddWithValue("@c_class", comboBox2.Text);
                    cmd.Parameters.AddWithValue("@c_amount", textBox1.Text);
                    cmd.Parameters.AddWithValue("@c_cash", 1);
                    cmd.Parameters.AddWithValue("@c_cheque", 0);
                    cmd.Parameters.AddWithValue("@c_date",Convert .ToDateTime  ( maskedTextBox2.Text));



                    cmd.Parameters.AddWithValue("@c_cheque_date", Convert .ToDateTime (maskedTextBox1 .Text ));
                    cmd.Parameters.AddWithValue("@c_chequeno", textBox3.Text);
                    cmd.Parameters.AddWithValue("@c_bankname", comboBox4.Text);

                    cmd.Parameters.AddWithValue("@c_branchnaame", textBox2.Text);
                    cmd.Parameters.AddWithValue("@c_user", Txtuser.Text);
                    cmd.ExecuteNonQuery();
                    con.Close();


                    MessageBox.Show("the record has been inserted successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);


我想将maskedtextbox1.text作为null值插入到我的sybase数据库中.但是我无法做到这一点.我也尝试像maskedtextbox.text="##/##/####";那样插入它,但触发了一个错误,该[字符串无效];


i want to insert maskedtextbox1.text as null value to my sybase databse.but am not able to do that.i also try to insert that as like maskedtextbox.text="##/##/####";but a error was fired that the [the string is not a valid] ;

推荐答案

您为什么希望它能起作用?
如果将字符串设置为"##/##/####",为什么会期望将其很好地转换为DateTime值?

实际上,为什么要为此使用MaskedTextBox?为什么不使用DateTimePicker代替呢?它具有一个Value属性,该属性以DateTime开头,并且始终是有效日期.对于您的版本,我可以输入"72/49/7321",然后尝试将其插入数据库!
Why would you expect that to work?
If you set a string to "##/##/####" why would you expect that to convert nicely to a DateTime value?

In fact, why are you using a MaskedTextBox for this at all? Why not use a DateTimePicker instead? It has a Value property which is a DateTime to start with, and which is always a valid date. With your version, I could enter "72/49/7321" and you would try to insert that to your DB!


作为Griffs答案的扩展,您也可以简单地检查是否给定的Text可以解析为DateTime ...用户可以自由选择首选格式.

如果不是日期,则可以保存一个空字符串.

[回复后编辑]

As an extension to Griffs answer, you could also simply check, if the given Text could be parsed as an DateTime... the user is free to choose the prefered format.

If it isn''t a date, you could save an empty string.

[edit after response]

if(maskedTextBox1.Text == "") {
    maskedTextBox1.Text = "##/##/#####";
}

//[...]

cmd.Parameters.AddWithValue("@c_cheque_date", Convert.ToDateTime (maskedTextBox1 .Text ));

//[...]



好的...这是您的代码中断的地方...

您应该预先检查maskedTextBox1中是否有有效的DateTime并将其存储在变量中.

D"您需要一些代码,还是可以从这里开始处理?



Ok... this is where youre code breaks...

You should rather check upfront if there''s a valid DateTime in the maskedTextBox1 and store it in a variable.

D''you need some code or could you handle it from here on?


这篇关于如何通过maskedtextbox在C#中插入空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆