Amr Ashraf
Member
- Joined
- Apr 5, 2022
- Messages
- 14
- Programming Experience
- Beginner
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 :
Then I need to check if the user checked any checkbox :
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 :
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 :
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 .
CheckNulls:
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 :
CheckExistAndDo:
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 :
TakeAction:
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 :
Problem:
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 .