Hi micah,
I have created a sample which full fill your requirement you need to modify the code according to your requirement.
SQL
CREATE TABLE Favorite
(
ID INT IDENTITY(1,1),
UserName VARCHAR(50),
Favorites VARCHAR(60)
)
Above is your favourite table designed and created based on your preference
Login.aspx
<div>
<table>
<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 Text="Login" OnClick="OnLogin" runat="server" />
</td>
</tr>
</table>
</div>
Login.aspx.cs
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("Default.aspx");
}
}
}
}
Login.aspx.vb
Private constring As String = ConfigurationManager.ConnectionStrings("constring").ToString()
Protected Sub OnLogin(sender As Object, e As EventArgs)
Using con As New SqlConnection(constring)
Using cmd As 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()
Dim idr As SqlDataReader = cmd.ExecuteReader()
If idr.Read() Then
Session("UserName") = idr("UserName").ToString()
Response.Redirect("Default.aspx")
End If
End Using
End Using
End Sub
Default.aspx
<div style="width: 250px">
<div class="row">
<div class="pull-left" style="margin-left: 16px;">
<h4 id="cooking" class="list-group-item-heading" runat="server">
Cooking</h4>
</div>
<div class="pull-right">
<asp:CheckBox ID="chkCooking" runat="server" />
</div>
</div>
<br />
<div class="row">
<div class="pull-left" style="margin-left: 16px;">
<h4 id="coding" runat="server" class="list-group-item-heading">
Coding</h4>
</div>
<div class="pull-right">
<asp:CheckBox ID="chkCoding" runat="server" />
</div>
</div>
<br />
<div class="row">
<div class="pull-left" style="margin-left: 16px;">
<h4 id="marriage" runat="server" class="list-group-item-heading">
Marriage</h4>
</div>
<div class="pull-right">
<asp:CheckBox ID="chkMarriage" runat="server" />
</div>
</div>
<br />
<div class="row">
<div class="pull-left" style="margin-left: 16px;">
<h4 id="Book" runat="server" class="list-group-item-heading">
Book on driving</h4>
</div>
<div class="pull-right">
<asp:CheckBox ID="chkBookOnDriving" runat="server" />
</div>
</div>
<br />
<asp:Button ID="btnSave" OnClick="Save" Text="Save" runat="server" />
<br />
<asp:Label ID="lblMessage" runat="server" />
<br />
<asp:GridView ID="gvFavorites" runat="server" />
</div>
Default.aspx.cs
protected void Save(object sender, EventArgs e)
{
string userName = Session["UserName"].ToString();
if (chkCooking.Checked)
{
SaveDetails(userName, cooking.InnerHtml.Trim());
}
if (chkCoding.Checked)
{
SaveDetails(userName, coding.InnerHtml.Trim());
}
if (chkMarriage.Checked)
{
SaveDetails(userName, marriage.InnerHtml.Trim());
}
if (chkBookOnDriving.Checked)
{
SaveDetails(userName, Book.InnerHtml.Trim());
}
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Records Inserted SuccessFully";
BindGrid();
}
string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
public void SaveDetails(string userName, string Favorites)
{
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Favorite values(@UserName,@Favorites)", con))
{
cmd.Parameters.AddWithValue("@UserName", userName);
cmd.Parameters.AddWithValue("@Favorites", Favorites);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
public void BindGrid()
{
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Favorite", con))
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
gvFavorites.DataSource = dt;
gvFavorites.DataBind();
}
}
}
Default.aspx.vb
Protected Sub Save(sender As Object, e As EventArgs)
Dim userName As String = Session("UserName").ToString()
If chkCooking.Checked Then
SaveDetails(userName, cooking.InnerHtml.Trim())
End If
If chkCoding.Checked Then
SaveDetails(userName, coding.InnerHtml.Trim())
End If
If chkMarriage.Checked Then
SaveDetails(userName, marriage.InnerHtml.Trim())
End If
If chkBookOnDriving.Checked Then
SaveDetails(userName, Book.InnerHtml.Trim())
End If
lblMessage.ForeColor = System.Drawing.Color.Green
lblMessage.Text = "Records Inserted SuccessFully"
BindGrid()
End Sub
Private constring As String = ConfigurationManager.ConnectionStrings("constring").ToString()
Public Sub SaveDetails(userName As String, Favorites As String)
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("INSERT INTO Favorite values(@UserName,@Favorites)", con)
cmd.Parameters.AddWithValue("@UserName", userName)
cmd.Parameters.AddWithValue("@Favorites", Favorites)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Public Sub BindGrid()
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT * FROM Favorite", con)
Dim sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
gvFavorites.DataSource = dt
gvFavorites.DataBind()
End Using
End Using
End Sub
ScreenShot