GridView, Update of post

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahling
    New Member
    • Jan 2009
    • 7

    GridView, Update of post

    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>
    &nbsp;</p>
    </form>
    </body>
    </html>
    [/code]
    -------------------
    Please help, I really have no idea what went wrong :(
    Last edited by Frinavale; Jan 8 '09, 03:28 PM. Reason: added [code] tags
  • ahling
    New Member
    • Jan 2009
    • 7

    #2
    Gridview doesnt show up.. even though...

    I'm using SQL Database to insert the datas in the database to the gridview. But the problem is, the gridview doesn't show up even though there are datas contained in the database. When I run the test query, there are datas contained in it. But why doesn't the gridview show up??

    Here is what I saw when I run the test query:




    This is where my gridview is placed:




    Ya, as mentioned before, I'm doing a asp.net blog where users can update/edit their profile, as well as update their blog entries. I'm having problems with the updating of blog entries cuz the gridview doesn't show up. What could go wrong? The place where I placed the gridview, I do not have to insert any codes in the asp.vb file right?


    Thanks, please help, I'm going crazy over this gridview =(

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Are you using the GridView.DataBi nd() method to bind the data you've retrieved to the GridView?

      Comment

      • Ahmedhussain
        New Member
        • Dec 2008
        • 79

        #4
        hi ahling,

        All you have to do is to connect the database with your datagrid.
        there are two methods. One is to dataset and then bid your dataset with gridview through defining your datasource of gridview.

        The other way is more simpler . there is an sqldatasource available in toolbox datapanel. use that datasource .. There is a tag showed up with it saying configure datasource ..Simply click on it and then add a connection to your database....
        then there will be a tag present with the gridview asking you to choose a datasource ... And providing you a small dropdownlist ...

        try it will help you out

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by Frinavale
          Are you using the GridView.DataBi nd() method to bind the data you've retrieved to the GridView?
          Ok, I now that I see your code for updating the blog I can see that you are not using the GridView.DataBi nd() method.

          Are you sure that the following sql statement works?
          [code=vbnet] sql = "Update BlogTable Set "[/code]
          Try this using Query Analizer to see if it updates properly.

          Anyways, after you update the blog, you need to retrieve the updated data from the database and bind the data you've retrieved to the GridView. I'm not seeing any code that does this and so this is probably the reason why your GridView is not displaying.



          In order for us to better understand your code could you Please name your buttons with meaningful names? It's hard to tell what Button1 does and what Button2 does. It would be easier for us to understand your code if you named your buttons "UpdateBlog " or "ShowNextPage". ..well really if you named your methods something like "UpdateBlogEven tHandler" or something. Also, it would be easier for us to help you if you only posted the code you are having problems with instead of posting the entire solution.

          Comment

          Working...