Display DataTable information in aspx page

2020-08-04 04:35发布

问题:

I am new to Asp.net. I have created a login page which retrieves the user information in a DataTable. I am storing this information in a Session Variable.

Here s the code:

 using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Web.Configuration; 


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from UserTable where UserName =@username and Password=@password", con);
    cmd.Parameters.AddWithValue("@username", txtUserName.Text);
    cmd.Parameters.AddWithValue("@password", txtPWD.Text);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        Session["userdata"] = dt;
        Response.Redirect("Home.aspx");
    }
    else
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
    }
}
}

Now want to display the DataTable information in Home.aspx. How can i do this?

回答1:

Home.aspx

<asp:Label ID="lblUserName" runat="server" />

code behind

protected void Page_Load(object sender, EventArgs e)
{

    DataTable dt=new DataTable();
    dt= (DataTable)Session["userdata"] ;
    lblUserName.Text=dt.Rows[0]["UserName"].ToString();//your cloumn name;

}


回答2:

Use gridview..

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

During runtime its rendered as table only..

then

GridView1.DataSource=Session[userdata];
GridView1.DataBind();


回答3:

You can retrieve your datatable using one of the following on Home.aspx

DataTable dtTable = Session["userdata"] as DataTable

or

DataTable dtTable= (DataTable)Session["userdata"];

To extract data-

dtTable.rows[rowindex][columnindex] // Ex: dtTable.rows[0][0]

or if you know the column name

dtTable.rows[rowindex][columnname] // Ex: dtTable.rows[0]["yourColumnName"] 


回答4:

in Home.aspx you can do as below

protected void Page_Load(object sender, EventArgs e)
{
     DataTable dt = (DataTable)Session["userdata"]
     gridview.DataSource =dt;
     gridview.DataBind();
}


回答5:

In your Home.aspx add the labels for the info you wan to display about the user. Lets say you wan to display the username, then add this :

<asp:Label ID="lblUserName" runat="server" />

In the code behind, add this :

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = (DataTable)Session["userdata"] ;
    lblUserName.Text = dt.Rows[0]["UserName"].ToString();
}