0

I'm creating a c# winform and I need to connect with a sql database which I already have, basically I need to registered users to log into the program using username and password..but I'm having issues with the db connection I guess, there's a run time error "connection property has not been initialized!"

I used this same connection string and my login form connected successfully..

String cs = (@"Data Source=DESKTOP-3BDK76K\SQLEXPRESS;Initial Catalog=FurnitureOrdering;Integrated Security=True");
private void button1_Click(object sender, EventArgs e)
       {

        try
        {

            SqlConnection myConnection = default(SqlConnection);
            myConnection = new SqlConnection(cs);

            SqlCommand myCommand = default(SqlCommand);

            myCommand = new SqlCommand("SELECT username,password FROM Register WHERE username= @username AND password = @password");

            SqlParameter uName = new SqlParameter("@username", SqlDbType.VarChar);
            SqlParameter uPassword = new SqlParameter("@password", SqlDbType.VarChar);

            uName.Value = txtUserName.Text;
            uPassword.Value = txtPassword.Text;

            myCommand.Parameters.Add(uName);
            myCommand.Parameters.Add(uPassword);


            myConnection.Open();

            SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            if (myReader.Read() == true)
            {
                MessageBox.Show("Successfully logged-in"+ txtUserName.Text);
                this.Hide();

                Main_UI ss2 = new Main_UI();
                ss2.Show();
            }
            else
            {
                MessageBox.Show("Login failed!");

                txtUserName.Clear();
                txtPassword.Clear();
            }
            if (myConnection.State == ConnectionState.Open)
            {
                myConnection.Dispose();
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

error message

"connection property has not been initialized!"

6
  • 1
    Note: setting a variable to default is pretty much never needed. Especially when on the next line you assign a value for them. Commented Jul 20, 2019 at 12:20
  • 2
    You are storing passwords in plainntext in the database. That is incredibly insecure. Commented Jul 20, 2019 at 12:22
  • The error means the command object's connection property has never been set. You can create a command from the connection and then it will have the property set like this: SqlCommand command = SqlConnection.CreatCommand() Commented Jul 20, 2019 at 12:30
  • I'm hoping to encrypt them later and thank you @mason Commented Jul 20, 2019 at 12:31
  • @M.Sachintha, rather than encrypt passwords, it would be better to store only the salted password hash and compare that to the hash of the provided password. That way, the secret cannot be obtained even if the data is compromised. Commented Jul 20, 2019 at 14:07

1 Answer 1

2

You must set connection property of your SqlCommand object.

myCommand.Connection = myConnection;
Sign up to request clarification or add additional context in comments.

2 Comments

thank you @parsa it worked!! can you explain this a little bit if you don't mind?
@M.Sachintha How do you think the command is going to know to use the connection you made unless you tell it to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.