Pop UP alert message from c# page - c#

I need to show a pop up alert when some fileds have got not null values. I Used code piece like below , but not working correctly..Could you pls let me know the corrcet syntax for doing that?
if (txtSearchName.Text != "" || cmbSearchOaO.SelectedItem.Text != "" || cmbVessel.SelectedItem.Text != "" || cmbSearchApplicationType.SelectedItem.Text != "" || cmbSearchHull.SelectedItem.Text != "" || cmbSearchCategory.SelectedItem.Text != "" || cmbSearchHazardCategory.SelectedItem.Text != "")
{
ScriptManager.RegisterClientScriptBlock(btApplySearch, btApplySearch, "<script> alert('Inserted successfully');</script>", true);
}
Here btApplySearch is the buttonID used in aspx markup.

Try this:
if (txtSearchName.Text != "" || cmbSearchOaO.SelectedItem.Text != "" || cmbVessel.SelectedItem.Text != "" || cmbSearchApplicationType.SelectedItem.Text != "" || cmbSearchHull.SelectedItem.Text != "" || cmbSearchCategory.SelectedItem.Text != "" || cmbSearchHazardCategory.SelectedItem.Text != "")
{
RegisterDOMReadyScript("alert message", "alert('Message Here');");
}
Hope Its Helpful.

if not using Update panel then use below code
Page.ClientScript.RegisterStartupScript(this.GetType(),"Msg",alert("Javascript message"), true);
else
ScriptManager.RegisterStartupScript(this.GetType(),"Msg",alert("Javascript message"), true);

try the following code inside the if :
if (txtSearchName.Text != "" || cmbSearchOaO.SelectedItem.Text != "" || cmbVessel.SelectedItem.Text != "" || cmbSearchApplicationType.SelectedItem.Text != "" || cmbSearchHull.SelectedItem.Text != "" || cmbSearchCategory.SelectedItem.Text != "" || cmbSearchHazardCategory.SelectedItem.Text != "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "err", "alert('Message you want write');", true);
}
RegisterStartupScript has 5 parameters we used.
this:: referers to the control,
this.GetType() is used to get the type of control,
err is a string key.
alert('mesage') whatever you want to write,
true:: do you want to add script block in code or not if yes the true otherwise false
Hope this will work for you

Related

Any way to further reduce the number of lines of code?

