0

I've written a code that will allow the user to enter data into the program and this will transfer to an excel file. However I dont want anything being added when one of my text boxes is empty.With this code a row is still being added despite my if else conditions.I have debugged and used break points and the txt_LRU != null is true even if txt_LRU is empty or not.Can someone please tell me why this is happening?

private void button_kaydet_Click(object sender, EventArgs e){

    //the text that is obtained from the text boxes and combo boxes.

    string txt_LRU = this.txtbx_LRUno.Text.ToString();
    string txt_yi = this.txtbx_yi.Text.ToString();
    string txt_td = this.cmbx_td.Text.ToString();
    string txt_toptarih = this.dtp_toplanti.Text.ToString();
    string txt_bastarih = this.dtp_bas.Text.ToString();
    string txt_teslimtarih = this.dtp_teslim.Text.ToString();
    string txt_ilgilinot = this.txtbx_ilgiliNot.Text.ToString();
    string txt_acikislem = this.txtbx_acikislem.Text.ToString();
    string txt_referedosya = this.txtbx_referedosya.Text.ToString();
    string txt_parcano = this.txtbx_parcano2.Text.ToString();

    int lru_row = 0;
    int kontrol = 0;




        //if there is non existing LRU then save the data into a new row in excel
        if (kontrol == 0)
        {
            if (txt_LRU != null || txt_LRU!="")
            {

                int x = satir_sayisi + 1;
                string satir_no = x.ToString();
                sheet1.Cells[1][satir_sayisi + 2] = satir_no;
                sheet1.Cells[2][satir_sayisi + 2] = txt_LRU;
                sheet1.Cells[3][satir_sayisi + 2] = txt_parcano;
                sheet1.Cells[4][satir_sayisi + 2] = txt_yi;
                sheet1.Cells[5][satir_sayisi + 2] = txt_acikislem;
                sheet1.Cells[7][satir_sayisi + 2] = txt_td;
                sheet1.Cells[8][satir_sayisi + 2] = txt_toptarih;
                sheet1.Cells[9][satir_sayisi + 2] = txt_bastarih;
                sheet1.Cells[10][satir_sayisi + 2] = txt_teslimtarih;
                sheet1.Cells[11][satir_sayisi + 2] = txt_ilgilinot;

            }   

            else if (txt_LRU == null || txt_LRU == "") 
                MessageBox.Show("Please add the LRU number "); 
            }


     //to save and close the excel file
    uyg.DisplayAlerts = false;
    kitap.Save();
    kitap.Close();
    uyg.DisplayAlerts = true;
    uyg.Quit();

   DialogResult dialogResult2 = MessageBox.Show("Would you like to see the excel file?", "Bilgilendirme", MessageBoxButtons.YesNo);
    if (dialogResult2 == DialogResult.Yes)
    {
        Process.Start(@"C:\\Users\\casperpc\\Desktop\\hey.xls");

    }
    else if (dialogResult2 == DialogResult.No)
    {

    }
}

3 Answers 3

2

This condition says that you will do stuff when your txt_LRU is not null, or is not empty. When txt_LRU is null is not equals to an empty string. This case fulfill the OR condition.

 if (txt_LRU != null || txt_LRU!="")
            { 
/*row added*/
     }

The correct condition is Not null AND not empty:

 if (txt_LRU != null && txt_LRU!="")
            { 
/*row added*/
     }

PS: try using string.IsNullOrWhiteSpace(txt_LRU)

Sign up to request clarification or add additional context in comments.

1 Comment

you're welcome... Also, the else condition is the same as the IF condition, check that up.
1

Not for points.

Null is not the same as a zero-length string. A simple check can give this info for you.

enter image description here

Code above is simply:

private void button1_Click(object sender, EventArgs e)
{
    string txt_string = this.textBox1.Text.ToString();
    if (txt_string == null)
        MessageBox.Show("Yes");
    else
        MessageBox.Show("No");
}

Hope this helps.

Comments

0

You are assigning a value to txt_LRU when you read from the text box. However, strings can only be null if they are not initialized. That is why you receive true.

See this explanation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.