在数据库中的网页上显示表格 [英] Display a table on a webpage from database
问题描述
我正在创建一个网站,允许患者登录并查看他们自己的数据.到目前为止,当他们登录时,他们将被重定向到user.aspx页,并且一个会话将其用户名显示在Patient表的标签上(我已经包括了会话信息以帮助解决问题)...但是我也想要一个表显示患者相应的药物信息:
I am creating a website that allows a patient to log in and see their own data. So far when they log in they are redirected to the user.aspx page and a session displays their username on a label from the Patient table( I have included the session information to help with the question) ... But I also want a table to display the patients corresponding medicine information:
患者表(所有表都是伪数据):
The patient table (all tables are dummy data):
药物表:
登录后的会话在login.aspx中进行了身份验证:
The session after login is authenticated in login.aspx:
Public Function CheckUser(username As String, password As String) As Integer
Dim cmdstring As String = "SELECT * FROM Patient Where Username=@USERNAME AND Password=@PASSWORD"
Dim found = 0
Using conn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Laura\Final_proj\App_Data\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")
Dim cmd = New SqlCommand(cmdstring, conn)
cmd.Parameters.Add("@USERNAME", SqlDbType.NChar).Value = username
cmd.Parameters.Add("@PASSWORD", SqlDbType.NChar).Value = password
conn.Open()
Dim reader = cmd.ExecuteReader()
While reader.Read()
Session("PatientId") = CInt(reader.Item("PatientId"))
Session("Username") = CStr(reader.Item("Username"))
found = CInt(reader.Item("PatientId"))
End While
reader.Close()
End Using
Return (found)
End Function
在user.aspx的标签中显示用户名的标签:
Label displaying user name in label in user.aspx:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Label1.Text = Session("Username")
End Sub
我还有另一个称为处方的表(链接表),该表具有复合键Patientid(来自Patient表)和Medicine(来自Medicine表)-都是外键.
I have another table called prescription (link table) that has composite keys Patientid (from Patient table) and Medicine (from medicine table) - both foreign keys.
当用户登录时,如何获取Medicine表以显示用户相应的药物以及user.aspx上该表中的信息(名称,目的,说明).我会使用工具箱中的gridview来做到这一点吗?
When the user logs in how can I get the Medicine table to display showing the user's corresponding medicine and the information from the table (Name, Purpose, Instructions) on user.aspx. Will I do this using a gridview from Toolbox?
不确定我的解决方案在哪里出问题
Not sure where I am going wrong with the solution here
错误:
推荐答案
是的,只需在user.aspx页面的工具箱中添加gridview,然后在user.aspx页面的页面加载事件上运行以下代码行
yea simply add the gridview from toolbox in user.aspx page and run the below line of code on page load event of user.aspx page
Partial Class Pages_user
Inherits System.Web.UI.Page
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Laura\Final_proj\App_Data\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")
Dim cmdstring As String = "SELECT pt.PatientId, pt.ForeName, pt.Username, md.Name, md.Purpose, md.Instrcutions " +
"FROM Patient pt INNER JOIN prescription pr ON pt.PatientId = pr.PatientId " +
"INNER JOIN medicine md ON md.MedicineId = pr.MedicineId Where pt.PatientId = @PatientId"
Dim dt As New System.Data.DataTable()
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmdstring, conn)
da.SelectCommand.Parameters.Add("@PatientId", System.Data.SqlDbType.Int).Value = CInt(Session("PatientId").ToString())
conn.Open()
da.Fill(dt)
conn.Close()
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
End Class
您的aspx页面代码将是
And your aspx page code will be
<%@ Page Title="" Language="VB" MasterPageFile="~/Masterpages/MasterPage2.master" AutoEventWireup="true" CodeFile="user.aspx.vb" Inherits="Pages_user" %>
<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="Server">
<style type="text/css">
.auto-style2 {
font-size: x-large;
}
</style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="Server" Inherits="Pages_user" CodeFile="Pages_user.aspx.vb">
<p>
<span class="auto-style2">Please Select Your Medication
</span>
</p>
<asp:GridView ID="GridView1" runat="server" ></asp:GridView>
</asp:Content>
这篇关于在数据库中的网页上显示表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!