I am creating a login form together with sql as may databse but when i run may program an error occur at "myData = myCommand.ExecuteReader()" stating that "Connection must be valid and open" what should i do on that to make it fix??

someone help me please

thanks in advance

Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient

Public Class Login


    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim conn As MySqlConnection

        conn = New MySqlConnection()
        conn.ConnectionString = "server=112.200.62.203; user id =root; password=; database=sample"
        Try
            conn.Open()

        Catch ex As Exception
            MsgBox("Error")

        End Try
        Dim MyAdapter As New MySqlDataAdapter

        Dim sqlquery = "SELECT * FROM tbllogin Where username='" & txtUsername.Text & "' and password='" & txtPassword.Text & "'"
        Dim myCommand As New MySqlCommand()
        myCommand.Connection = conn
        myCommand.CommandText = sqlquery
        MyAdapter.SelectCommand = myCommand
        Dim myData As MySqlDataReader
        myData = myCommand.ExecuteReader()

        If myData.HasRows = 0 Then
            MsgBox("Invalid Login", MsgBoxStyle.Critical)
            txtUsername.Clear()
            txtPassword.Clear()
        Else
            MsgBox("Login Successful")
            ProfileForm.Show()

            Me.Hide()

        End If


    End Sub

If you are using mySql for your database then your connection string is wrong. Use uid not user Id.

i change the user id to uid as you said but the same error was occur what should i do??

by the way thanks for the reply

I see you're using an IP address. Is the database on a remote machine rather than local host? Maybe you need to include a port number? And try changing password to pwd as well. A handy site for locating the format of connection strings is http://www.connectionstrings.com. It has every connection string you will ever need.

I see you're using an IP address. Is the database on a remote machine rather than local host? Maybe you need to include a port number? And try changing password to pwd as well. A handy site for locating the format of connection strings is http://www.connectionstrings.com. It has every connection string you will ever need.

now its working thank you very much sir ^_^

I noticed in your code that you haven't closed and dispose your connection. So when you clicked twice in the btnOK, an error will occur. Try closing your connection first to ensure that your connection was closed, and close it after you're done with it. Hope this helps.

You can do what archelle stated by this simple if statement.

If conn.State = ConnectionState.Open Then
            conn.Close()
            conn.Dispose()
End If

yeah... thats what i mean..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.