C#

Can someone please help me to edit this code and get it working.
Im trying to company a value in SQL table (Column) with the value that is in a lable and comes from the Windows username.

 
label1.Text = WindowsIdentity.GetCurrent().Name.Split('\\')[1];
             SqlCommand UsersTable = new SqlCommand("Select User_ID from Users_Table where User_ID like '" + label1.Text + "%'"); 
             string table = UsersTable.ToString(); 
             UsersTable.CommandType = CommandType.Text;
             if (table != label1.Text)
             {
                 MessageBox.Show("You are Logged in as an End user");
             }
             else
             {
                 MessageBox.Show("You Have a username in database");

            }

Open in new window

pauledwardianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You need to execute the command, then read the result:

label1.Text = WindowsIdentity.GetCurrent().Name.Split('\\')[1];

SqlCommand UsersTable = new SqlCommand("Select User_ID from Users_Table where User_ID like '" + label1.Text + "%'");

UsersTable.CommandType = CommandType.Text;

string table = UsersTable.ExecuteScalar() as string;

if (table != null && table != label1.Text)
{
    MessageBox.Show("You are Logged in as an End user");
}
else
{
    MessageBox.Show("You Have a username in database");
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wouter BoevinkMasterCommented:
Why do you do a ´select like´ and than compare it to an exact value?

For Example if you have a user with user id kaufmed in you db.
label1.Text contains kauf, de query will return the user kaufmed.
And if (table != label1.Text) will return false.

ExecuteScalar returns only the first column of the first row, however there could be more results.

I would use count()

label1.Text = WindowsIdentity.GetCurrent().Name.Split('\\')[1];
var UsersTable = new SqlCommand("Select count(*) from Users_Table where User_ID like '" + label1.Text + "%'");
UsersTable.CommandType = CommandType.Text;
var table = UsersTable.ExecuteScalar() as int;
if (table = 0){
    MessageBox.Show("You are Logged in as an End user");
}
else{
    MessageBox.Show("You Have a username in database");
}

Open in new window

0
käµfm³d 👽Commented:
ExecuteScalar returns only the first column of the first row, however there could be more results.
Exactly. There is only one label, so it wouldn't matter how many names are in the database... you can only display one name! Either approach would work, methinks.
0
pauledwardianAuthor Commented:
0
pauledwardianAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.