I'm looking for ways to reduce the number of lines of code for this function. Any help is appreciated!
private bool CanSubmitPackage ( object obj )
{
// ---- Checks if the package contains any files to be submitted ----
if ( _selectedWorkspace.Package.Files != null )
{
if ( _selectedWorkspace.Package.Files.Count > 0 )
if ( _selectedWorkspace.Package.Platform != null || _selectedWorkspace.Package.Platform != "" )
if ( _selectedWorkspace.Package.PackagePath != null || _selectedWorkspace.Package.PackagePath != "" )
if ( _selectedWorkspace.Package.PackageSize != null || _selectedWorkspace.Package.PackageSize != "" )
if ( _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "" )
return true;
else
return false;
else
return false;
else
return false;
else
return false;
else
return false;
}
else
return false;
}
return ( _selectedWorkspace.Package.Files?.Count > 0
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));
You can make it shorter and readable by using && operator and string.IsNullOrEmpty method:
return ( _selectedWorkspace.Package.Files != null
&& _selectedWorkspace.Package.Files.Count > 0
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));
If you're using c#6+, then you can go further (just a little bit) and use Expression Bodied method:
private bool CanSubmitPackage () => //I'm not sure about object obj argument
( _selectedWorkspace.Package.Files != null
&& _selectedWorkspace.Package.Files.Count > 0
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));
If you are going after making your code text shorted, you can declare a short named variable and use that instead of _selectedWorkspace.Package:
var p = _selectedWorkspace.Package;
return (p.Files?.Count > 0
&& !string.IsNullOrEmpty(p.Platform)
&& !string.IsNullOrEmpty(p.PackagePath)
&& !string.IsNullOrEmpty(p.PackageSize)
&& !string.IsNullOrEmpty(p.SubmittedBy));;
This is one way to do it
Update
return ( _selectedWorkspace != null
&& _selectedWorkspace.Package != null
&& _selectedWorkspace.Package.Files != null
&& _selectedWorkspace.Package.Files.Count > 0
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackagePath)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.PackageSize)
&& !string.IsNullOrEmpty(_selectedWorkspace.Package.SubmittedBy));
Old Ans
return ( _selectedWorkspace.Package.Files != null
&& _selectedWorkspace.Package.Files.Count > 0
&& ( _selectedWorkspace.Package.Platform != null || _selectedWorkspace.Package.Platform != "" )
&& ( _selectedWorkspace.Package.PackagePath != null || _selectedWorkspace.Package.PackagePath != "" )
&& ( _selectedWorkspace.Package.PackageSize != null || _selectedWorkspace.Package.PackageSize != "" )
&& ( _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "" ));
This is another way to do it
private bool CanSubmitPackage(object obj)
{
// ---- Checks if the package contains any files to be submitted ----
if (_selectedWorkspace.Package.Files == null) return false;
if (_selectedWorkspace.Package.Files.Count <= 0) return false;
if (_selectedWorkspace.Package.Platform == null && _selectedWorkspace.Package.Platform == "") return false;
if (_selectedWorkspace.Package.PackagePath == null && _selectedWorkspace.Package.PackagePath == "") return false;
if (_selectedWorkspace.Package.PackageSize != _selectedWorkspace.Package.PackageSize != "")
return _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "";
return false;
}
I would prefer #xneg's answer (https://stackoverflow.com/a/53096740/10588170), however, you could also drop all 'else' Statements and replace them with a single return statement after the 'if'-block.
Make sure to use string.IsNullOrEmpty(_str) in future, or you have to use && instead of ||, otherwise you will get an Exception because you are checking if _str is empty if it is null and if it is not null, you are not checking the content of _str at all. I am pretty sure that that's not what you wanna do.
I would maybe split the conditions, for example:
public bool HasFile
{
get { return _selectedWorkspace.Package.Files != null && _selectedWorkspace.Package.Files.Count > 0; }
}
public bool HasPlatform
{
get { return !string.IsNullOrEmpty(_selectedWorkspace.Package.Platform); }
}
// Do that for all conditions
And then you can write your final if like this:
bool result = HasFile && HasPlatform && …
If you wanted to keep it largely the same, as the return for all of your IF statements failing is false, you only need to include it once. If any of your current checks fail, it won't be able to execute "return true", so you can just have one final return statement like this:
private bool CanSubmitPackage ( object obj )
{
// ---- Checks if the package contains any files to be submitted ----
if ( _selectedWorkspace.Package.Files != null )
if ( _selectedWorkspace.Package.Files.Count > 0 )
if ( _selectedWorkspace.Package.Platform != null || _selectedWorkspace.Package.Platform != "" )
if ( _selectedWorkspace.Package.PackagePath != null || _selectedWorkspace.Package.PackagePath != "" )
if ( _selectedWorkspace.Package.PackageSize != null || _selectedWorkspace.Package.PackageSize != "" )
if ( _selectedWorkspace.Package.SubmittedBy != null || _selectedWorkspace.Package.SubmittedBy != "" )
return true;
// One of the above checks failed, that's why we're running this line
return false;
}
I also prefer #xneg, #SeM, or #Mihir Dave's approach (as they're all basically the same answer), but it does come down to what you find most comfortable / readable. I would definitely use the method String.IsNullOrEmpty() to simplify your if statements however.
Additionally, what the ? does in xneg's answer: _selectedWorkspace.Package.Files?.Count is say "If Files is not null, retrieve Files.Count, otherwise return null. Then in C# a statement like, null > 0 always returns false.

How to set conditional filter in where clause using linq?

