Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Get user name from one form to another


Users groups

access rights


I have error in this line

korisnickaGrupa = Int32.Parse(myCommand3.ExecuteScalar().ToString());


Sql query are OK

Some help?

What I have tried:

private void prijava()
        if (korisnikId > 0)
            {
                command = "SELECT osobe_korisnici.lozinka FROM osobe_korisnici where osobe_korisnici.id = " + korisnikId;
                SqlCommand myCommand2 = new SqlCommand(command, myConnection);
                String lozinka = myCommand2.ExecuteScalar().ToString();
                if (lozinka == lozinkaTextBox.Text)
                {
                    command = "SELECT korisnik_grupa.redni_broj FROM korisnik_grupa where korisnik_grupa.broj_grupe = \"" + korisnikId + "\"";
                    //MessageBox.Show(command);
                    SqlCommand myCommand3 = new SqlCommand(command, myConnection);
                    korisnickaGrupa = Int32.Parse(myCommand3.ExecuteScalar().ToString());
                   
                    if (korisnickaGrupa == 1)
                        {
                        findsUserName();
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        errorLabel.Text = "You dont have acces for this app";
                    }
                }
                else
                {
                    errorLabel.Text = "Wrong password";
                }
            }
            else
            {
                errorLabel.Text = "Wrong user name";
            }


        }



public void findsUserName()
        {
            SqlConnection myConnection = new SqlConnection(cs);
            String command = "SELECT ime_prezime FROM osobe_korisnici  where id = \"" + korisnikId + "\"";
            //MessageBox.Show(command);
            SqlCommand myCommand = new SqlCommand(command, myConnection);


            myCommand.Connection.Open();
            this.korisnickoIme = myCommand.ExecuteScalar().ToString();
        }
Posted
Updated 23-May-18 0:49am
Comments
Stylus STYLUS 23-May-18 5:07am    
private static int korisnikId;
private static int korisnickaGrupa;
private string korisnickoIme;

I forgot this...I added on start code line
Ziee-M 23-May-18 5:34am    
What is the exception you get ?
It seems your returned id (before parse) is in wrong format, probably a special caracter in the start and/Or the end. Just remove them with a Trim() or anyother way.
Stylus STYLUS 23-May-18 5:41am    
Exception is number from command
Stylus STYLUS 23-May-18 5:42am    
I wrong I need help. Thank you
Richard Deeming 25-May-18 8:17am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

Fix that, and you'll almost certainly fix your error too.

1 solution

Quote:
Int32.Parse(myCommand3.ExecuteScalar().ToString());

This statement is a potential source of many exceptions, you have to handle them properly.
You stated your query is OK, but neverthless such code fails. Use the debugger to locate the problem (e.g. invalid textual representation of a Int32).
 
Share this answer
 
Comments
Stylus STYLUS 24-May-18 1:32am    
korisnikId = Convert.ToInt32(myCommand.ExecuteScalar());

This line is error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900