Unable to insert in Nested DataList

Last Reply 6 hours ago By micah

Posted 6 days ago

i have been able to creat different method for inserting the two different IDs, now my challenge is on txtcommentpost in Datalist GetUSERCOMMENT2, i want to insert CommentID Column of TABLE A into table B ID column, i tried to do so and its not inserting, it can only insert ID Column of A table into CommentID Column of B table.

Code for Datalist GetUSERCOMMENT2

 

TextBox textBox = sender as TextBox;

        Label label = sender as Label;
        if (textBox.ID == "txtcommentpost")
        {
           // int comment2ID = Convert.ToInt32((textBox.NamingContainer.FindControl("lblId2") as Label).Text);
          int lblcomid = Convert.ToInt32((textBox.NamingContainer.FindControl("LBLCOM") as Label).Text);
          
            string comment = textBox.Text.Trim();
            
            string name = (textBox.NamingContainer.FindControl("lblnestname") as Label).Text;
            string username = (textBox.NamingContainer.FindControl("lblshId") as Label).Text;
           string imagename = (textBox.NamingContainer.FindControl("FImageName2") as Label).Text;
           Insert(lblcomid, comment, name, username, imagename, "PostComment2");

-----------------------------

 

<asp:DataList ID="GetUSERCOMMENTS2" runat="server" OnItemDataBound="GetUSERCOMMENTS2_ItemDataBound" Width="100%">
                        <ItemTemplate>
                            
                              <div class="media-left"  style="">
                                  
                                  </div>
                           
                           
                           
                       
        <article class="timeline-entry">

            <div class="timeline-entry-inner">

                <div class="timeline-icon bg-warning " style="">
                    <i class="entypo-feather">
                        
                    </i>
                </div>

                <div class="timeline-label">
                     
                    <h2 style="margin-bottom:2px"><a href="#" style=" margin-bottom:2px">
                          <asp:Label ID="lblfname" Text='<%#Eval("fName") %>' CssClass="" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="Black" Font-Size="Smaller" />
                        <asp:Label ID="lblUserNamecom" Text='<%#Eval("UserName") %>' CssClass="" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="Gray" Font-Size="Smaller" />
                        
                        
                        </a> 
                       
                    </h2>
                    
                    <span>
                        <asp:Label ID="LBLCOM" runat="server" Text='<%#Eval("CommentID") %>' Visible="true" />
                       //  <asp:Label ID="lblId2" runat="server" Text='<%#Eval("ID") %>' Visible="false" />
                         <asp:Label ID="lblComments2" runat="server" Text='<%#Eval("Comments") %>'  ForeColor="#333333" CssClass="" Font-Names="Arial" Font-Size="Smaller" Font-Bold="False" />
                       
                    </span>

 

Post Table

ID Username  Posts

10  mic22       hello world

--------------------------------

Comment A table

CommentID       Comments   ID

4               Nice one     10

----------------------------------

Comment B table

ID   Comment2   CommentID

8    i like that     1


 

 

 

You are viewing reply posted by: micah 5 days ago.
Posted 5 days ago

 

 protected void OnTextChanged(object sender, EventArgs e)
    {
        TextBox textBox = sender as TextBox;

        Label label = sender as Label;
        if (textBox.ID == "txtcommentpost")
        {
           // int comment2ID = Convert.ToInt32((textBox.NamingContainer.FindControl("lblId2") as Label).Text);
          int lblcomid = Convert.ToInt32((textBox.NamingContainer.FindControl("LBLCOM") as Label).Text);
          
            string comment = textBox.Text.Trim();
            
            string name = (textBox.NamingContainer.FindControl("lblnestname") as Label).Text;
            string username = (textBox.NamingContainer.FindControl("lblshId") as Label).Text;
           string imagename = (textBox.NamingContainer.FindControl("FImageName2") as Label).Text;
           Insert(lblcomid, comment, name, username, imagename, "PostComment2");
            //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + comment2 + " inserted into CommentB table')", true);
        }

        if (textBox.ID == "txtcommentpost2")
        {
            int comment3ID = Convert.ToInt32((textBox.NamingContainer.FindControl("Commentnest") as Label).Text);
            string comment3 = textBox.Text.Trim();
            string fname = (textBox.NamingContainer.FindControl("lblnestname") as Label).Text;
            string usernamenest = (textBox.NamingContainer.FindControl("lblshId") as Label).Text;
           Insert2(comment3ID, comment3, fname, usernamenest, "PostComment2");
            //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + comment3 + " inserted into CommentC table')", true);
        }
    }

    private DataTable BindDataList()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("CommentID", typeof(int));
        dt.Columns.Add("ID", typeof(int));
        dt.Rows.Add(1);
        return dt;
    }

    private void Insert(int id, string comment, string name, string username, string imagename, string tableName)
    {
        string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "INSERT INTO " + tableName + " (CommentID,UserName,fName,Comments,FImageName2) VALUES (@CommentID,@UserName,@fName,@Comments,@FImageName2)";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@CommentID", id);
                cmd.Parameters.AddWithValue("@Comments", comment);
                cmd.Parameters.AddWithValue("@fName", name);
             
                cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
                cmd.Parameters.AddWithValue("@FImageName2", imagename);
               
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
    private void Insert2(int id2, string usernamenest, string fname, string comment3, string tableName)
    {
        string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "INSERT INTO " + tableName + " (TableID,UserName,fName,Comments) VALUES (@TableId,@UserName,@fName,@Comments)";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@TableId", id2);
                cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
                cmd.Parameters.AddWithValue("@fName", fname);
                cmd.Parameters.AddWithValue("@Comments", comment3);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
    protected void GetUSERCOMMENTS2_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView dataRowView = e.Item.DataItem as DataRowView;
            string Id2 = dataRowView["CommentID"].ToString();
            DataList GetUSERCOMMENTS3 = e.Item.FindControl("GetUSERCOMMENTS3") as DataList;
            GetUSERCOMMENTS3.DataSource = GetData("SELECT TOP 5 ID,CommentID, UserName,fName,Comments,FImageName2,SendDate FROM PostComment2 WHERE CommentID = '" + Id2 + "'");
            GetUSERCOMMENTS3.DataBind();

        }
    }


    protected void GetMergedAll_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
          DataRowView dataRowView = e.Item.DataItem as DataRowView;
          string Id = dataRowView["ID"].ToString();
            DataList GetUSERCOMMENTS2 = e.Item.FindControl("GetUSERCOMMENTS2") as DataList;
            GetUSERCOMMENTS2.DataSource = GetData("SELECT TOP 5 ID,CommentID,UserName,fName,ID,Comments,FImageName,SendDate FROM PostComment WHERE ID = '" + Id + "'");
            GetUSERCOMMENTS2.DataBind();

            string commentId3 = (e.Item.FindControl("lbCOMlId") as Label).Text;

 

 
  <asp:DataList ID="GetMergedAll" runat="server">
                <ItemTemplate>


       






