本文介绍了我不想只在C#中的文本框中输入空格,而是在空格中输入charector。这是我的错误代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void btnsave_Click(object sender,EventArgs e) { if(comboslm.Text ==) { MessageBox.Show(请选择推销员名称); comboslm.Focus(); } 否则if(slmname.Text ==|| slmname.Text ==!char.IsWhiteSpace(e.KeyChar)) (这里只有红线显示。也无法渲染。) { MessageBox.Show(空的推销员名称不被接受); slmname.Focus(); } else if(combosmancode.Text ==) { MessageBox.Show(Select Record Key Number); combosmancode.Focus(); } else { SqlConnection con = new SqlConnection(@Data Source = M2\SQL2016; Initial Catalog = inventoryDB; Integrated Security = True); con.Open(); SqlCommand cmd = new SqlCommand(UPDATE smanDB SET smaname = @ label2 WHERE smancode = @ label4,con); cmd.Parameters.Add(@ label2,slmname.Text); cmd.Parameters.Add(@ label4,combosmancode.GetItemText(combosmancode.SelectedItem)); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show(已成功更新); / * combosmancode.SelectedIndex = -1; comboslm.SelectedIndex = -1; slmname.Text =; comboslm.Focus(); * / con = new SqlConnection(@Data Source = M2\SQL2016; Initial Catalog = inventoryDB; Integrated Security = True); con.Open(); string query =SELECT smaname FROM smanDB; string que =SELECT smancode FROM smanDB; SqlDataAdapter da = new SqlDataAdapter(query,con); SqlDataAdapter sda = new SqlDataAdapter(que,con); DataSet ds = new DataSet(); DataSet sds = new DataSet(); da.Fill(ds,smanDB); sda.Fill(sds,smanDB); comboslm.DisplayMember =smaname; comboslm.ValueMember =smaname; comboslm.DataSource = ds.Tables [smanDB]; combosmancode.DisplayMember =smancode; combosmancode.ValueMember =smancode; combosmancode.DataSource = sds.Tables [smanDB]; combosmancode.SelectedIndex = -1; comboslm.SelectedIndex = -1; slmname.Text =; con.Close();
我的尝试: 我试图检查空。我成功了。但是对于空格只有它显示错误。有空格的charector也行。但是只防止显示错误的空格
解决方案else if (slmname.Text == || slmname .Text ==!char.IsWhiteSpace(e.KeyChar))
该声明的后半部分是错误的。您无法将文本字段( slmname.Text )与布尔值(!char.IsWhiteSpace(e.KeyChar))。
private void btnsave_Click(object sender, EventArgs e) { if(comboslm.Text == "") { MessageBox.Show("Please Select Salesman Name"); comboslm.Focus(); } else if(slmname.Text=="" || slmname.Text==!char.IsWhiteSpace(e.KeyChar)) (Here only redline it is showing.So unable to render also) { MessageBox.Show("Empty Salesman Name Not Accepted"); slmname.Focus(); } else if (combosmancode.Text == "") { MessageBox.Show("Select Record Key Number"); combosmancode.Focus(); } else { SqlConnection con = new SqlConnection(@"Data Source=M2\SQL2016;Initial Catalog=inventoryDB;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand("UPDATE smanDB SET smaname=@label2 WHERE smancode=@label4", con); cmd.Parameters.Add("@label2", slmname.Text); cmd.Parameters.Add("@label4", combosmancode.GetItemText(combosmancode.SelectedItem)); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Updated Successfully"); /* combosmancode.SelectedIndex = -1; comboslm.SelectedIndex = -1; slmname.Text = ""; comboslm.Focus();*/ con = new SqlConnection(@"Data Source=M2\SQL2016;Initial Catalog=inventoryDB;Integrated Security=True"); con.Open(); string query = "SELECT smaname FROM smanDB"; string que = "SELECT smancode FROM smanDB"; SqlDataAdapter da = new SqlDataAdapter(query, con); SqlDataAdapter sda = new SqlDataAdapter(que, con); DataSet ds = new DataSet(); DataSet sds = new DataSet(); da.Fill(ds, "smanDB"); sda.Fill(sds, "smanDB"); comboslm.DisplayMember = "smaname"; comboslm.ValueMember = "smaname"; comboslm.DataSource = ds.Tables["smanDB"]; combosmancode.DisplayMember = "smancode"; combosmancode.ValueMember = "smancode"; combosmancode.DataSource = sds.Tables["smanDB"]; combosmancode.SelectedIndex = -1; comboslm.SelectedIndex = -1; slmname.Text = ""; con.Close();What I have tried: I tried to check empty. I succeeded. But for spaces only then it showing error. charector with spaces also ok. But prevent only spaces showing error
解决方案 else if(slmname.Text=="" || slmname.Text==!char.IsWhiteSpace(e.KeyChar))The second half of that statement is wrong. You cannot compare a text field (slmname.Text) to a boolean value (!char.IsWhiteSpace(e.KeyChar)).
更多推荐
我不想只在C#中的文本框中输入空格,而是在空格中输入charector。这是我的错误代码。
发布评论