 |
|
VB.NET ดูโค้ดหน้า Login ให้หน่อยครับ รันไม่ผ่านเวลาใส่รหัสหรือ ไอดีผิดครับ |
|
 |
|
|
 |
 |
|
Debug ค่า sqlselect ให้ดูค่าหน่อยครับ
|
 |
 |
 |
 |
Date :
2012-02-25 20:23:42 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
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
|
 |
 |
 |
 |
Date :
2012-02-25 20:30:48 |
By :
tee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Error ตามนี้เลยครับ
ปล. ลองโค้ดของพี่ tee ดู มันติดตรงบันทัดที่ 24 บอกว่า ไม่สามารถใช้ rows กับ dataset ได้

|
 |
 |
 |
 |
Date :
2012-02-25 21:03:22 |
By :
Mikkola |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันบอกว่าไม่มี Rows และ Column ครับ
ลองใช้
Code (VB.NET)
if sqlreader.HasRows Then
Else
End IF
ผมถึงให้คุณ Debug เอาค่า SQL ออกมาดูครับ
|
 |
 |
 |
 |
Date :
2012-02-25 21:06:44 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จะ Debug SQL ออกมาดูยังไงเหรอครับ ผมไม่เป็นอ่ะครับ ขอโทษด้วยครับ
|
 |
 |
 |
 |
Date :
2012-02-25 21:31:26 |
By :
Mikkola |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาแบบง่ายๆคือ ย้ายบรรทัดที 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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เม้นข้างบนตอบผิดนะครับ ขอตอบใหม่
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
|
 |
 |
 |
 |
Date :
2012-02-26 04:51:03 |
By :
noom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ hasrows ไม่ได้อะครับ

|
 |
 |
 |
 |
Date :
2012-02-26 10:08:16 |
By :
Mikkola |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างการใช้ DataReader กับ HasRows ครับ
Code (VB.NET)
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
|
 |
 |
 |
 |
Date :
2012-02-26 10:13:33 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จะลองดูครับ ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2012-02-26 10:25:32 |
By :
Mikkola |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|