473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

GridView, Update of post

7
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.
Expand|Select|Wrap|Line Numbers
  1. 'Access .NET framework for ADO library classes (place above the class code)
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Partial Class Blog
  5.     Inherits System.Web.UI.Page
  6.  
  7.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  8.  
  9.         Try
  10.             'Create Connection Object
  11.             Dim cn As New SqlClient.SqlConnection()
  12.             cn.ConnectionString = _
  13.             ConfigurationManager.ConnectionStrings("BlogPostCS").ConnectionString
  14.             ' Construct SQL, use @param to represent the new value
  15.             Dim sql As String
  16.             sql = "Insert into BlogTable(Title, Blog, Date, Name) "
  17.             sql = sql & "Values (@pTitle, @pBlog, @pDate, @pName)"
  18.             ' Create Command and add parameters
  19.             Dim cmd As New SqlCommand(sql, cn)
  20.             cmd.Parameters.AddWithValue("@pTitle", tb_title.Text)
  21.             cmd.Parameters.AddWithValue("@pBlog", tb_blog.Text)
  22.             cmd.Parameters.AddWithValue("@pDate", lb_date.Text)
  23.             ' Open connection, and ExecuteNonQuery
  24.             cn.Open()
  25.             cmd.ExecuteNonQuery()
  26.             cn.Close()
  27.             cmd.Dispose()
  28.             cn.Dispose()
  29.             ' Store Login id and Redirect to main page
  30.             Session("Username") = lb_login.Text
  31.             Response.Redirect("DashBoard.aspx")
  32.         Catch ex As Exception
  33.             lb_msg.Text = "Error! Blog Post Not Added"
  34.         End Try
  35.  
  36.         Response.Redirect("Blogtable.aspx")
  37.  
  38.     End Sub
  39.  
  40.     Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  41.  
  42.         'Create Connection Object
  43.         Dim cn As New SqlClient.SqlConnection()
  44.         cn.ConnectionString = _
  45.         ConfigurationManager.ConnectionStrings("BlogPostCS").ConnectionString
  46.         ' Construct SQL, use @param to represent the new value
  47.         Dim sql As String
  48.         sql = "Update BlogTable Set "
  49.  
  50.         sql = sql & "Title=@pTitle, "
  51.         sql = sql & "Blog=@pBlog, "
  52.         sql = sql & "Where Username=@pUsername "
  53.         ' Create Command and add parameters
  54.         Dim cmd As New SqlCommand(sql, cn)
  55.         'cmd.Parameters.AddWithValue("@pUsername", tb_login.Text)
  56.         cmd.Parameters.AddWithValue("@pTitle", tb_title.Text)
  57.         cmd.Parameters.AddWithValue("@pBlog", tb_blog.Text)
  58.         cmd.Parameters.AddWithValue("@pUsername", Session("Username"))
  59.         ' Open connection, and ExecuteNonQuery
  60.         cn.Open()
  61.         cmd.ExecuteNonQuery()
  62.         cn.Close()
  63.         cmd.Dispose()
  64.         cn.Dispose()
  65.  
  66.     End Sub
  67.  
  68.     Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
  69.         Response.Redirect("Blogtable.aspx")
  70.     End Sub
  71.  
  72.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  73.  
  74.         'Dim thisDate As Date
  75.         'thisDate = Today
  76.         'tb_date.Text = Convert.ToString(thisDate)
  77.  
  78.         Dim currentTime As System.DateTime = System.DateTime.Now
  79.         lb_date.Text = Convert.ToString(currentTime)
  80.  
  81.         'Create Connection Object
  82.         Dim cn As New SqlClient.SqlConnection()
  83.         cn.ConnectionString = _
  84.         ConfigurationManager.ConnectionStrings("UserCS").ConnectionString
  85.         ' Construct SQL, use @param to represent the new value
  86.         Dim sql As String
  87.         sql = "Select * From UserTable where Username = @pUsername"
  88.         ' Create Command and Data Reader
  89.         Dim cmd As New SqlCommand(sql, cn)
  90.         cmd.Parameters.AddWithValue("@pUsername", Session("Username"))
  91.         Dim dr As SqlDataReader
  92.         ' Open connection, and ExecuteReader
  93.         cn.Open()
  94.         dr = cmd.ExecuteReader
  95.         If dr.Read Then
  96.             lb_login.Text = dr("Username").ToString
  97.             lb_name.Text = dr("Name").ToString
  98.  
  99.         End If
  100.         cn.Close()
  101.         cmd.Dispose()
  102.         cn.Dispose()
  103.     End Sub
  104. End Class
  105.  
--------------
And here is the interface for the gridview part:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Blogtable.aspx.vb" Inherits="Blogtable" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body background="http://bytes.com/images/ooo1.1-elements-background_v1.png">
  10.     <form id="form1" runat="server">
  11.     <div>
  12.  
  13.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
  14.             DataSourceID="SqlDataSource1">
  15.             <Columns>
  16.                 <asp:BoundField DataField="Blog" HeaderText="Blog" SortExpression="Blog" />
  17.                 <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
  18.                 <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
  19.                 <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
  20.             </Columns>
  21.         </asp:GridView>
  22.  
  23.     </div>
  24.     <p>
  25.         <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  26.             ConnectionString="<%$ ConnectionStrings:BlogCS %>" 
  27.             SelectCommand="SELECT * FROM [BlogTable]"></asp:SqlDataSource>
  28.     </p>
  29.     <p>
  30.         &nbsp;</p>
  31.     </form>
  32. </body>
  33. </html>
  34.  
-------------------
Please help, I really have no idea what went wrong :(
Jan 7 '09 #1
4 3901
ahling
7
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 =(
Jan 8 '09 #2
Frinavale
9,735 Expert Mod 8TB
Are you using the GridView.DataBind() method to bind the data you've retrieved to the GridView?
Jan 8 '09 #3
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
Jan 8 '09 #4
Frinavale
9,735 Expert Mod 8TB
@Frinavale
Ok, I now that I see your code for updating the blog I can see that you are not using the GridView.DataBind() method.

Are you sure that the following sql statement works?
Expand|Select|Wrap|Line Numbers
  1.  sql = "Update BlogTable Set "
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 "UpdateBlogEventHandler" 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.
Jan 8 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: | last post by:
Hello, I have created an ASP.NET 2.0 application that utilized a Gridview Control to display and update/delete data. The problem I am having is that the gridview control is displaying the data...
4
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I...
5
by: GaryDean | last post by:
I have a GridView that works fine when I bind it to an ObjectDataSource at design time. But if I bind it at run time nothing happens. On a button click event I say... GridView1.DataSourceID =...
1
by: jmdolinger | last post by:
Hi all, I'm a newbie to Atlas (and recently ASP.NET) after coming from a long Java background, also have done quite a bit with an Ajax.NET/ASP.NET 1.1 project, but it was basically all...
4
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which -...
2
by: BenCoo | last post by:
Hello, In a gridview I've a checkbox (indicating a flag that is set or not). I fill the gridview via the wizard from ADO.NET 2.0 that generates automaticly the Insert, Update and Delete...
2
by: Looch | last post by:
Hi, I'm filling a GridView with a simple sproc select statement. I'm trying to use the Update tab to create an update statement and using the following: Update Shipping_Requests Set Shipped...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
4
by: BiffMaGriff | last post by:
Hello, I have a GridView that I put inside an update panel. I have a control that attaches to the datasource of the gridview that filters the data, databinds the gridview and then updates the...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.