2 Replies - 712 Views - Last Post: 05 December 2011 - 02:50 PM Rate Topic: -----

#1 JoshuaH83  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-December 11

IF statement in .cs file to check table in SQL

Posted 04 December 2011 - 06:38 PM

Hello, I am having an issue with my IF statement. My SQL command is working properly, but I am not 100% sure what I need to do on my execute reader & If statement.

Thank you in advance for your help.
int results = 0;

        // SQL connection
        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["TEST_Usertdb"].ConnectionString);
        thisConnection.Open();

        SqlDataReader PermisionReader = null;

        //Check Permission for Payroll
        SqlCommand PermissionCheck = new SqlCommand("SELECT Roles.RoleName FROM Roles " +
                    "INNER JOIN UsersInRoles ON aspnet_Roles.RoleId = aspnet_UsersInRoles.RoleId " +
                    "INNER JOIN vw_Users ON UsersInRoles.UserId = vwUsers.UserId " +
                    "INNER JOIN vw_Roles ON Roles.RoleId = vwRoles.RoleId " +
                    "where UserName=@thisLogin ", thisConnection);

        PermissionCheck.Parameters.Add("@thisLogin", SqlDbType.NVarChar).Value = thisLogin;

        PermisionReader = PermissionCheck.ExecuteReader();

        if (PermisionReader = "Server")
        {
	        ServerPnl.Visible = true;
        }
        else if (PermisionReader = "Chief")
        {
	        ChiefPnl.Visible = true;
        }
        thisConnection.Close();
    }

This post has been edited by CodingSup3rnatur@l-360: 05 December 2011 - 02:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: IF statement in .cs file to check table in SQL

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,665
  • Joined: 02-June 10

Re: IF statement in .cs file to check table in SQL

Posted 04 December 2011 - 09:13 PM

:code:

Please edit your post to include code tags

If comparisons are done with a double equal ==

All of your if checks right now are setting values not comparing.
Was This Post Helpful? 0
  • +
  • -

#3 sidtrey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-December 11

Re: IF statement in .cs file to check table in SQL

Posted 05 December 2011 - 02:50 PM

View PostJoshuaH83, on 04 December 2011 - 06:38 PM, said:

Hello, I am having an issue with my IF statement. My SQL command is working properly, but I am not 100% sure what I need to do on my execute reader & If statement.

Thank you in advance for your help.
int results = 0;

        // SQL connection
        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["TEST_Usertdb"].ConnectionString);
        thisConnection.Open();

        SqlDataReader PermisionReader = null;

        //Check Permission for Payroll
        SqlCommand PermissionCheck = new SqlCommand("SELECT Roles.RoleName FROM Roles " +
                    "INNER JOIN UsersInRoles ON aspnet_Roles.RoleId = aspnet_UsersInRoles.RoleId " +
                    "INNER JOIN vw_Users ON UsersInRoles.UserId = vwUsers.UserId " +
                    "INNER JOIN vw_Roles ON Roles.RoleId = vwRoles.RoleId " +
                    "where UserName=@thisLogin ", thisConnection);

        PermissionCheck.Parameters.Add("@thisLogin", SqlDbType.NVarChar).Value = thisLogin;

        PermisionReader = PermissionCheck.ExecuteReader();

        if (PermisionReader = "Server")
        {
	        ServerPnl.Visible = true;
        }
        else if (PermisionReader = "Chief")
        {
	        ChiefPnl.Visible = true;
        }
        thisConnection.Close();
    }



Ok man, PermissionReader should be a datareader right? Your treating it like a string.

You want something like:
while(rdr.Read())
                {Console.Writeline(rdr["RoleName"].ToString(); }


That's my big fat hint of the day :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1