Hi all.
As mentioned in my first post, I'm currently doing an asp.net blog where user can update their profile, and update their blog entry.
But I encountered problem with the update of entry part.
When I tried to post a blog entry by hitting the 'Post' button, it brings me to a blank page which is supposed to show me the gridview that contains the Blog subject, title of the blog entry, date and the name of the poster itself.
But what exactly went wrong?
I don't know, I have been pondering over this for quite some time and I seriously need help =(
Here's the code for the page where user can enter their blog entry.
[code=cpp]
'Access .NET framework for ADO library classes (place above the class code)
Imports System.Data
Imports System.Data.Sql Client
Partial Class Blog
Inherits System.Web.UI.P age
Protected Sub Button1_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button1.Click
Try
'Create Connection Object
Dim cn As New SqlClient.SqlCo nnection()
cn.ConnectionSt ring = _
ConfigurationMa nager.Connectio nStrings("BlogP ostCS").Connect ionString
' Construct SQL, use @param to represent the new value
Dim sql As String
sql = "Insert into BlogTable(Title , Blog, Date, Name) "
sql = sql & "Values (@pTitle, @pBlog, @pDate, @pName)"
' Create Command and add parameters
Dim cmd As New SqlCommand(sql, cn)
cmd.Parameters. AddWithValue("@ pTitle", tb_title.Text)
cmd.Parameters. AddWithValue("@ pBlog", tb_blog.Text)
cmd.Parameters. AddWithValue("@ pDate", lb_date.Text)
' Open connection, and ExecuteNonQuery
cn.Open()
cmd.ExecuteNonQ uery()
cn.Close()
cmd.Dispose()
cn.Dispose()
' Store Login id and Redirect to main page
Session("Userna me") = lb_login.Text
Response.Redire ct("DashBoard.a spx")
Catch ex As Exception
lb_msg.Text = "Error! Blog Post Not Added"
End Try
Response.Redire ct("Blogtable.a spx")
End Sub
Protected Sub Button2_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button2.Click
'Create Connection Object
Dim cn As New SqlClient.SqlCo nnection()
cn.ConnectionSt ring = _
ConfigurationMa nager.Connectio nStrings("BlogP ostCS").Connect ionString
' Construct SQL, use @param to represent the new value
Dim sql As String
sql = "Update BlogTable Set "
sql = sql & "Title=@pTi tle, "
sql = sql & "Blog=@pBlo g, "
sql = sql & "Where Username=@pUser name "
' Create Command and add parameters
Dim cmd As New SqlCommand(sql, cn)
'cmd.Parameters .AddWithValue(" @pUsername", tb_login.Text)
cmd.Parameters. AddWithValue("@ pTitle", tb_title.Text)
cmd.Parameters. AddWithValue("@ pBlog", tb_blog.Text)
cmd.Parameters. AddWithValue("@ pUsername", Session("Userna me"))
' Open connection, and ExecuteNonQuery
cn.Open()
cmd.ExecuteNonQ uery()
cn.Close()
cmd.Dispose()
cn.Dispose()
End Sub
Protected Sub Button3_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button3.Click
Response.Redire ct("Blogtable.a spx")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
'Dim thisDate As Date
'thisDate = Today
'tb_date.Text = Convert.ToStrin g(thisDate)
Dim currentTime As System.DateTime = System.DateTime .Now
lb_date.Text = Convert.ToStrin g(currentTime)
'Create Connection Object
Dim cn As New SqlClient.SqlCo nnection()
cn.ConnectionSt ring = _
ConfigurationMa nager.Connectio nStrings("UserC S").ConnectionS tring
' Construct SQL, use @param to represent the new value
Dim sql As String
sql = "Select * From UserTable where Username = @pUsername"
' Create Command and Data Reader
Dim cmd As New SqlCommand(sql, cn)
cmd.Parameters. AddWithValue("@ pUsername", Session("Userna me"))
Dim dr As SqlDataReader
' Open connection, and ExecuteReader
cn.Open()
dr = cmd.ExecuteRead er
If dr.Read Then
lb_login.Text = dr("Username"). ToString
lb_name.Text = dr("Name").ToSt ring
End If
cn.Close()
cmd.Dispose()
cn.Dispose()
End Sub
End Class
[/code]
--------------
And here is the interface for the gridview part:
[code=asp]
<%@ Page Language="VB" AutoEventWireup ="false" CodeFile="Blogt able.aspx.vb" Inherits="Blogt able" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body background="htt p://bytes.com/images/ooo1.1-elements-background_v1.p ng">
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns="False"
DataSourceID="S qlDataSource1">
<Columns>
<asp:BoundFie ld DataField="Blog " HeaderText="Blo g" SortExpression= "Blog" />
<asp:BoundFie ld DataField="Titl e" HeaderText="Tit le" SortExpression= "Title" />
<asp:BoundFie ld DataField="Date " HeaderText="Dat e" SortExpression= "Date" />
<asp:BoundFie ld DataField="Name " HeaderText="Nam e" SortExpression= "Name" />
</Columns>
</asp:GridView>
</div>
<p>
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:BlogCS %>"
SelectCommand=" SELECT * FROM [BlogTable]"></asp:SqlDataSour ce>
</p>
<p>
</p>
</form>
</body>
</html>
[/code]
-------------------
Please help, I really have no idea what went wrong :(
As mentioned in my first post, I'm currently doing an asp.net blog where user can update their profile, and update their blog entry.
But I encountered problem with the update of entry part.
When I tried to post a blog entry by hitting the 'Post' button, it brings me to a blank page which is supposed to show me the gridview that contains the Blog subject, title of the blog entry, date and the name of the poster itself.
But what exactly went wrong?
I don't know, I have been pondering over this for quite some time and I seriously need help =(
Here's the code for the page where user can enter their blog entry.
[code=cpp]
'Access .NET framework for ADO library classes (place above the class code)
Imports System.Data
Imports System.Data.Sql Client
Partial Class Blog
Inherits System.Web.UI.P age
Protected Sub Button1_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button1.Click
Try
'Create Connection Object
Dim cn As New SqlClient.SqlCo nnection()
cn.ConnectionSt ring = _
ConfigurationMa nager.Connectio nStrings("BlogP ostCS").Connect ionString
' Construct SQL, use @param to represent the new value
Dim sql As String
sql = "Insert into BlogTable(Title , Blog, Date, Name) "
sql = sql & "Values (@pTitle, @pBlog, @pDate, @pName)"
' Create Command and add parameters
Dim cmd As New SqlCommand(sql, cn)
cmd.Parameters. AddWithValue("@ pTitle", tb_title.Text)
cmd.Parameters. AddWithValue("@ pBlog", tb_blog.Text)
cmd.Parameters. AddWithValue("@ pDate", lb_date.Text)
' Open connection, and ExecuteNonQuery
cn.Open()
cmd.ExecuteNonQ uery()
cn.Close()
cmd.Dispose()
cn.Dispose()
' Store Login id and Redirect to main page
Session("Userna me") = lb_login.Text
Response.Redire ct("DashBoard.a spx")
Catch ex As Exception
lb_msg.Text = "Error! Blog Post Not Added"
End Try
Response.Redire ct("Blogtable.a spx")
End Sub
Protected Sub Button2_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button2.Click
'Create Connection Object
Dim cn As New SqlClient.SqlCo nnection()
cn.ConnectionSt ring = _
ConfigurationMa nager.Connectio nStrings("BlogP ostCS").Connect ionString
' Construct SQL, use @param to represent the new value
Dim sql As String
sql = "Update BlogTable Set "
sql = sql & "Title=@pTi tle, "
sql = sql & "Blog=@pBlo g, "
sql = sql & "Where Username=@pUser name "
' Create Command and add parameters
Dim cmd As New SqlCommand(sql, cn)
'cmd.Parameters .AddWithValue(" @pUsername", tb_login.Text)
cmd.Parameters. AddWithValue("@ pTitle", tb_title.Text)
cmd.Parameters. AddWithValue("@ pBlog", tb_blog.Text)
cmd.Parameters. AddWithValue("@ pUsername", Session("Userna me"))
' Open connection, and ExecuteNonQuery
cn.Open()
cmd.ExecuteNonQ uery()
cn.Close()
cmd.Dispose()
cn.Dispose()
End Sub
Protected Sub Button3_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button3.Click
Response.Redire ct("Blogtable.a spx")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
'Dim thisDate As Date
'thisDate = Today
'tb_date.Text = Convert.ToStrin g(thisDate)
Dim currentTime As System.DateTime = System.DateTime .Now
lb_date.Text = Convert.ToStrin g(currentTime)
'Create Connection Object
Dim cn As New SqlClient.SqlCo nnection()
cn.ConnectionSt ring = _
ConfigurationMa nager.Connectio nStrings("UserC S").ConnectionS tring
' Construct SQL, use @param to represent the new value
Dim sql As String
sql = "Select * From UserTable where Username = @pUsername"
' Create Command and Data Reader
Dim cmd As New SqlCommand(sql, cn)
cmd.Parameters. AddWithValue("@ pUsername", Session("Userna me"))
Dim dr As SqlDataReader
' Open connection, and ExecuteReader
cn.Open()
dr = cmd.ExecuteRead er
If dr.Read Then
lb_login.Text = dr("Username"). ToString
lb_name.Text = dr("Name").ToSt ring
End If
cn.Close()
cmd.Dispose()
cn.Dispose()
End Sub
End Class
[/code]
--------------
And here is the interface for the gridview part:
[code=asp]
<%@ Page Language="VB" AutoEventWireup ="false" CodeFile="Blogt able.aspx.vb" Inherits="Blogt able" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body background="htt p://bytes.com/images/ooo1.1-elements-background_v1.p ng">
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns="False"
DataSourceID="S qlDataSource1">
<Columns>
<asp:BoundFie ld DataField="Blog " HeaderText="Blo g" SortExpression= "Blog" />
<asp:BoundFie ld DataField="Titl e" HeaderText="Tit le" SortExpression= "Title" />
<asp:BoundFie ld DataField="Date " HeaderText="Dat e" SortExpression= "Date" />
<asp:BoundFie ld DataField="Name " HeaderText="Nam e" SortExpression= "Name" />
</Columns>
</asp:GridView>
</div>
<p>
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:BlogCS %>"
SelectCommand=" SELECT * FROM [BlogTable]"></asp:SqlDataSour ce>
</p>
<p>
</p>
</form>
</body>
</html>
[/code]
-------------------
Please help, I really have no idea what went wrong :(


Comment