How can I pass empty textboxes
I have 5 textbox. I want to put all 4 textbox text in 5th textbox. I can do it, but when any textbox was null ,I had an error. Some textboxes will be empty. How can I do this without error.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2015, 10:51 AM EDT
Otherwise it's default value is an empty string and so this will be the value it has if the user doesn't fill it.
To allow for that possibility or that the user may fill the TextBox with several spaces, try the following code instead:
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Mar 28, 2015, 9:48 AM EDT
textBox5.Text = (textBox1.Text.Length > 0) ? textBox1.Text : " " ;
textBox5.Text += (textBox2.Text.Length > 0) ? textBox1.Text : " " ;
textBox5.Text += (textBox3.Text.Length > 0) ? textBox1.Text : " " ;
textBox5.Text += (textBox4.Text.Length > 0) ? textBox1.Text : " " ;
textBox5.Text=textBox5.Text.Trim();
Afzaal Ahmad ZeeshanRank #60 · 27.6K reputationPosted Mar 28, 2015, 8:01 AM EDT
if(textBox1.Text != null && textBox2.Text != null && textBox3.Text != null) {
// value is not null
}
I have tested only 3, you can add the fourth condition too. This way, the code would only execute if the value is not null, I am using AND operator. Which evaluates only and only if all of the conditions are true.
Artur Vahe Karapekmez19 reputationPosted Mar 28, 2015, 8:12 AM EDT
My question is for example;
Textbox1.Text ="Hello";
Textbox2.Text =null;
Textbox3.Text ="World";
Textbox4.Text = null;
when textboxes like that;
Textbox5.Text= Hello World;
How can I do This.(Not allways Textbos2 and 4 will be null it will be empty according to users but at least 1 textbox will be not null.
Khargesh RajputRank #616 · 3.9K reputationPosted Mar 28, 2015, 8:41 AM EDT
Textbox2.text=" ";
Artur Vahe Karapekmez19 reputationPosted Mar 28, 2015, 8:52 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2015, 9:22 AM EDT
string temp = (Textbox1.Text == null) ? "" : TextBox1.Text + " ";
temp += (Textbox2.Text == null) ? "" : Textbox2.Text + " ";
temp += (Textbox3.Text == null) ? "" : Textbox3.Text + " ";
temp += (Textbox4.Text == null) ? "" : Textbox4.Text;
Textbox5.Text = temp.Trim();
Artur Vahe Karapekmez19 reputationPosted Mar 28, 2015, 9:39 AM EDT
Gowtham RajamanickamRank #57 · 29K reputationPosted Apr 5, 2015, 7:23 PM EDT
Subhashkumar YadavRank #1,792 · 832 reputationPosted Dec 16, 2016, 2:14 AM EST
Amar SrivastavaRank #1,375 · 1.2K reputationPosted Sep 14, 2017, 10:12 PM EDT