Imports System.Data
Imports System.Data.SqlServerCe
Public Class Login
Dim Cnn As New SqlCeConnection(Cnnstring)
Dim Cnnstring As String
Dim sqlselect As String
Dim sqlreader As SqlCeDataReader
Dim userida As String
Dim passida As String
Dim myConnection As SqlCeConnection = New SqlCeConnection("Data source=D:\Project New\WindowsApplication1\WindowsApplication1\Database1.sdf;")
Dim myCommand As SqlCeCommand
Dim myConn As SqlCeCommand
Dim cmdString As String
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
With Cnn
If .State = ConnectionState.Open Then Close()
.ConnectionString = Cnnstring
myConnection.Open()
myConnection.Close()
End With
If txtuser_ID.Text = "" And txtpassword.Text = "" Then
MessageBox.Show("ใส่ข้อมูล", "No userID", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
ReadMyData(Cnnstring)
End If
End Sub
Public Sub readmydata(ByVal myConnString As String)
On Error GoTo 0
With Cnn
If .State = ConnectionState.Open Then Close()
.ConnectionString = Cnnstring
myConnection.Open()
End With
sqlselect = "select * from mytable Where id = '" & txtuser_ID.Text & "' and Password = '" & txtpassword.Text & "'"
Dim sqlcommand As New SqlCeCommand(sqlselect, myConnection)
sqlreader = sqlcommand.ExecuteReader
sqlreader.Read()
userida = CStr(sqlreader.Item("id"))
passida = CStr(sqlreader.Item("password"))
If txtuser_ID.Text = userida And txtpassword.Text = passida Then
chair.Show()
txtuser_ID.Text = ""
txtpassword.Text = ""
Me.Hide()
Else
MessageBox.Show("กรุณาใส่ข้อมูลให้ถูกต้อง", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub OK_Click(.....
If txtuser_ID.Text = "" And txtpassword.Text = "" Then
MessageBox.Show("ใส่ข้อมูล", "No userID", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
If ReadMyData(txtuser_id.text,txtpassword.text) = false then
Messagebox.Show("Username And Password ไม่ถูกต้อง")
end if
end if
end sub
Private function ReadMyData (ByVal userid As String,ByVal password As String) as boolean
if Cnn.State = ConnectionState.Open then Cnn.Close()
Cnn.connectionstring = Cnnstring
Cnn.Open()
sqlselect = "select * from mytable Where id = '" & userid & "' and Password = '" & password & "'"
Dim da as New SqlDataadapter(sqlselect,Cnn)
dim ds as New Dataset
da.fill(ds,"dtLogin")
if ds.rows.count = 0 then
return false
end if
return true
end function
เอาแบบง่ายๆคือ ย้ายบรรทัดที 56 เเละ 57 ไปไว้ใน if หลังบรรทัดที่ 59
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlServerCe
Public Class Login
Dim Cnn As New SqlCeConnection(Cnnstring)
Dim Cnnstring As String
Dim sqlselect As String
Dim sqlreader As SqlCeDataReader
Dim userida As String
Dim passida As String
Dim myConnection As SqlCeConnection = New SqlCeConnection("Data source=D:\Project New\WindowsApplication1\WindowsApplication1\Database1.sdf;")
Dim myCommand As SqlCeCommand
Dim myConn As SqlCeCommand
Dim cmdString As String
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
With Cnn
If .State = ConnectionState.Open Then Close()
.ConnectionString = Cnnstring
myConnection.Open()
myConnection.Close()
End With
If txtuser_ID.Text = "" And txtpassword.Text = "" Then
MessageBox.Show("ใส่ข้อมูล", "No userID", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
ReadMyData(Cnnstring)
End If
End Sub
Public Sub readmydata(ByVal myConnString As String)
On Error GoTo 0
With Cnn
If .State = ConnectionState.Open Then Close()
.ConnectionString = Cnnstring
myConnection.Open()
End With
sqlselect = "select * from mytable Where id = '" & txtuser_ID.Text & "' and Password = '" & txtpassword.Text & "'"
Dim sqlcommand As New SqlCeCommand(sqlselect, myConnection)
sqlreader = sqlcommand.ExecuteReader
sqlreader.Read()
if sqlreader.HasRows Then
userida = CStr(sqlreader.Item("id"))
passida = CStr(sqlreader.Item("password"))
end if
If txtuser_ID.Text = userida And txtpassword.Text = passida Then
chair.Show()
txtuser_ID.Text = ""
txtpassword.Text = ""
Me.Hide()
Else
MessageBox.Show("กรุณาใส่ข้อมูลให้ถูกต้อง", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
เหตุผลที่มัน Error คือมัน Reader มาเป็นตารางเปล่า
Date :
2012-02-26 04:49:14
By :
noom
No. 7
Guest
เม้นข้างบนตอบผิดนะครับ ขอตอบใหม่ Code (VB.NET)
Imports System.Data
Imports System.Data.SqlServerCe
Public Class Login
Dim Cnn As New SqlCeConnection(Cnnstring)
Dim Cnnstring As String
Dim sqlselect As String
Dim sqlreader As SqlCeDataReader
Dim userida As String
Dim passida As String
Dim myConnection As SqlCeConnection = New SqlCeConnection("Data source=D:\Project New\WindowsApplication1\WindowsApplication1\Database1.sdf;")
Dim myCommand As SqlCeCommand
Dim myConn As SqlCeCommand
Dim cmdString As String
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
With Cnn
If .State = ConnectionState.Open Then Close()
.ConnectionString = Cnnstring
myConnection.Open()
myConnection.Close()
End With
If txtuser_ID.Text = "" And txtpassword.Text = "" Then
MessageBox.Show("ใส่ข้อมูล", "No userID", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
ReadMyData(Cnnstring)
End If
End Sub
Public Sub readmydata(ByVal myConnString As String)
On Error GoTo 0
With Cnn
If .State = ConnectionState.Open Then Close()
.ConnectionString = Cnnstring
myConnection.Open()
End With
sqlselect = "select * from mytable Where id = '" & txtuser_ID.Text & "' and Password = '" & txtpassword.Text & "'"
Dim sqlcommand As New SqlCeCommand(sqlselect, myConnection)
sqlreader = sqlcommand.ExecuteReader
sqlreader.Read()
If sqlreader.HasRows Then
userida = CStr(sqlreader.Item("id"))
passida = CStr(sqlreader.Item("password"))
End If
If txtuser_ID.Text = userida And txtpassword.Text = passida Then
chair.Show()
txtuser_ID.Text = ""
txtpassword.Text = ""
Me.Hide()
Else
MessageBox.Show("กรุณาใส่ข้อมูลให้ถูกต้อง", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Sub myDataReader()
Dim strSQL As String
Dim dtReader As SqlDataReader
strSQL = "SELECT * FROM customer "
dtReader = clsDB.QueryDataReader(strSQL)
'*** Bind Rows ***'
If dtReader.HasRows = True Then
Me.lblCustomerID.Text = dtReader.Item("CustomerID")
Me.lblName.Text = dtReader.Item("Name")
Me.lblEmail.Text = dtReader.Item("Email")
Me.lblCountryCode.Text = dtReader.Item("CountryCode")
Me.lblBudget.Text = dtReader.Item("Budget")
Me.lblUsed.Text = dtReader.Item("Used")
End If
clsDB.Close()
End Sub