Response.Redirect is not functioning [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to redirect with this
code:
protected void btnLogin_Click(object sender, EventArgs e)
{
User user = ConnectionClass.LoginUser(txtUsername.Text, txtPassword.Text);
if (user != null)
{
//Store login variables in session
Session["login"] = user.Username;
Session["user_type"] = user.Type;
var userType = (string)Session["user_type"];
if (userType == "user" && userType == "special_user")
{
Response.Redirect("~/Pages/Customers/Quotation_Customers.aspx");
}
}
else
{
lblError.Text = "Login Failed";
}
}
that is suppose to redirect me to another web page,but the code do not redirect
at all.

if (userType == "user" && userType == "special_user")
This cannot possibly be true. The string cannot be equal to two different strings. Did you mean OR (||) instead?

Related

C# - How do I put the Function Regex working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Good morning,
I am doing a page for my intership and I found this function but it isn't working, can someone help?
Thank you in advance.
string _id = this.txtIdGrupo.Text;
if (!Regex.IsMatch(_id, #"^\d+$"))
return false;
The output says this:
error CS0103: The name 'Regex' does not exist in the current context
this is not an issue with the bit of code you showed.
This is a namespace issue at the very top of your file.
The solution should be https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
var isNumeric1 = IsNumeric("1");
Console.WriteLine(isNumeric1);
var isNumeric2 = IsNumeric("HelloWorld");
Console.WriteLine(isNumeric2);
//call IsNumeric with the value of this.txtIdGrupo.Text like this
//var isNumeric = IsNumeric(this.txtIdGrupo.Text);
}
private static bool IsNumeric(string str)
{
string id = str;
if (!Regex.IsMatch(id, #"^\d+$"))
return false;
return true;
}
}
See it in action:
https://dotnetfiddle.net/I7cAKs
notice the "using System.Text.RegularExpressions"
Using tools like Resharper and similar will definitely give you hints and will improve your day to day !
Make sure you have the System.Text.RegularExpressions
is imported

What's wrong? I can't seem to close this code out [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I cant figure out how to close this out properly, no matter where i add a bracket it breaks the code.
I appreciate the help, thanks :)
int NewID = Convert.ToInt32(Adapter.InsertQuery()); // new relationship id
if (!Session.GetHabbo().Relationships.ContainsKey(Them))
Session.GetHabbo().Relationships.Add(Them, new Relationship(NewID, Them, 3)); // create the relationship
Session.GetHabbo().GetMessenger().UpdateFriend(Them, Session, true);
}
else
{
Habbo Habbo = PlusEnvironment.GetHabboById(Them);
if (Habbo != null)
{
MessengerBuddy Bud = null;
if (Session.GetHabbo().GetMessenger().TryGetFriend(Them, out Bud))
Session.SendMessage(new FriendListUpdateComposer(Session, Bud));
}
}
return false;
}
P.S I'm still new to this.
This seems like a simple fix, but if you format your code this sort of stuff usually sticks out. In Visual Studio you can hit Ctrl+K, Ctrl+D (in succession) and it'll format your code to the default.
int NewID = Convert.ToInt32(Adapter.InsertQuery()); // new relationship id
if (!Session.GetHabbo().Relationships.ContainsKey(Them))
Session.GetHabbo().Relationships.Add(Them, new Relationship(NewID, Them, 3)); // create the relationship
Session.GetHabbo().GetMessenger().UpdateFriend(Them, Session, true);
}
else
{
Habbo Habbo = PlusEnvironment.GetHabboById(Them);
if (Habbo != null)
{
MessengerBuddy Bud = null;
if (Session.GetHabbo().GetMessenger().TryGetFriend(Them, out Bud))
Session.SendMessage(new FriendListUpdateComposer(Session, Bud));
}
}
return false;
}
Alright. Now that that's done. You're, in general, missing your beginning brackets on your if-statements.
int NewID = Convert.ToInt32(Adapter.InsertQuery()); // new relationship id
if (!Session.GetHabbo().Relationships.ContainsKey(Them))
{ // Added here
Session.GetHabbo().Relationships.Add(Them, new Relationship(NewID, Them, 3)); // create the relationship
Session.GetHabbo().GetMessenger().UpdateFriend(Them, Session, true);
}
else
{
Habbo Habbo = PlusEnvironment.GetHabboById(Them);
if (Habbo != null)
{
MessengerBuddy Bud = null;
if (Session.GetHabbo().GetMessenger().TryGetFriend(Them, out Bud))
{ // Added here.
Session.SendMessage(new FriendListUpdateComposer(Session, Bud));
}
}
return false;
}
So if you add those two brackets noted by the // Added here comments, that should work.

