If Username == Anton, then do something - c#

how can i do so if my username are Anton, then show, if not then dont show.
I already tried the following thing.
if(Session["username"] == "Anton")
{
btnNew.Visible = true;
}
else
{
btnNew.Visible = false;
}

if(!string.IsNullOrEmpty((string)Session["username"]) && (string)Session["username"] == "Anton")
{
btnNew.Visible = true;
}
else
{
btnNew.Visible = false;
}

Related

If Session match string

I got a little problem. I got a if statement which says if Session isn't equal 3, then do something, and if that isn't true, then do something else. My problem is just, that it isn't working proberly.
I've already tried:
1)
if (Session["userrank"] != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
2)
if (Session["userrank"].ToString() != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
3)
if ((string)Session["userrank"] != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
4)
if (((string)Session["userrank"]) != "3")
{
pnlAdmin.Visible = false;
}
else
{
pnlAdmin.Visible = true;
}
but none of them seems to work. And i have already checked if there's a Session called userrank that is getting the result 3.
sorry for the "stupid" question. I'm kind of new to C# & ASP.net.
Best Regards,
Anton
Your code sets pnlAdmin.Visible = false; if whatever is in Session["userrank"] is not 3.
It sets pnlAdmin.Visible = true; if whatever is in Session["userrank"] is 3.
You said it is 3; therefore, the panel should be visible. And that seems to be what is happening.

How do i Check if a xml Sibling has attributes?

My program is doing weird stuff if i delete one of my Attributes because it is not able to handle Siblings without Attributes. Now i googled for a while but i am not able to find a good way to check for attributes.
Whats the way you prefer checking for attributes?
while (FXMLNode != null)
{
if (FXMLNode.Name.ToLower() == "datei")
{
xmlInformationen oInfo = new xmlInformationen();
oInfo.Dateipfad = FXMLNode.InnerText;
if (FXMLNode.Attributes["checked"].Value.ToString() == "true")
oInfo.CheckBox = true;
else if (FXMLNode.Attributes["checked"].Value.ToString() == "false")
oInfo.CheckBox = false;
else if(FXMLNode == null)
oInfo.CheckBox = true;
else
oInfo.CheckBox = true;
lstAttribute.Add(oInfo);
iCounter++;
if (FXMLNode.NextSibling == null)
{
FXMLNode = FXMLNode.FirstChild;
}
else
{
FXMLNode = FXMLNode.NextSibling;
}
}
else
{
if (FXMLNode.NextSibling == null)
{
FXMLNode = FXMLNode.FirstChild;
}
else
{
FXMLNode = FXMLNode.NextSibling;
}
}
}
You are accessing the value of an attribute without knowing if the attribute exists or not. Rewrite your code to check for the attribute first:
oInfo.CheckBox = true;
if(FXMLNode == null) oInfo.CheckBox = true; //not sure why you set it to true here
else if (FXMLNode.HasAttribute("checked"))
{
if (FXMLNode.Attributes["checked"].Value.ToString() == "true")
oInfo.CheckBox = true;
else if (FXMLNode.Attributes["checked"].Value.ToString() == "false")
oInfo.CheckBox = false;
}
Please note that checking if the Xml element is null should be the first thing you do. If it's null then it surely won't have any attributes but you'll have an exception.
you can just check thats the attribute in not null like this
if (FXMLNode.Attributes["checked"]!=null)
and then check the value

Find out User cannot change password value of ldap

