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:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"BorderStyle="None"Height="0px"Width="443px"GridLines="None"OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBoxID="CommentText"runat="server"Text='<%# Bind("FullName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<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/>
<asp:TextBoxID="Notestxtbox"runat="server"Columns="36"TextMode="MultiLine"MaxLength="100"Height="60px"Width="304px"Visible="False"></asp:TextBox>
&n bsp; &nbs p;
&n bsp; &nbs p;
<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"/>
<asp:ButtonID="NotePost"runat="server"OnClick="NotePost_Click"Text="Post"Visible="false"/> <br/><br/>
</ItemTemplate>
</asp:TemplateField></Columns>
</asp:GridView>
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);
cmd.Parameters.Add("@TypeId", SqlDbType.VarChar);
cmd.Parameters["@TypeId"].Value = "Notes";
cmd.Parameters.Add("@Email", SqlDbType.VarChar);
cmd.Parameters["@Email"].Value = Emaillbl.Text; //Session
cmd.Parameters.Add("@TextInput", SqlDbType.VarChar);
cmd.Parameters["@TextInput"].Value = textBoxText;
cmd.Parameters.Add("@UpdateID", SqlDbType.VarChar);
cmd.Parameters["@UpdateID"].Value = UpdateIDlbl.Text; //Session
myConnect.Open();
cmd.ExecuteNonQuery();
myConnect.Close();
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);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
con.Close();
}
Last edited by julius; March 23rd, 2009 at 03:55 AM..
|