Hi micah,
I have created sample code which full-fill your requirement.
HTML
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
UserName
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" />
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<asp:TextBox ID="txtPassword" TextMode="Password" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" Text="Login" OnClick="OnLogin" runat="server" />
</td>
</tr>
</table>
</div>
C#
private string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
protected void OnLogin(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [User] WHERE UserName=@UserName AND Password=@Password", con))
{
cmd.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
con.Open();
SqlDataReader idr = cmd.ExecuteReader();
if (idr.Read())
{
Session["UserName"] = idr["UserName"].ToString();
Response.Redirect("Default2.aspx");
}
con.Close();
}
}
}
ViewPage
<form id="form1" runat="server">
<div>
<asp:FormView ID="formview1" runat="server" Width="100%" OnDataBound="formview1_DataBound">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="FavoriteId" Text="0" Visible="false" runat="server" />
<asp:Label ID="lblId" Text='<%#Eval("favoriteId") %>' Visible="false" runat="server" />
</td>
<td>
UserName:<asp:Label ID="lblUserName" Text='<%#Eval("StudentUserName")%>' runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
Count:
<asp:Label ID="lblCount" Text="0" runat="server" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
</div>
<asp:LinkButton ID="btnback" OnClick="OnBack" Text="Back" runat="server" />
</form>
C#
private string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetData();
}
}
private void GetData()
{
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT [favoriteId],[StudentUserName],[TeachersUserName],[Status] FROM [Dim_favorite] WHERE StudentUserName='" + Session["UserName"] + "'", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
formview1.DataSource = dt;
formview1.DataBind();
}
}
}
protected void OnBack(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
protected void formview1_DataBound(object sender, EventArgs e)
{
string userName = (formview1.FindControl("lblUserName") as Label).Text;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(" SELECT COUNT(StudentUserName) as Count FROM Dim_favorite where StudentUserName='" + userName + "'AND Status='1'", con))
{
con.Open();
SqlDataReader idr = cmd.ExecuteReader();
if (idr.Read())
{
(formview1.FindControl("lblCount") as Label).Text = idr["Count"].ToString();
}
con.Close();
}
}
}
Vb.Net
private string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
protected void OnLogin(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [User] WHERE UserName=@UserName AND Password=@Password", con))
{
cmd.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
con.Open();
SqlDataReader idr = cmd.ExecuteReader();
if (idr.Read())
{
Session["UserName"] = idr["UserName"].ToString();
Response.Redirect("Default2.aspx");
}
con.Close();
}
}
}
ViewPage
Private constring As String = ConfigurationManager.ConnectionStrings("constring").ToString()
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Me.GetData()
End If
End Sub
Private Sub GetData()
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT [favoriteId],[StudentUserName],[TeachersUserName],[Status] FROM [Dim_favorite] WHERE StudentUserName='" + Session("UserName") + "'", con)
Dim dt As New DataTable()
Dim sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
formview1.DataSource = dt
formview1.DataBind()
End Using
End Using
End Sub
Protected Sub OnBack(sender As Object, e As EventArgs)
Response.Redirect("Default.aspx")
End Sub
Protected Sub formview1_DataBound(sender As Object, e As EventArgs)
Dim userName As String = TryCast(formview1.FindControl("lblUserName"), Label).Text
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand((Convert.ToString(" SELECT COUNT(StudentUserName) as Count FROM Dim_favorite where StudentUserName='") & userName) + "'AND Status='1'", con)
con.Open()
Dim idr As SqlDataReader = cmd.ExecuteReader()
If idr.Read() Then
TryCast(formview1.FindControl("lblCount"), Label).Text = idr("Count").ToString()
End If
con.Close()
End Using
End Using
End Sub
Screenshot