locked
Exception Unhandled (System.InvalidOperationException:'ExecuteReader Connection property has not been initialized' RRS feed

  • Question

  • User-2081767517 posted

    Hi all. i am new to coding in visual studio(C#). I am using Windows Forms App (.Net Framework). I am currently doing this code but i meant an error. The error is shown in the title. These are my codes

     private void registerbutton_Click(object sender, EventArgs e)

            {

                if (confirmMembersIDmaskedTextBox.Text != string.Empty || enterMembersIDmaskedTextBox.Text != string.Empty || enterNametextBox.Text != string.Empty)

                {

                    if (enterMembersIDmaskedTextBox.Text == confirmMembersIDmaskedTextBox.Text)

                    {

                        SqlConnection cn = null;

                        SqlCommand cmd = new SqlCommand("select * from LoginTable where username='" + enterNametextBox.Text + "'", cn);

                        SqlDataReader dr = cmd.ExecuteReader();Exception unhandled 

                        if (dr.Read())

                        {

                            dr.Close();

                            MessageBox.Show("Username Already exist please try another ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        }

                        else

                        {

                            dr.Close();

                            cmd = new SqlCommand("insert into LoginTable values(@username,@password)", cn);

                            cmd.Parameters.AddWithValue("username", enterNametextBox.Text);

                            cmd.Parameters.AddWithValue("password", enterMembersIDmaskedTextBox.Text);

                            cmd.ExecuteNonQuery();

                            MessageBox.Show("Your Account is created . Please login now.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        }

                    }

                    else

                    {

                        MessageBox.Show("Please enter both password same ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }

                }

                else

                {

                    MessageBox.Show("Please enter value in all field.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

    Please help me. Thank you!

    Monday, January 25, 2021 2:52 AM

Answers

  • User-939850651 posted

    Hi StarryNight_XD,

    If you want to connect to the database and use ADO.NET to perform some operations, you need the correct connection string to instantiate the database connection.

    Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

    For example, if you need to connect to the database named Test in the local address. It should be something like:

    string connectionString = "Server=.;Database=Test;Trusted_Connection=True;";
    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("query statement...",conn);
    ......

    For more details, you could refer to this document.

    https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-5.0

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 26, 2021 7:00 AM

All replies

  • User-1716253493 posted

    You need to set the connection

    SqlConnection cn = null;

    Monday, January 25, 2021 4:56 AM
  • User-2081767517 posted

    Sorry but how do i connect it?

    Tuesday, January 26, 2021 5:26 AM
  • User-939850651 posted

    Hi StarryNight_XD,

    If you want to connect to the database and use ADO.NET to perform some operations, you need the correct connection string to instantiate the database connection.

    Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

    For example, if you need to connect to the database named Test in the local address. It should be something like:

    string connectionString = "Server=.;Database=Test;Trusted_Connection=True;";
    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("query statement...",conn);
    ......

    For more details, you could refer to this document.

    https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring?view=dotnet-plat-ext-5.0

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 26, 2021 7:00 AM