I have a bolean condition called accessories
If it is true then I want to make sure that a cell value is not empty
otherweise it can be empty. I have the following:
var items = (from a in allRowsrows
where accessories == true ? a["MASTERID"].ToString() != "": a["MASTERID"].ToString() == "" &&
a["ITEMNO"].ToString() != a["NEWITEMNO"].ToString() ||
a["LABEL"].ToString() != a["NEWVISITEMNO"].ToString()
select new ItemsToUpdate
{
isAccessory = accessories,
Item = a
}
).ToList();
Problem:
Well simply put it does not filter correctly when using the above.
ITEMNO cell, `NEWITEMNO` cell are the same , LABEL cell and `NEWVISITEMNO` cell are also the same
and this should then give me a list with 0 items but I am still getting items in my list.
What am I doing wrong?
EDIT:
Modified code:
var items = (from a in allRows
where accessories == true ? a["MASTERID"].ToString() != "": true &&
(a["ITEMNO"].ToString() != a["NEWITEMNO"].ToString()) ||
(a["LABEL"].ToString() != a["NEWVISITEMNO"].ToString())
select new ItemsToUpdate
{
isAccessory = accessories,
Item = a
}
).ToList();
Still wrong filtering.
Here is why you are not getting the correct results. This condition:
where accessories == true ? a["MASTERID"].ToString() != "": a["MASTERID"].ToString() == "" &&
a["ITEMNO"].ToString() != a["NEWITEMNO"].ToString() ||
a["LABEL"].ToString() != a["NEWVISITEMNO"].ToString()
means this:
if accessories is true, then check if MASTERID is not empty. That is it. But, and this is where the bug is, if accessories is false, check this whole condition below:
a["MASTERID"].ToString() == "" &&
a["ITEMNO"].ToString() != a["NEWITEMNO"].ToString() ||
a["LABEL"].ToString() != a["NEWVISITEMNO"].ToString()
which is evaluated like this:
(a["MASTERID"].ToString() == "" &&
a["ITEMNO"].ToString() != a["NEWITEMNO"].ToString()
)
OR
a["LABEL"].ToString() != a["NEWVISITEMNO"].ToString()
In other words, if MASTERID is empty and ITEMNO is not the same as NEWITEMNO, it will never check LABEL.
Your edited code in your question makes no difference, it still means the same thing as above.
Now your question is why Enigmativity's answer gives you the correct results. Here is how he has it:
where accessories ? a["MASTERID"].ToString() != "" : true
where a["ITEMNO"].ToString() != a["NEWITEMNO"].ToString() || a["LABEL"].ToString() != a["NEWVISITEMNO"].ToString()
which means this:
If accessories is true, then check if MASTERID is not empty. Otherwise use the value true. Also, check to make sure ITEMNO is not the same as NEWITEMNO OR LABEL is not the same as NEWVISITITEMNO.
So essentially it is doing this:
(accessories ? a["MASTERID"].ToString() != "" : true)
&&
(
a["ITEMNO"].ToString() != a["NEWITEMNO"].ToString() ||
a["LABEL"].ToString() != a["NEWVISITEMNO"].ToString()
)
I don't know your full requirements but I am afraid even Enigmativity's answer may produce the wrong results (or may not). It all depends on when you will hit the specific case. Perhaps, my explanation can guide you.
Is this what you're after?
var items =
(
from a in allRowsrows
where accessories ? a["MASTERID"].ToString() != "" : true
where a["ITEMNO"].ToString() != a["NEWITEMNO"].ToString() || a["LABEL"].ToString() != a["NEWVISITEMNO"].ToString()
select new ItemsToUpdate
{
isAccessory = accessories,
Item = a
}
).ToList();
Did you try to use brackets in your where clause?
var items = (from a in allRows
where (accessories == true ? a["MASTERID"].ToString() != "": true &&
(a["ITEMNO"].ToString() != a["NEWITEMNO"].ToString()) ||
(a["LABEL"].ToString() != a["NEWVISITEMNO"].ToString()))
select new ItemsToUpdate
{
isAccessory = accessories,
Item = a
}
).ToList();

1 Button 4 Comboboxes On Win Form

