Imports System.Data
Imports System.Data.SqlClient

Partial Class ViewPosts
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadRecentPosts()
        End If
    End Sub

    Private Sub LoadRecentPosts()
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("NaveedDBConnection").ConnectionString
        Dim query As String = "SELECT TOP 20 p.*, u.USER_NAME " &
                             "FROM POSTS p " &
                             "INNER JOIN USERS u ON p.USER_ID = u.ID " &
                             "ORDER BY p.CREATED_AT DESC"

        Using conn As New SqlConnection(connectionString)
            Dim cmd As New SqlCommand(query, conn)
            Dim adapter As New SqlDataAdapter(cmd)
            Dim dt As New DataTable()

            Try
                conn.Open()
                adapter.Fill(dt)
                rptPosts.DataSource = dt
                rptPosts.DataBind()
            Catch ex As Exception
                Response.Write("Error loading posts: " & ex.Message)
            End Try
        End Using
    End Sub
End Class


Imports System.Data Imports System.Data.SqlClient Partial Class CreatePost Inherits System.Web.UI.Page Protected Sub btnCreatePost_Click(sender As Object, e As EventArgs) Handles btnCreatePost.Click ' Get the current user ID (in a real app, this would come from your authentication system) Dim currentUserId As Integer = 1 ' Replace with actual user ID from session ' Validate input If String.IsNullOrEmpty(txtPostContent.Text) Then Return End If ' Create connection and command Dim connectionString As String = ConfigurationManager.ConnectionStrings("NaveedDBConnection").ConnectionString Using conn As New SqlConnection(connectionString) Dim cmd As New SqlCommand("INSERT INTO POSTS (POST_CONTENT, POST_TYPE, POST_URL, POST_LOCATION, USER_ID, CREATED_AT) VALUES (@content, @type, @url, @location, @userId, GETDATE())", conn) ' Add parameters cmd.Parameters.AddWithValue("@content", txtPostContent.Text) cmd.Parameters.AddWithValue("@type", ddlPostType.SelectedValue) cmd.Parameters.AddWithValue("@url", If(String.IsNullOrEmpty(txtPostUrl.Text), DBNull.Value, txtPostUrl.Text)) cmd.Parameters.AddWithValue("@location", If(String.IsNullOrEmpty(txtLocation.Text), DBNull.Value, txtLocation.Text)) cmd.Parameters.AddWithValue("@userId", currentUserId) Try conn.Open() cmd.ExecuteNonQuery() lblMessage.Text = "Post created successfully!" lblMessage.ForeColor = System.Drawing.Color.Green ' Clear fields after successful post txtPostContent.Text = "" txtPostUrl.Text = "" txtLocation.Text = "" Catch ex As Exception lblMessage.Text = "Error creating post: " & ex.Message End Try End Using End Sub End Class