Last post Apr 17, 2018 08:59 AM by Eric Du
Apr 16, 2018 02:45 PM|charliekeegan|LINK
How do i connect this SQL statement to a gridview? My database is Microsoft Access.
Select Posts.[Post_Value], Posts.[Date_Time] FROM
Posts WHERE posts.[user_ID] = '" & Session("user_id") & "'
My database connection as a string is this:
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & Server.MapPath("database/Database.mdb")
I know i have to open the connection to the database, but what i cant figure out is how to fill the gridview with the data including the session variable? I also want this to happen on page load.
I am doing this in windows visual studio, so if you know what i need to do on the html side aswell that would be a huge help!
This is as far as i have got with my code, but i know its rubbish and may not be anywhere near what i need.
Private Sub Page_Load1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
Dim strSQL As String = "Select Posts.[Post_Value], Posts.[Date_Time]
FROM Posts WHERE posts.[user_ID] = '" & Session("user_id") & "'"
Dim connection As New OleDbConnection(connectionString)
Dim oledb As OleDbDataReader(connection)
Dim dataadapter As New OleDbDataAdapter(strSQL, connection)
Dim ds As New DataSet()
GridView1.DataSource = ds
GridView1.DataMember = "posts"
In case i haven't been clear, what i would like to have at the end of this is: - at page load, a gridview will appear with all the posts from the user that is signed in to the session.
Honestly, any pointers would be hugely appreciated!
Apr 17, 2018 08:59 AM|Eric Du|LINK
According to your description and code, you could check the following tutorial about how to bind database data to gridview:
ASP.Net MySQL CRUD: Select Insert Edit Update and Delete in GridView using MySQL database in ASP.Net:
Also you could try the following code to access session value, then use it directly.
string s = Session["UserName"].ToString();