Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 23rd, 2009, 03:39 AM
Authorized User
Join Date: Jan 2009
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
Default Gridview, databind retrieving value from database

Hey people. I am currently doing a social portal like Facebook. What i am facing over now is whenever a user makes an update, for example : Posted a new status or note.

Below, when pple click on the Note button, it will display the top 5 updates from the database. But this raises a question. Every update will have an UpdateID, but how do i actually know what updateID are they each.
This is because i allow other users to comment on each of these update. Without capturing the UpdateID, i will have trouble in inserting the right Comments onto the status or note.

Hope you guys understand what i am trying to intepret over here.

Source code

<asp:TextBoxID="CommentText"runat="server"Text='<%# Bind("FullName") %>'></asp:TextBox>
<ahref=""><asp:LabelID="Label1"runat="server"Text='<%# Bind("FullName") %>'></asp:Label></a> wrote a note.<br/><br/>
<asp:LabelID="Label2"runat="server"Text='<%# Bind("TextInput") %>'></asp:Label><br/><br/><asp:ButtonID="WriteComment"runat="server"Text="Write Comment"Visible="true"/><br/><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
<asp:ButtonID="ViewComment"runat="server"OnClientClick="window.open('ViewComment.aspx','','left=100,top= 50,width=500,height=500,scrollbars=1');return false;"PostBackUrl="~/ViewComment.aspx"Text="View Comment"Visible="false"/>

cs code

protectedvoid NotePost_Click(object sender, EventArgs e)
foreach (GridViewRow row in GridView1.Rows)
// Selects the text from the TextBox
// which is inside the GridView control
string textBoxText = ((TextBox)row.FindControl("Notestxtbox")).Text;
TextBox noteText = ((TextBox)row.FindControl("Notestxtbox"));
if (textBoxText != "")
string strConnectionString = ConfigurationManager.ConnectionStrings["SocialSystemConnectionString"].ConnectionString;
SqlConnection myConnect = newSqlConnection(strConnectionString);
SqlCommand cmd = newSqlCommand("INSERT INTO [Comment] (TypeId,Email,TextInput,UpdateID) VALUES (@TypeId,@Email,@TextInput,@UpdateID)", myConnect);
"@TypeId", SqlDbType.VarChar);
"@TypeId"].Value = "Notes";
"@Email", SqlDbType.VarChar);
"@Email"].Value = Emaillbl.Text; //Session
cmd.Parameters.Add("@TextInput", SqlDbType.VarChar);
"@TextInput"].Value = textBoxText;
"@UpdateID", SqlDbType.VarChar);
"@UpdateID"].Value = UpdateIDlbl.Text; //Session
noteText.Text =

protectedvoid Notebtn_Click(object sender, EventArgs e)
GridView1.Visible = true;
GridView2.Visible = false;
GridView3.Visible = false;
string strConString = ConfigurationManager.ConnectionStrings["SocialSystemConnectionString"].ConnectionString;
SqlConnection con = newSqlConnection(strConString);
SqlCommand cmd = newSqlCommand("SELECT TOP 5 Types.TypeID, [Update].TextInput, [Update].UpdateID, [User].FullName FROM Types INNER JOIN [Update] ON Types.TypeID = [Update].TypeId INNER JOIN [User] ON [Update].Email = [User].Email WHERE [Update].TypeId = 'Notes' ORDER BY [Update].UpdateTime DESC", con);
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;

Last edited by julius; March 23rd, 2009 at 03:55 AM..
  #2 (permalink)  
Old March 23rd, 2009, 11:33 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

Its late here so my mind is definately not firing on all cylinders but here are a few thoughts:

Why is the UpdateID of any consequence? A comment or otherwise to a users profile is related to the user. The UpdateID, as I have understood it, is simply the ID of said update. Is the UpdateID supposed to be the ID of the user whom something is being commented on? What I mean by this is if you were posting a comment about something on my profile, would the UpdateID by my UserID from the database??

Next, you should not place your database code directly into your code behind this makes for a mainteance nightmare! Look into breaking this out into a DAL all its own so as to encapsulate your data access code in one place.

Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."

Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving value from textbox in a Gridview julius C# 2005 1 March 12th, 2009 08:12 AM
Databind a gridview question. chobo2 ASP.NET 3.5 Basics 4 March 8th, 2009 04:38 PM
DataView will not DataBind with a GridView?! jason.l ASP.NET 2.0 Basics 0 October 13th, 2006 10:01 PM
Gridview DataBind to DataTable not showing changes wirerider ASP.NET 2.0 Basics 1 March 4th, 2006 08:59 PM
databind database to label svinson6 ASP.NET 1.0 and 1.1 Basics 3 October 17th, 2004 04:18 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.