Error code - non static method requires a target [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My problem is an error while executing this code. This function work perfectly into an other utilisation.
This is my complete function code :
public List<t_heure_indemnite> GetAllHeureIndemnite(FirmMda f, bool justeActive)
{
List<t_heure_indemnite> lHi = new List<t_heure_indemnite>();
ce = new callistoEntities();
try
{
var load = from i in ce.t_heure_indemnite
where i.FIRM == f.Name
select i;
if (load != null)
{
List<t_heure_indemnite> liste = load.ToList();
foreach (t_heure_indemnite h in liste)
{
lHi.Add(h);
}
}
}
catch (Exception e)
{
MsgBox.Show(globale.AfficheErreur(e));
}
if (justeActive)
return lHi.Where(i => i.ACTIVE == true).OrderBy(j => j.ORE).ToList();
else
return lHi.OrderBy(j => j.ORDRE).ToList();
}
Exception is present while executing : Non static method requires a target.
It appears when load.ToList is called.
It's called into public method and during an import traitement into an other list.
I don't know where is problem. Can you help me please ?
It can happen when your you use a null-reference variable in a lambda expression. I would suggest to check if your variable ce.t_heure_indemnite is a null-reference.
if(ce.t_heure_indemnite != null)

Check Public Static Bool in C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
im kinda new to C# and i got a code that uses public static bool.
But how do i check it?
I tried to do this
public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
{
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
}
catch
{
return false;
}
}
private async void Form1_Load(object sender, EventArgs e)
{
await Task.Delay(5000);
if (CheckForInternetConnection() = true)
{
}
}
And it gave me an error.
I confirm the comments: it's only a syntax error due to the lack of the 2nd = char in the if(...) conditional block. I've tested your code in a console project on the run and, with the 2nd = it works fine.

String comparison with StartsWith or Substring does not work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to find what attachment i downloaded from email message, but im not able to compare the attachment filename with string, what im doing wrong ? Script should return "in in" but it returns "out out"
FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;
Console.WriteLine(fileAttachment.Name);
if (fileAttachment.FileName.StartsWith("OpenOrders")) {
Console.WriteLine("in");
}
else {
Console.WriteLine("out");
}
if (fileAttachment.FileName.Substring(0, 10) == "OpenOrders") {
Console.WriteLine("in");
} else {
Console.WriteLine("out");
}
Output:
OpenOrders some text with spaces.xlsx
out
out
You are outputting fileAttachment.Name but using fileAttachment.FileName in the StartsWith. Use the correct version and it should work.
FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;
Console.WriteLine(fileAttachment.Name);
if (fileAttachment.Name.StartsWith("OpenOrders")) {
Console.WriteLine("in");
}
else {
Console.WriteLine("out");
}
if (fileAttachment.Name.Substring(0, 10) == "OpenOrders") {
Console.WriteLine("in");
} else {
Console.WriteLine("out");
}
You're outputting the Name property:
Console.WriteLine(fileAttachment.Name);
But your StartsWith and Substring calls are against theFileName property. I suspect you'll find that Name is returning something different to FileName.

Categories