user3266908 user3266908 - 1 year ago 135
ASP.NET (C#) Question

SQL command will not redirect

I created this slice of code but not behaving the way that it should.

What should happen is when the query returns a matching value in the database, it should redirect the page. But no matter what I do, it always returns the value as false, and never redirects correctly.

At least that what I think is going on, any help would be great..

I've been working on this for 3 days and I'm about to lose my mind. If there is a better way of doing this I am all ears.

var SuserId = User.Identity.GetUserId();

SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;

cmd.CommandText = "select Count(ID) from AspNetStripeUsers where ID=@ID";
cmd.Parameters.AddWithValue("@ID", SuserId);

int result = (int)cmd.ExecuteScalar();

string wewe = result.ToString();

if (wewe == SuserId)

Answer Source

This int result = (int)cmd.ExecuteScalar(); will always returns a int value which is not be similar to the userID because it will retrieve the first value from Database so just check whether it has any value which means your condition is true

so the above code which is given by Nayan is true.

if (result>0)
Console.Writeline("The user id is not valid";

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download