<asp:UpdatePanel ID="cooment" runat="server" >
     
                <ContentTemplate>
                <asp:HiddenField ID="hfUserNamelike" runat="server" />

                   
                   
             <asp:DataList ID="GetUSERCOMMENTS2" runat="server" OnItemDataBound="GetUSERCOMMENTS2_ItemDataBound" Width="100%">
                        <ItemTemplate>
                            
                              <div class="media-left"  style="">
                                  
                                  </div>
                           
                           
                           
                       
        <article class="timeline-entry">

            <div class="timeline-entry-inner">

                <div class="timeline-icon bg-warning " style="">
                    <i class="entypo-feather">
                         <a class="" href='<%#getUserHREF1(Container.DataItem)%>' >


                             <asp:Image ID="lblFImageName" runat="server" src='<%#getSRCcomment(Container.DataItem)%>' Width="35px" Height="35px"  CssClass="img-circle animated fadeInDown"/>
                                
                                       
                                           </a>
                    </i>
                </div>

                <div class="timeline-label">
                     
                    <h2 style="margin-bottom:2px"><a href="#" style=" margin-bottom:2px">
                          <asp:Label ID="lblfname" Text='<%#Eval("fName") %>' CssClass="" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="Black" Font-Size="Smaller" />
                        <asp:Label ID="lblUserNamecom" Text='<%#Eval("UserName") %>' CssClass="" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="Gray" Font-Size="Smaller" />
                        
                        
                        </a> 
                        <small>
                              <asp:Label ID="FavouriteID" Style="display: none;" runat="server" />
                             <asp:Label ID="lblDate" Text='<%#Eval("Senddate") %>' runat="server" CssClass="" Font-Bold="True" Font-Names="Arial" ForeColor="Black" Font-Size="Smaller" />
                        </small>
                    </h2>
                    
                    <span>
                        <asp:Label ID="LBLCOM" runat="server" Text='<%#Eval("CommentID") %>' Visible="true" />
                         <asp:Label ID="lblId2" runat="server" Text='<%#Eval("ID") %>' Visible="false" />
                         <asp:Label ID="lblComments2" runat="server" Text='<%#Eval("Comments") %>'  ForeColor="#333333" CssClass="" Font-Names="Arial" Font-Size="Smaller" Font-Bold="False" />
                       
                    </span>
                     <p class="media-body "  style="  ">
                                
                             
                                
                                <div class="clearfix"/>
                              
                                 <div class="" style=""/>
                                
                                 <span class="" style="margin-right:22px">
                                           <asp:LinkButton ID="btnlikcomment" runat="server" CssClass="fa fa-star-o" ForeColor="Silver">  
                                                <span class="" style="">
                                        <asp:Label ID="Label32" runat="server" Text="7 " Font-Size="Smaller" ForeColor="Gray"></asp:Label>
                                   </span>
                                           </asp:LinkButton></span><span class="" style="margin-right:6px"><asp:LinkButton ID="btnIncomm" runat="server" CssClass=" fa fa-comment-o" ForeColor="Silver"> 
                                                <span class="" style="">
                                        <asp:Label ID="Label30" runat="server" Text="12 " Font-Size="Smaller" ForeColor="Gray"></asp:Label>
                                   </span>
                                           </asp:LinkButton></span><span class=" text-left" style="margin-top:6px"><span class=" pull-right"><asp:LinkButton ID="LinkButton8" runat="server" ForeColor="#B0B0B0">
                                         <span class="" style="margin-right:8px">
                                         <asp:Label ID="Label29" runat="server" Text="" CssClass=" glyphicon glyphicon-eye-open" ForeColor="#B0B0B0" Font-Size="Smaller"></asp:Label>
                                   </span>
                                              
                                        

                                    </asp:LinkButton></span></span></p><div class="clearfix"></div>
                    <asp:Panel ID="Panel1" runat="server"  class=" commentpost">
                      
                          <div class="" style="margin-top:6px; margin-bottom:8px">
                                            <asp:TextBox ID="txtcommentpost" runat="server" CssClass=" form-control" placeholder="Reply to this comment..." AutoPostBack="true" OnTextChanged="OnTextChanged"></asp:TextBox><asp:Label ID="Label36" runat="server" Text="Hit Enter after typing " Font-Size="X-Small" ForeColor="Gray"></asp:Label><div class="pull-right">
                                  <asp:Label ID="Label38" runat="server" Text="500" Font-Size="Smaller" ForeColor="Gray"></asp:Label></div>
                          </div>

                    </asp:Panel>
                    <div class="clearfix"></div>
                    <asp:Panel ID="Pnlcomment" runat="server">
                    <asp:DataList ID="GetUSERCOMMENTS3" runat="server" >
                                <ItemTemplate>
                                     <div class="media-left"  style="margin-top:2px">
                                         <a class="timeline-item" href='<%#getUserHREF1(Container.DataItem)%>' >
                                               <img src='<%#getSRCcomment2(Container.DataItem)%>' class=" img-circle animated fadeInDown"
                                    style="width:25px; height:25px;" />
                                              <asp:Label ID="Label69" runat="server" Text="|" Font-Size="X-Large" ForeColor="#0099CC"></asp:Label></a></div><div class="media-body "  style="  ">
                                                    <asp:Label ID="lblnestname" Text='<%#Eval("fName") %>' CssClass="" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="Black" Font-Size="Smaller" />
                        <asp:Label ID="lblshId" Text='<%#Eval("UserName") %>' CssClass="" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="Gray" Font-Size="Smaller" />

                                <asp:Label ID="Commentnest" runat="server" Text='<%# Eval("CommentID") %>' Visible="False"></asp:Label>
                                 <br />
  <asp:Label ID="lblcomment" runat="server" Text='<%#Eval("Comments") %>'  ForeColor="#333333" CssClass="" Font-Names="Arial" Font-Size="Smaller" Font-Bold="False" />
                                
                                <div class="clearfix"/>
                              
                                 <div class="" style="margin-top:6px"/>
                                
                                 <span class="" style="margin-right:22px">
                                           <asp:LinkButton ID="LinkButton4" runat="server" CssClass="fa fa-star-o" ForeColor="Silver">  
                                                <span class="" style="">
                                        <asp:Label ID="Label32" runat="server" Text="7 " Font-Size="Smaller" ForeColor="Gray"></asp:Label>
                                   </span>
                                           </asp:LinkButton></span><span class="" style="margin-right:22px"><asp:LinkButton ID="LinkButton6" runat="server" CssClass="  glyphicon glyphicon-share-alt" ForeColor="Silver">  </asp:LinkButton></span><span class="" style="margin-right:6px"><asp:LinkButton ID="LinkButton7" runat="server" CssClass=" fa fa-comment-o" ForeColor="Silver"> 
                                                <span class="" style="">
                                        <asp:Label ID="Label30" runat="server" Text="12 " Font-Size="Smaller" ForeColor="Gray"></asp:Label>
                                   </span>
                                           </asp:LinkButton></span><span class=" text-left" style="margin-top:6px"><span class=" pull-right"><asp:LinkButton ID="LinkButton8" runat="server" ForeColor="#B0B0B0">
                                         <span class="" style="margin-right:8px">
                                         <asp:Label ID="Label29" runat="server" Text="" CssClass=" glyphicon glyphicon-eye-open" ForeColor="#B0B0B0" Font-Size="Smaller"></asp:Label>
                                   </span>
                                       
                                        

                                    </asp:LinkButton></span></span></div><div class="clearfix"></div>
                                       <div class="" style="margin-top:6px; margin-bottom:8px">
                                            <asp:TextBox ID="txtcommentpost2" runat="server" CssClass="commentpost2 form-control" placeholder="Reply to this comment..." AutoPostBack="true" OnTextChanged="OnTextChanged"></asp:TextBox><div class="pull-right">
                                  <asp:Label ID="Label38" runat="server" Text="500" Font-Size="Smaller" ForeColor="Gray"></asp:Label></div></div><div class="clearfix"></div>
                                    <div class="" style="margin-top:4px; margin-bottom:4px"></div>
                                </ItemTemplate>

                            </asp:DataList>
                        </asp:Panel>
                    

                   
                    
                </div>
            </div>

        </article>
                           
 </ItemTemplate>
                    </asp:DataList>

       
 </ContentTemplate>
                  
                    </asp:UpdatePanel>
       
       </ItemTemplate>
            </asp:DataList>

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html