I am trying to find out that in ad, user has allowed to change password or not.
I have used SearchResponse to find out that user exists or not.
SearchResponse response = (SearchResponse)connection.SendRequest(request);
DirectoryAttribute attribute = response.Entries[0].Attributes["ntSecurityDescriptor"];
if (attribute != null)
{
const string PASSWORD_GUID = "{ab721a53-1e2f-11d0-9819-00aa0040529b}";
const int ADS_ACETYPE_ACCESS_DENIED_OBJECT = 6;
bool fEveryone = false;
bool fSelf = false;
ActiveDs.ADsSecurityUtility secUtility = new ActiveDs.ADsSecurityUtility();
ActiveDs.IADsSecurityDescriptor sd = (IADsSecurityDescriptor)secUtility.ConvertSecurityDescriptor((byte[])attribute[0], (int)ADS_SD_FORMAT_ENUM.ADS_SD_FORMAT_RAW, (int)ADS_SD_FORMAT_ENUM.ADS_SD_FORMAT_IID);
ActiveDs.IADsAccessControlList acl = (ActiveDs.IADsAccessControlList)sd.DiscretionaryAcl;
foreach (ActiveDs.IADsAccessControlEntry ace in acl)
{
if ((ace.ObjectType != null) && (ace.ObjectType.ToUpper() == PASSWORD_GUID.ToUpper()))
{
if ((ace.Trustee == "Everyone") && (ace.AceType == ADS_ACETYPE_ACCESS_DENIED_OBJECT))
{
fEveryone = true;
}
if ((ace.Trustee == #"NT AUTHORITY\SELF") && (ace.AceType == ADS_ACETYPE_ACCESS_DENIED_OBJECT))
{
fSelf = true;
}
break;
}
}
if (fEveryone || fSelf)
{
return Global.RequestContants.CANT_CHANGE_PASSWORD;
}
else
{
return string.Empty;
}
}

Data list binding with some text and validating datalist

I am using this code for datalist validation.
I am binding the image in datalist after that i am trying to give the caption to each image suppose there is 3 image is datalist then for the first image i am able to give but for the other one i am unable to.. i think there is some error in conditions help me in this. code is following..
if (DataList1.Items.Count == 0)
{
msgError.Text = "Please add images and captions for each image";
msgError.Focus();
}
else
AddCaption();
bool IsEmptyCaption = false;
Hashtable htble = (Hashtable)ViewState["imgIdCapHtbl"];
List<int> imgIds = (List<int>)ViewState["imgIds"];
if (htble != null && imgIds != null)
{
foreach (int id in imgIds)
{
if (htble[id] == "")
{
IsEmptyCaption = true;
break;
}
else
IsEmptyCaption = false;
}
}
else
IsEmptyCaption = true;
if (DataList1.Items.Count == 0)
{
msgError.Text = "Please add images";
msgError.Focus();
}
else if (IsEmptyCaption)
{
msgError.Text = "Please add captions for each image";
msgError.Focus();
}
else
{
Args[0] = "Section1";
Args[1] = "";
Args[2] = FindingId.ToString();
Args[3] = FindingIdIns.ToString();
AnotherHeading = false;
//AddCaption();
objGetBaseCase.UpdateImageCaptions((Hashtable)ViewState["imgIdCapHtbl"]);
if (AddFindingsViewerDetails != null)
AddFindingsViewerDetails(Args, e);
ClearImages();
PageContent pg = (PageContent)this.Parent.FindControl("PageContent");
if (pg != null)
pg.LoadWorkflowForCase();
if (Display != null)
Display(null, EventArgs.Empty);
}
if (DataList1.Items.Count == 0)
{
msgError.Text = "Please add images and captions for each image";
msgError.Focus();
}
else
AddCaption();
bool IsEmptyCaption = false;
Hashtable htble = (Hashtable)ViewState["imgIdCapHtbl"];
List<int> imgIds = (List<int>)ViewState["imgIds"];
if (htble != null && imgIds != null)
{
foreach (int id in imgIds)
{
if (htble[id] == "" || htble[id] == null) // New code implemented here
{
IsEmptyCaption = true;
break;
}
else
IsEmptyCaption = false;
}
}
else
IsEmptyCaption = true;
if (DataList1.Items.Count == 0)
{
msgError.Text = "Please add images";
msgError.Focus();
}
else if (IsEmptyCaption)
{
msgError.Text = "Please add captions for each image";
msgError.Focus();
}
else
{
Args[0] = "Section1";
Args[1] = "";
Args[2] = FindingId.ToString();
Args[3] = FindingIdIns.ToString();
AnotherHeading = false;
//AddCaption();
objGetBaseCase.UpdateImageCaptions((Hashtable)ViewState["imgIdCapHtbl"]);
if (AddFindingsViewerDetails != null)
AddFindingsViewerDetails(Args, e);
ClearImages();
PageContent pg = (PageContent)this.Parent.FindControl("PageContent");
if (pg != null)
pg.LoadWorkflowForCase();
if (Display != null)
Display(null, EventArgs.Empty);
}
Now its working fine...

Refactoring an If else tree

I have an if else tree that is going to grow as I add additional items for it to maintain and I'm looking at the best way to write it for maintainability I'm starting with this code
private void ControlSelect()
{
if (PostingType == PostingTypes.Loads && !IsMultiPost)
{
singleLoadControl.Visible = true;
singleTruckControl.Visible = false;
multiTruckControl.Visible = false;
multiLoadControl.Visible = false;
}
else if (PostingType == PostingTypes.Trucks && !IsMultiPost)
{
singleLoadControl.Visible = false;
singleTruckControl.Visible = true;
multiTruckControl.Visible = false;
multiLoadControl.Visible = false;
}
else if (PostingType == PostingTypes.Loads && IsMultiPost)
{
singleLoadControl.Visible = false;
singleTruckControl.Visible = false;
multiTruckControl.Visible = false;
multiLoadControl.Visible = true;
}
else if (PostingType == PostingTypes.Trucks && IsMultiPost)
{
singleLoadControl.Visible = false;
singleTruckControl.Visible = false;
multiTruckControl.Visible = true;
multiLoadControl.Visible = false;
}
}
and thinking of re-factoring it to something like this
private void ControlSelect()
{
List<UserControl> controlList = GetControlList();
string visableControl = singleLoadControl.ID;
if (PostingType == PostingTypes.Loads && !IsMultiPost)
{
visableControl = singleLoadControl.ID;
}
else if (PostingType == PostingTypes.Trucks && !IsMultiPost)
{
visableControl = singleTruckControl.ID;
}
else if (PostingType == PostingTypes.Loads && IsMultiPost)
{
visableControl = multiLoadControl.ID;
}
else if (PostingType == PostingTypes.Trucks && IsMultiPost)
{
visableControl = multiTruckControl.ID;
}
foreach (UserControl userControl in controlList)
{
userControl.Visible = (userControl.ID == visableControl);
}
}
private List<UserControl> GetControlList()
{
List<UserControl> controlList = new List<UserControl>
{
singleLoadControl,
multiTruckControl,
singleTruckControl,
multiLoadControl
};
return controlList;
}
I take a performance hit but I can manage all of my controls is a single place
my other thought was to make each selected control block it own method, something like this
private void SetSingleLoadControlAsSelected()
{
singleLoadControl.Visible = true;
singleTruckControl.Visible = false;
multiTruckControl.Visible = false;
multiLoadControl.Visible = false;
}
I don't take a performance hit but I'm maintaining the controls in multiple location
I'm leaning for option one just because I like maintainability aspect of it.
what about
singleLoadControl.Visible =
PostingType == PostingTypes.Loads && !IsMultiPost;
singleTruckControl.Visible =
PostingType == PostingTypes.Trucks && !IsMultiPost;
multiTruckControl.Visible =
PostingType == PostingTypes.Loads && IsMultiPost;
multiLoadControl.Visible =
PostingType == PostingTypes.Trucks && IsMultiPost;
if you want capability to make multiple controls visible (or add more enumerated values) decorate the enum with [Flags] attribute as follows:
[Flags]
public enum PostTyp { None=0, IsMultiPost = 1, Loads = 2, Trucks = 4 }
and modify Code as follows:
singleLoadControl.Visible =
((PostingType & (PostTyp.Loads | ~PostTyp.MultiCast))
== PostingType );
singleTruckControl.Visible =
((PostingType & (PostTyp.Trucks | ~PostTyp.MultiCast))
== PostingType );
multiTruckControl.Visible =
((PostingType & (PostTyp.Loads | PostTyp.MultiCast))
== PostingType );
multiLoadControl.Visible =
((PostingType & (PostTyp.Trucks | PostTyp.MultiCast))
== PostingType );
As you appear to be using an enumeration, I would recommend a switch with a default case to cope with unknown values. I believe this approach makes intentions clearer than doing all the checking in the assignment.
switch (PostingType)
{
case PostingTypes.Loads:
singleLoadControl.Visible = !IsMultiPost;
multiTruckControl.Visible = IsMultiPost;
singleTruckControl.Visible = false;
multiTruckLoadControl.Visible = false;
break;
case PostingTypes.Trucks:
singleLoadControl.Visible = false;
multiTruckControl.Visible = false;
singleTruckControl.Visible = !IsMultiPost;
multiLoadControl.Visible = IsMultiPost;
break;
default:
throw InvalidOperationException("Unknown enumeration value.");
}
What about this:
singleLoadControl.Visible = false;
singleTruckControl.Visible = false;
multiTruckControl.Visible = false;
multiLoadControl.Visible = false;
if (PostingType == PostingTypes.Loads && !IsMultiPost)
{
singleLoadControl.Visible = true;
}
else if (PostingType == PostingTypes.Trucks && !IsMultiPost)
{
singleTruckControl.Visible = true;
}
else if (PostingType == PostingTypes.Loads && IsMultiPost)
{
multiLoadControl.Visible = true;
}
else if (PostingType == PostingTypes.Trucks && IsMultiPost)
{
multiTruckControl.Visible = true;
}
If it would be otherwise sensible (for example, if these are already domain-specific custom controls), you could encapsulate the logic inside the controls themselves (Replace Conditional with Polymorphism). Perhaps create an interface like this:
public interface IPostingControl {
void SetVisibility(PostingType postingType, bool isMultiPost);
}
Then each control would be responsible for its own visibility rules:
public class SingleLoadControl: UserControl, IPostingControl {
// ... rest of the implementation
public void SetVisibility(PostingType postingType, bool isMultiPost) {
this.Visible = postingType == PostingType.Load && !isMultiPost);
}
}
Finally, in your page, just iterate over your IPostingControls and call SetVisibility(postingType, isMultiPost).
If you are expecting to be adding a lot of different parameters, you could create a multi-dimensional array of objects
Arr[0][0] = singleLoadControl
Arr[0][1] = singleTruckControl
Arr[1][0] = multiLoadControl
Arr[1][1] = multiTruckControl
This is pretty scary, but makes for simpler if statements. If you're going to have loads of references to the controls anyhow, I'd rather use a code-based representation of what those loads are. Such an array can be wrapped in a class to let you access the elements using something more like:
ControlClassInstance.single.truck
You'd have code like this:
p1 = IsMultiPost ? ControlClassInstance.multi : ControlClassInstance.single
p2 = p1[PostingType] //(this call would mean adding an indexer)
This kind of solution is way too sophisticated unless you expect things to get complicated...and might be poor then, too.
singleLoadControl.Visible = false;
singleTruckControl.Visible = false;
multiTruckControl.Visible = false;
multiLoadControl.Visible = false;
singleLoadControl.Visible = (PostingType == PostingTypes.Loads && !IsMultiPost);
singleTruckControl.Visible = (PostingType == PostingTypes.Trucks && !IsMultiPost);
multiLoadControl.Visible = (PostingType == PostingTypes.Loads && IsMultiPost);
multiTruckControl.Visible = (PostingType == PostingTypes.Trucks && IsMultiPost);
Have you considered the State Pattern?
private void ControlSelect()
{
if (PostingType == PostingTypes.Loads && !IsMultiPost)
{
singleLoadControl.Visible = true;
singleTruckControl.Visible = false;
multiTruckControl.Visible = false;
multiLoadControl.Visible = false;
return;
}
if (PostingType == PostingTypes.Trucks && !IsMultiPost)
{
singleLoadControl.Visible = false;
singleTruckControl.Visible = true;
multiTruckControl.Visible = false;
multiLoadControl.Visible = false;
return;
}
if (PostingType == PostingTypes.Loads && IsMultiPost)
{
singleLoadControl.Visible = false;
singleTruckControl.Visible = false;
multiTruckControl.Visible = false;
multiLoadControl.Visible = true;
return;
}
if (PostingType == PostingTypes.Trucks && IsMultiPost)
{
singleLoadControl.Visible = false;
singleTruckControl.Visible = false;
multiTruckControl.Visible = true;
multiLoadControl.Visible = false;
return;
}
}

Categories