Reputation: 319
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?
Upvotes: 0
Views: 19179
Reputation: 3740
Use gridview..
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
During runtime its rendered as table only..
then
GridView1.DataSource=Session[userdata];
GridView1.DataBind();
Upvotes: 5
Reputation: 4081
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();
}
Upvotes: 1
Reputation: 8726
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;
}
Upvotes: 3
Reputation: 63065
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();
}
Upvotes: 1
Reputation: 10020
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"]
Upvotes: 1