Consider the example of login when user login to the website then its username and password will be stored in the session. so here i have stored the session information in Database
HTML:
<form id="form1" runat="server">
<div>
UserName
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br />
Password
<asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox><br />
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="Login" />
</div>
</form>
C#:
protected void Login(object sender, EventArgs e)
{
Session["UserName"] = this.txtUserName.Text.Trim();
Session["Password"] = this.txtPassword.Text.Trim();
string insertSatement = "Insert into Session(UserName,Password) Values(@UserName,@password)";
string conStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(insertSatement, con))
{
con.Open();
cmd.Parameters.AddWithValue("@UserName", Session["UserName"].ToString());
cmd.Parameters.AddWithValue("@Password", Session["Password"].ToString());
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Thanks