This is my code
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.OleDb
Imports System.Text
Public Class Form1
Dim Conn As New OleDbConnection()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String
strConn = DBConn.strConn
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
End Sub
Private Sub Login_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Login.Click
Dim sb As New StringBuilder()
sb.Append("Select Users,Passw FROM Users")
sb.Append(" WHERE (Users = '" & user.Text.Trim() & "')")
sb.Append(" AND (Passw = '" & pass.Text.Trim() & "')")
Dim sqlLogin As String
sqlLogin = sb.ToString()
Dim com As New OleDbCommand()
Dim dr As OleDbDataReader
With com
.CommandType = CommandType.Text
.CommandText = sqlLogin
.Connection = Conn
dr = com.ExecuteReader << this is problem that occure
If dr.HasRows Then
MessageBox.Show("รหัสผ่านแล้ววววววว !!!", "ผลการทำงาน",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Dim fMain As New _002allmodule()
fMain.Show()
Me.Hide()
Else
MessageBox.Show("UserName หรือ Password ที่คุณป้อน
ไม่ถูกต้อง !!!", "ผลการทำงาน", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
user.Text = ""
user.Focus()
pass.Text = ""
End If
End With
dr.Close()
dr.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Application.Exit()
End Sub
End Class
Error in detail
System.InvalidOperationException was unhandled
Message="ExecuteReader requires an open and available Connection. The
connection's current state is closed."
Source="System.Data"
StackTrace:
at System.Data.OleDb.OleDbConnection.CheckStateOpen(String method)
at System.Data.OleDb.OleDbCommand.ValidateConnection(String method)
at
System.Data.OleDb.OleDbCommand.ValidateConnectionAndTransaction(String method)
at
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior
behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior
behavior)
at CarPark.Form1.Login_Click(Object sender, EventArgs e) in
I:\CarPark\001login.vb:line 39
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at CarPark.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
PS. even I change patch file to other in this class
Option Explicit On
Option Strict On
Public NotInheritable Class DBConn
Public Shared strConn As String = "Provider=Microsoft.JET.OLEDB.4.0;data
source=I:\CarPark\carpark.mdb"
End Class
It won't error on open() fuction but it still error in dr = com.ExecuteReader
Thank You for your attention