Solved

Retrieving Sql Data And Storing In Session Variables

Posted on 2014-03-27
4
362 Views
Last Modified: 2014-04-01
Hi

I'm trying/want to store the results of a sql query in session variables, how do I do this?

My code so far works and returns results to a gridview..

        If True Then
            'Establishing the MySQL Connection
            Dim strConnString As String = ConfigurationManager.ConnectionStrings("ZUConnectionString").ConnectionString

            Dim sqlConnection1 As New SqlConnection(strConnString)

            Dim query As String
            Dim SqlCommand As SqlCommand
            Dim reader As SqlDataReader

            Dim adapter As New SqlDataAdapter()
            'Open the connection to db
            sqlConnection1.Open()

            'Generating the query to fetch the contact details
            query = "SELECT * FROM All"

            SqlCommand = New SqlCommand(query, sqlConnection1)
            adapter.SelectCommand = New SqlCommand(query, sqlConnection1)
            'execute the query
            reader = SqlCommand.ExecuteReader()
            'Assign the results 
            GridView1.DataSource = reader

            'Bind the data
            GridView1.DataBind()

Open in new window



I'd like to store each returned field as  

  Session("xxxxxx") =



Thanks
0
Comment
Question by:edjones1
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
It would be simpler to load the data into a DataTable and store that - plus you can't use a DataReader in this context because it is read-forward only so you can't bind to it and then read it again manually:
        If True Then
            'Establishing the MySQL Connection
            Dim strConnString As String = ConfigurationManager.ConnectionStrings("ZUConnectionString").ConnectionString

            Dim sqlConnection1 As New SqlConnection(strConnString)
            Dim query As String

            Dim adapter As New SqlDataAdapter()
            Dim table As New DataTable()

            'Generating the query to fetch the contact details
            query = "SELECT * FROM All"

            adapter.SelectCommand = New SqlCommand(query, sqlConnection1)
            adapter.Fill(table)

            'Assign the results 
            GridView1.DataSource = table

           '// Store in session
           Session("Table") = table

            'Bind the data
            GridView1.DataBind()

Open in new window

The bigger question, of course, is why do you want to store it in a Session variable? Holding large amounts of data in user specific context is usually a bad idea.
0
 

Accepted Solution

by:
edjones1 earned 0 total points
Comment Utility
Thanks for you help.

It can be done though and I've managed to get it working now.

See the code below. It works on a selected gridview row.

Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        Dim row As GridViewRow = GridView1.SelectedRow

        Dim Val = row.Cells(0).Text





        Dim strConn As String = System.Configuration.ConfigurationManager.ConnectionStrings("QuoteConnectionString").ToString()
        Dim sql As String = "SELECT * FROM AllQuotes where QuoteID =@QuoteID"
        Dim conn As New Data.SqlClient.SqlConnection(strConn)
        Dim objDR As Data.SqlClient.SqlDataReader
        Dim Cmd As New Data.SqlClient.SqlCommand(sql, conn)

        Cmd.Parameters.AddWithValue("@username", row.Cells(0).Text)

        conn.Open()
        objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
        While objDR.Read()
    


            Session("LoanAmount") = objDR("LoanAmount")
            Session("LoanTerm") = objDR("LoanTerm")
     


        End While

        Response.Redirect("xxxxxxx.aspx")
    End Sub

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
That isn't quite what you asked for in your question....but at least you're sorted.
0
 

Author Closing Comment

by:edjones1
Comment Utility
Worked it out myself
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now