I have 1 button that will execute a stored procedure based off which combobox has data in it. Are multiple if statements the best course for me to account for each combobox scenario? I currently have my code like so - which works, but is a bit slow. Is there a better way to write this syntax using C# and VS2013?
private void btn1_Click()
{
if (cbo1.Text.ToString() == "" && cbo2.Text.ToString() == "" && cbo3.Text.ToString() == "" && cbo4.Text.ToString() == "" )
{
MessageBox.Show("You failed to make a selection.");
return;
}
if (cbo1.Text.ToString() != "" && cbo2.Text.ToString() == "" && cbo3.Text.ToString() == "" && cbo4.Text.ToString() == "" )
{
//Go route1
}
if (cbo1.Text.ToString() == "" && cbo2.Text.ToString() 1= "" && cbo3.Text.ToString() == "" && cbo4.Text.ToString() == "" )
{
//Go route2
}
if (cbo1.Text.ToString() == "" && cbo2.Text.ToString() == "" && cbo3.Text.ToString() != "" && cbo4.Text.ToString() == "" )
{
//Go route3
}
if (cbo1.Text.ToString() == "" && cbo2.Text.ToString() == "" && cbo3.Text.ToString() == "" && cbo4.Text.ToString() != "" )
{
//Go route4
}
}
EDIT
#MethodMan --> is this how you would go about setting up the check?
var comboBoxes = this.Controls
.OfType<ComboBox>()
.Where(x => x.Name.StartsWith("comboBox"));
foreach(var cmbBox in comboBoxes)
{
(string.IsNullOrEmpty(cmbBox.Text)) || if (cmbBox.SelectedIndex == -1)
{
//How to find which combobox is cmbBox
}
}
my suggestion will be something like this:
private void btn1_Click()
{
int data = 0;
if(cbo1.Text.ToString() != "")
data+=1;
if(cbo2.Text.ToString() != "")
data+=2;
if(cbo3.Text.ToString() != "")
data+=4;
if(cbo4.Text.ToString() != "")
data+=8;
switch(data)
{
case 1:
//Go route1
break;
case 2:
//Go route2
break;
case 4:
//Go route3
break;
case 8:
//Go route4
break;
default:
MessageBox.Show("You failed to make a selection.");
break;
}
}
i`m not sure it will do the job you want but it is a lot faster and this way you can check wich combo the user selected and wich combo he did not

How to make so that you can only enter a date?

So I need to do so in the "Year", you can enter the date only.
To pop up a warning something.
Here's the code, if the user enters an empty value, you need to add?
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "")
{
MessageBox.Show("Не заполнены все поля!");
return;
}
And why in the "Year" the whole date? Plug the NumericUpDown to the limitations of values and the whole business.)))
Put together textBox dateTimePicker, further:
string date=dateTimePicker1.Value.ToString ("dd.mm.yy")

How to break if-else-if using C#

How do I break if-else-if.....Why its not working? its just checking all the conditions instead of performing the tasks. following is my code. I have checked it through breakpoints its moving to all conditions why it doesn't get stop after meeting the correct condition. even it is not going into the if activity it just read all the conditions and do nothing at the end.
private void ShowHash()
{
inpic = pb_selected.Image;
Bitmap image = new Bitmap(inpic);
byte[] imgBytes = new byte[0];
imgBytes = (byte[])converter.ConvertTo(image, imgBytes.GetType());
string hash = ComputeHashCode(imgBytes);
txt_selectedText.Text = hash;
GetHash();
}
private void GetHash()
{
if (txt_sel1.Text == null && (txt_sel2.Text == null || txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null ))
{
txt_sel1.Text = txt_selectedText.Text;
return;
}
else if (txt_sel1.Text != null && (txt_sel2.Text == null || txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null))
{
txt_sel2.Text = txt_selectedText.Text;
return;
}
else if (txt_sel2.Text != null && (txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null))
{
txt_sel3.Text = txt_selectedText.Text;
return;
}
else if (txt_sel3.Text != null && (txt_sel4.Text == null || txt_sel5.Text == null))
{
txt_sel4.Text = txt_selectedText.Text;
return;
}
else if (txt_sel4.Text != null && (txt_sel5.Text == null))
{
txt_sel5.Text = txt_selectedText.Text;
return;
}
}
I strongly suspect the problem is that the Text property is never null for any of txt_sel*. Assuming these are text boxes in a UI, it's much more likely that if there's no text in the text box, the Text property will return "" instead of null. That's the way most UI frameworks handle empty controls.
I would also suggest extracting the conditions into local variables first:
bool hasSel1 = txt_sel1.Text != "";
bool hasSel2 = txt_sel2.Text != "";
bool hasSel3 = txt_sel3.Text != "";
bool hasSel4 = txt_sel4.Text != "";
bool hasSel5 = txt_sel5.Text != "";
if (!hasSel1 && (!hasSel2 || !hasSel3 || !hasSel4 || !hasSel5)
{
...
}
And ideally, give your controls more meaningful names - a collection of variables with the same prefix but then a numeric suffix is very rarely a good idea, in terms of readability.
Reason:
If there is nothing in those textboxes, textbox.Text will return an empty string ("") not null.
Solution:
Check against "" not null:
private void GetHash()
{
if (txt_sel1.Text == "" && (txt_sel2.Text == "" || txt_sel3.Text == "" || txt_sel4.Text == "" || txt_sel5.Text == ""))
{
txt_sel1.Text = txt_selectedText.Text;
return;
}
else if (txt_sel1.Text != "" && (txt_sel2.Text == "" || txt_sel3.Text == "" || txt_sel4.Text == "" || txt_sel5.Text == ""))
{
txt_sel2.Text = txt_selectedText.Text;
return;
}
....
....
EDIT: You don't have to do ==true for boolean variables. If statement checks it against true by default. Use ! to check against false:
if (hasValue1 && (hasValue2 || hasValue3 || hasValue4 || hasValue5))
{
txt_sel1.Text = txt_selectedText.Text;
return;
}
else if (hasValue2 && (!hasValue1 ||hasValue3 || hasValue4 || hasValue5))
{
txt_sel2.Text = txt_selectedText.Text;
return;
}
else if (hasValue3 && (!hasValue1 || hasValue2 || hasValue4 || hasValue5))
{
txt_sel3.Text = txt_selectedText.Text;
return;
}
....
....
I think for string better to use inbuild null and whitespace check function:
bool hasValue1 = string.IsNullOrWhiteSpace(txt_sel1.Text);
bool hasValue2= string.IsNullOrWhiteSpace(txt_sel2.Text);
bool hasValue3= string.IsNullOrWhiteSpace(txt_sel3.Text);
bool hasValue4= string.IsNullOrWhiteSpace((txt_sel4.Text);
bool hasValue5 = string.IsNullOrWhiteSpace(txt_sel5.Text);
And then define if condition on these bool. This case can handle unexpected null or white space values.
That is bacause it probably doesn't find any of those conditions true.
If it would find a true condition it would execute that if block and won't event check the others.
To add to what others have said, you should really have a final else statement in there to catch issues such as this:
else
{
throw new InvalidOperationException("My If Statement is Broken");
}
Thanks to you all! My problem is solved by changing the conditions and your suggested alteration, following is the code may be helped some beginner like me.
private void GetHash()
{
bool hasValue1 = string.IsNullOrWhiteSpace(txt_sel1.Text);
bool hasValue2 = string.IsNullOrWhiteSpace(txt_sel2.Text);
bool hasValue3 = string.IsNullOrWhiteSpace(txt_sel3.Text);
bool hasValue4 = string.IsNullOrWhiteSpace(txt_sel4.Text);
bool hasValue5 = string.IsNullOrWhiteSpace(txt_sel5.Text);
if (hasValue1 && (hasValue2 || hasValue3 || hasValue4 || hasValue5))
{
txt_sel1.Text = txt_selectedText.Text;
return;
}
else if (hasValue2 && (!hasValue1 ||hasValue3 || hasValue4 || hasValue5 ))
{
txt_sel2.Text = txt_selectedText.Text;
return;
}
else if (hasValue3 && (!hasValue1 || !hasValue2 || hasValue4 || hasValue5 ))
{
txt_sel3.Text = txt_selectedText.Text;
return;
}
else if (hasValue4 && (!hasValue1 || !hasValue2 || !hasValue3 || hasValue5 ))
{
txt_sel4.Text = txt_selectedText.Text;
return;
}
else if (hasValue5 && (!hasValue1 || !hasValue2 || !hasValue3 || !hasValue4 ))
{
txt_sel5.Text = txt_selectedText.Text;
return;
}
else
{
CompareHash();
}
}

Categories