ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Insert (Save) Session ID in Database using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <p><strong>CS.aspx</strong></p> <pre class="brush: html">Id : &lt;asp:TextBox ID=&#34;txtId&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt;&lt;br /&gt; &lt;asp:Button Text=&#34;Insert&#34; runat=&#34;server&#34; OnClick=&#34;Save&#34; /&gt;</pre> <p><strong>Default.aspx</strong></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;gvSessioDetails&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Id&#34; HeaderText=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;SessionId&#34; HeaderText=&#34;SessionId&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; using System.Configuration; using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Configuration Imports System.Data </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>CS.aspx.cs</strong></p> <pre class="brush: csharp">protected void Save(object sender, EventArgs e) { Session[&#34;Id&#34;] = txtId.Text; Response.Redirect(&#34;Default.aspx&#34;); }</pre> <p><strong>Default.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { int id = Convert.ToInt16(Session[&#34;Id&#34;]); string sessionId = Session.SessionID; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;INSERT INTO tblSesioData VALUES(@Id,@SessionId)&#34;, con); cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); cmd.Parameters.AddWithValue(&#34;@SessionId&#34;, sessionId); con.Open(); cmd.ExecuteNonQuery(); con.Close(); cmd = new SqlCommand(&#34;SELECT Id, SessionId FROM tblSesioData&#34;, con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); gvSessioDetails.DataSource = dt; gvSessioDetails.DataBind(); }</pre> <p><strong>VB.Net</strong></p> <p><strong>VB.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs) Session(&#34;Id&#34;) = txtId.Text Response.Redirect(&#34;Default.aspx&#34;) End Sub</pre> <p><strong>Default.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle.Me.Load Dim id As Integer = Convert.ToInt16(Session(&#34;Id&#34;)) Dim sessionId As String = Session.SessionID Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(&#34;INSERT INTO tblSesioData VALUES(@Id,@SessionId)&#34;, con) cmd.Parameters.AddWithValue(&#34;@Id&#34;, id) cmd.Parameters.AddWithValue(&#34;@SessionId&#34;, sessionId) con.Open() cmd.ExecuteNonQuery() con.Close() cmd = New SqlCommand(&#34;SELECT Id, SessionId FROM tblSesioData&#34;, con) Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) gvSessioDetails.DataSource = dt gvSessioDetails.DataBind() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/r3d9vDA.gif" alt="" width="248" height="251" /></p>https://www.aspforums.net:443/Threads/268280/Insert-Save-Session-ID-in-Database-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/268280/Insert-Save-Session-ID-in-Database-using-C-and-VBNet-in-ASPNet/Wed, 17 Jul 2019 03:25:32 GMT