Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class Tbl_RTp
Inherits System.Web.UI.Page
Protected objConn As SqlConnection
Protected strTID As String = HttpContext.Current.Request.QueryString("TID")
Protected strReplyNo As Integer = 1
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("key") <> "" Then
lblUsername.Text = Session("key")
Blogout.Visible = True
Else
Session("key") = ""
MsgBox("กรุณาลงชื่อเข้าสู่ระบบก่อนตั้งกระทู้ใหม่หรือตอบกระทู้")
Response.Redirect("Default.aspx")
End If
Dim strConnString As String
strConnString = "data source=TIP-TOP-PC\SQLEXPRESS;Initial Catalog=Webbel;Integrated Security=True"
objConn = New SqlConnection(strConnString)
objConn.Open()
ShowQuestion()
ShowReply()
End Sub
Protected Sub ShowQuestion()
Dim dtAdapter As SqlDataAdapter
Dim objCmd As SqlCommand
Dim dt As New DataTable
Dim strSQL As New StringBuilder
strSQL.Append("SELECT T_Name,T_Detail,Username,T_Date,Viewcount,Replycount FROM Tbl_NTp")
strSQL.Append(" WHERE TID = '" + strTID + "'")
dtAdapter = New SqlDataAdapter(strSQL.ToString, objConn)
dtAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
Me.lblQuestion.Text = dt.Rows(0)("T_Name")
Me.lblDetails.Text = dt.Rows(0)("T_Detail")
Me.lblName.Text = "<b>Username : </b>" & dt.Rows(0)("Username")
Me.lblCreateDate.Text = "<b>T_Date : </b>" & CDate(dt.Rows(0)("T_Date")).ToString("dd/mm/yyyy HH:mm")
Me.lblReply.Text = "<b>Replycount : </b>" & dt.Rows(0)("Replycount")
Me.lblView.Text = "<b>Viewcount : </b>" & dt.Rows(0)("Viewcount")
End If
'*** Update Number of View ***'
'strSQL.Remove(0, strSQL.Length)
strSQL.Append(" UPDATE Tbl_NTp SET Viewcount = Viewcount + 1 ")
strSQL.Append(" WHERE TID = @sTID ")
objCmd = New SqlCommand(strSQL.ToString(), objConn)
'objCmd.Parameters.Add("@sTID", SqlDbType.NVarChar).Value = strTID.ToString()
objCmd.Parameters.Add("@sTID", SqlDbType.VarChar).Value = strTID.ToString()
objCmd.ExecuteNonQuery()
End Sub
Protected Sub ShowReply()
Dim strSQL As New StringBuilder
Dim objCmd As SqlCommand
strSQL.Append(" SELECT * FROM Tbl_RTp ")
strSQL.Append(" WHERE TID = '" + strTID + "' ")
Dim dtReader As SqlDataReader
objCmd = New SqlCommand(strSQL.ToString, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to Repeater ***'
myRepeater.DataSource = dtReader
myRepeater.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Protected Sub ViewWebboard_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
objConn.Close()
objConn = Nothing
End Sub
Protected Sub Blogout_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Blogout.Click
If Session("key") <> "" Then
lblUsername.Text = ""
Blogout.Visible = False
Session("Key") = ""
End If
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Dim strSQL As New StringBuilder
Dim objCmd As SqlCommand
Dim sb As New StringBuilder
sb.Append("SELECT TOP 1 TID FROM Tbl_NTp ORDER BY TID Desc")
Dim sqlSave = sb.ToString
Dim lastID As String = ""
Dim com As New SqlCommand
Dim dr As SqlDataReader
With com
.Connection = objConn
.CommandType = CommandType.Text
.CommandText = sqlSave
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
Dim CurrentTID = dr.GetString(0)
Dim running As Integer = Convert.ToInt32(CurrentTID.Substring(1, CurrentTID.Length - 1))
running += 1
lastID = "R" & running.ToString("000000")
Else
lastID = "R000001"
End If
End With
'*** Insert Reply ***'
sb.Append(" INSERT INTO Tbl_RTp(TID,RID,R_Detail,R_date,username) ")
sb.Append(" VALUES ")
sb.Append(" (@sTID,@sRID,@sR_Detail,@sR_date,@susername) ")
objCmd = New SqlCommand(sb.ToString(), objConn)
objCmd.Parameters.Add("@sTID", SqlDbType.NVarChar).Value = strTID.ToString()
objCmd.Parameters.Add("@sRID", SqlDbType.NVarChar).Value = lastID
objCmd.Parameters.Add("@sR_Detail", SqlDbType.NVarChar).Value = Me.txtDetail.Text
objCmd.Parameters.Add("@sR_date", SqlDbType.DateTime).Value = DateTime.Now()
objCmd.Parameters.Add("@sUserName", SqlDbType.VarChar).Value = Me.lblUsername.Text
objCmd.ExecuteNonQuery()
'*** Update Number of Reply ***'
sb.Remove(0, sb.Length)
sb.Append(" UPDATE Tbl_NTp SET Replycount = Replycount + 1 ")
sb.Append(" WHERE TID = @sTID ")
sb.Append(" UPDATE Tbl_NTp SET Replycount = √")
sb.Append(" WHERE TID = @sTID ")
objCmd = New SqlCommand(sb.ToString(), objConn)
objCmd.Parameters.Add("@sTID", SqlDbType.NVarChar).Value = strTID.ToString()
objCmd.ExecuteNonQuery()
Response.Redirect("Tbl_RTp.aspx?TID=" & strTID)
End Sub
Protected Sub myRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
'*** No ***'
Dim lblReplyNo As Label = DirectCast(e.Item.FindControl("lblReplyNo"), Label)
If Not IsNothing(lblReplyNo) Then
lblReplyNo.Text = "No : " & strReplyNo
strReplyNo = strReplyNo + 1
End If
'*** Details ***'
Dim lblReplyDetails As Label = DirectCast(e.Item.FindControl("lblReplyDetails"), Label)
If Not IsNothing(lblReplyDetails) Then
lblReplyDetails.Text = e.Item.DataItem("R_Detail")
End If
'*** Name ***'
Dim lblReplyName As Label = DirectCast(e.Item.FindControl("lblReplyName"), Label)
If Not IsNothing(lblReplyName) Then
lblReplyName.Text = e.Item.DataItem("username")
End If
'*** CreateDate ***'
Dim lblReplyCreateDate As Label = DirectCast(e.Item.FindControl("lblReplyCreateDate"), Label)
If Not IsNothing(lblReplyCreateDate) Then
lblReplyCreateDate.Text = CDate(e.Item.DataItem("R_date")).ToString("dd/MM/yyyy HH:mm")
End If
End If
End Sub
End Class