为什么我的方法互相干扰以及C#中的if语句?
我有一个表格可以在某些情况下插入SQL数据库中的某些数据,首先我需要检查nulls并提醒用户:
void CheckNulls() {
try {
if (txtcode.Text.Length == 0 || txtItem.Text.Length == 0 || txtWh.Text.Length == 0) {
MessageBox.Show("Fill Required Fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else {
checkExistAndDo();
}
}
catch(Exception Err) {
MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
然后,我需要检查用户是否检查任何consectbox
:
void checkExistAndDo() {
if (chkProduct.Checked || chkMaterial.Checked)
{
if (chkProduct.Checked == true) {
if (!chkMaterial.Checked) {
da = new SqlDataAdapter("SELECT [ProductCode] FROM Products Where [ProductCode] = @prcode ", Cn);
da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0) {
MessageBox.Show("Existing Code", "Error");
}
else {
TakeAction();
}
}
}
else {
da = new SqlDataAdapter("SELECT Code FROM Items Where Code = @prcode ", Cn);
da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0) {
MessageBox.Show("Existing Code", "Error");
}
else {
TakeAction();
}
}
}
else {
MessageBox.Show("Check even one", "check", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
因此,我有总共4种不同的组合:
chkProduct.Checked : chkMaterial.Checked : Action
--------------------------------------------------------------
true : true : InsertSubProduct()
true : false : InsertProduct()
false : true : InsertMaterial()
false : false : Ask User to Check
然后,我需要根据组合采取行动:
private void TakeAction()
{
try
{
if (chkProduct.Checked == true && chkMaterial.Checked == false)
{
InsertProduct();
MessageBox.Show("Product Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (chkProduct.Checked == false && chkMaterial.Checked == true)
{
InsertMaterial();
MessageBox.Show("Material Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
InsertSubProduct();
MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception Err)
{
MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.Close();
}
}
问题是,该部分没有做任何不显示我的消息的事情,甚至不是错误消息,就好像它在我的代码中不存在:
else
{
InsertSubProduct();
MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
对不起。对于长期的帖子,但我尽了最大的努力并多次调试了代码,但我无法通过此事,谢谢您,您的帮助非常感谢。
I have a form to insert some data in sql database with some conditions , First I need to check for nulls and alert the user :
void CheckNulls() {
try {
if (txtcode.Text.Length == 0 || txtItem.Text.Length == 0 || txtWh.Text.Length == 0) {
MessageBox.Show("Fill Required Fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else {
checkExistAndDo();
}
}
catch(Exception Err) {
MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Then I need to check if the user checked any checkbox
:
void checkExistAndDo() {
if (chkProduct.Checked || chkMaterial.Checked)
{
if (chkProduct.Checked == true) {
if (!chkMaterial.Checked) {
da = new SqlDataAdapter("SELECT [ProductCode] FROM Products Where [ProductCode] = @prcode ", Cn);
da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0) {
MessageBox.Show("Existing Code", "Error");
}
else {
TakeAction();
}
}
}
else {
da = new SqlDataAdapter("SELECT Code FROM Items Where Code = @prcode ", Cn);
da.SelectCommand.Parameters.AddWithValue("@prcode", txtcode.Text);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0) {
MessageBox.Show("Existing Code", "Error");
}
else {
TakeAction();
}
}
}
else {
MessageBox.Show("Check even one", "check", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
So , I have 4 different combinations in total :
chkProduct.Checked : chkMaterial.Checked : Action
--------------------------------------------------------------
true : true : InsertSubProduct()
true : false : InsertProduct()
false : true : InsertMaterial()
false : false : Ask User to Check
Then I need to take action based on the combination :
private void TakeAction()
{
try
{
if (chkProduct.Checked == true && chkMaterial.Checked == false)
{
InsertProduct();
MessageBox.Show("Product Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (chkProduct.Checked == false && chkMaterial.Checked == true)
{
InsertMaterial();
MessageBox.Show("Material Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
InsertSubProduct();
MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception Err)
{
MessageBox.Show("This Error Occured :" + Err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.Close();
}
}
The problem is that this part doesn't do anything not showing my message , Not even an error message as if it doesn't exist in my code :
else
{
InsertSubProduct();
MessageBox.Show("SubProduct Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Sorry for long post but i tried my best and debugged the code many times and i can't get pass this , Thanks in advance and your help is deeply appreciated .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
threestate
属性设置为true,则检查的属性将返回true
,用于检查
或不确定的
checkstate 。检查
threestate
属性chkmaterial
复选框If the
ThreeState
property is set to true, the Checked property will returntrue
for either aChecked
orIndeterminate
CheckState.Check the
ThreeState
property forchkMaterial
checkbox根据@joel Coehoorn评论,我尝试了不同的方法
我引用“更好的关注点。CheckNulls()只能返回bool,并且不向用户显示任何消息或调用任何其他方法。Checkexistanddo()根本不应该存在(该逻辑应完全在数据库中作为数据库插入产品/材料的一部分())
。
Based on @Joel Coehoorn comments I tried different approach
I quote " Do separation of concerns better. CheckNulls() should only return a bool, and not show any messages to the user or call any other methods. checkExistAndDo() should not exist at all (that logic should be entirely in the database as part of InsertProduct/MaterialSubproduct()). "
It worked great , Thanks all for replying , I appreciate your guiding .