มันติดปัญหามากเลยครับ ส่งตัวแปร session ไปแล้วมันอ่านข้อมูลแค record แรกอ่ะครับ ไม่ทราบมันเกิดปัญหาตรงไหนอ่ะครับช่วยดูให้หน่อย
Code (VB.NET)
หน้า Login.aspx
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports Microsoft.VisualBasic
Public Class Login
Inherits System.Web.UI.Page
Dim ds As New DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Public Function dtCon(ByVal StrUsername As String, _
Optional ByVal StrPassword As String = "") As DataTable
Dim strCon As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim con As New SqlConnection(strCon)
con.Open()
Dim dt As New DataTable
Dim dt2 As New DataTable
Dim sql As String = "SELECT * From ConsumerDB "
sql &= "where Username = '" & StrUsername & "'"
sql &= " and Password = '" & StrPassword & "' "
Dim cm As New SqlCommand(sql, con)
Dim da As New SqlDataAdapter(cm)
Dim br As New SqlCommandBuilder(da)
da.Fill(ds)
dt = ds.Tables("ConsumerDB")
da = Nothing
con = Nothing
con.Close()
dt2 = ds.Tables("ConsumerDB")
Return dt2
End Function
Private Sub CheckData()
Dim dv As New Data.DataView(dtCon(Me.txtUser.Text, Me.txtPwd.Text))
If dv.Count > 0 Then
Session("ConsumerID") = dv.Item(CInt("ConsumerID")).Row("ConsumerID")
Response.Redirect("Consumerhome.aspx")
Else
Alert.Msg(Page, "ป้อนรหัสผ่าน หรือ username ไม่ถูกต้อง")
End If
End Sub
Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
CheckData()
End Sub
End Class
Code (VB.NET)
[vb]Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Public Class WebUserControl1
Inherits System.Web.UI.UserControl
Dim conn As SqlConnection
Dim ds As New DataSet
Dim dt As DataTable
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If Not IsNothing(Session("ConsumerID")) Then
LoadUser()
End If
End If
End Sub
Public Function LoadData(Optional ByVal strCus_ID As Integer = 0) As DataTable
If Not IsNothing(Session("ConsumerID")) Then
Dim strCon As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim con As New SqlConnection(strCon)
con.Open()
Dim sql As String = "Select * From ConsumerDB "
'sql &= "Where (ConsumerID = ConsumerID And [Company] = Company"
Dim cm As New SqlCommand(sql, con)
cm.CommandType = CommandType.Text
Dim da As New SqlDataAdapter(cm)
Dim br As New SqlCommandBuilder(da)
'Dim rd As SqlDataReader = cm.ExecuteReader()
Dim ds As New DataSet
Dim dt As DataTable
da.Fill(ds, "ConsumerDB")
dt = ds.Tables("ConsumerDB")
da = Nothing
con.Close()
con = Nothing
Me.lblShow.Text = dt.Rows(0).Item("Company").ToString
If CBool(Session("ComsumerID")) = True Then
Response.Redirect("W.aspx")
End If
Else
Response.Redirect("W.aspx")
End If
End Function
Private Sub LoadUser(Optional ByVal strCus_ID As Integer = 0)
If Not IsNothing(Session("ConsumerID")) Then
Dim dt As DataTable = dt.Rows(0).Item("Password")(Session("ConsumerID")))
Me.lblShow.Text = CStr(Session("ConsumerID"))
If dt.Rows.Count > 0 Then
Me.lblShow.Text = CStr(dt.Rows(0)("Company"))
End If
Me.lblShow.Visible = True
'Session("Flag") = "Customer"
Else
Me.lblShow.Visible = False
End If
End Sub
End Class