Asked by:
Inserting into a nested gridview

Question
-
User-1402564948 posted
Happy New Year!
As an ametuer, I can finally sync my nested gridview and the parent.
However, I need to edit the nested gridview as a comment reply insert.
There are many solutions online that I've tried, but I haven't had any luck. I tried all day yesterday to insert one record. No go.
I tried a button with a method, a detailsview, OnRowDataBound, but I can't seem to insert a record.
Can you please take a look? Of course, I'm trying to do this with ajax update panels. I tried both postback and async triggers.
Thank you! The code below is markup, button click, and OnRowDataBound, respectively.
<asp:UpdatePanel ID="UpdatePanel8" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Panel ID="Panel5" runat="server"> <asp:TextBox ID="getwId" runat="server" Visible="false" Text='<%# Eval("wId") %>'></asp:TextBox> <asp:TextBox ID="rComment" runat="server" CssClass="bhandle"></asp:TextBox> <asp:LinkButton ID="LinkButton1" runat="server" Text="Reply" CssClass="bhandlebttn" OnClick="LinkButton1_Click" /></LinkButton> <asp:Label runat="server" ID="eMessage" Text=""></asp:Label> </asp:Panel> <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" GridLines="None" CssClass = "" ShowHeader="False"> <Columns> <asp:BoundField ItemStyle-Width="150px" DataField="Id" HeaderText="Id" Visible="false" /> <asp:BoundField ItemStyle-Width="150px" DataField="userName" HeaderText="userName" /> <asp:BoundField ItemStyle-Width="150px" DataField="Reply" HeaderText="Reply" /> </Columns> </asp:GridView> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="LinkButton1" /> </Triggers> </asp:UpdatePanel>
Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) Dim rComment As TextBox = DirectCast(GridView1.Rows(0).FindControl("rComment"), TextBox) If rComment.Text IsNot Nothing Then 'If sender Is Nothing Then ' Throw New ArgumentNullException(NameOf(sender)) ' End If 'Dim bReply As Button = DirectCast(gvOrders.Rows(0).FindControl("bReply"), Button) 'Dim rComment As TextBox = DirectCast(GridView1.Rows(0).FindControl("rComment"), TextBox) Try Dim strConn As [String] = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString Dim conn As New SqlConnection(strConn) Dim strQ As String = "INSERT INTO Replies(Id, UserName, Reply, wId) Values (@Id, @UserName, @Reply, @wId)" 'Dim User As String = HttpContext.Current.User.Identity.Name 'You need to use the GetUserId() method to get the userid value 'Dim strConn As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString 'Dim conn As New SqlConnection(strConn) conn.Open() Dim cmd As New SqlCommand(strQ, conn) 'myCommand.Parameters.AddWithValue("@Subject", Subject.Text.Trim()) Dim gvOrders As GridView = DirectCast(GridView1.Rows(0).FindControl("gvOrders"), GridView) Dim UserName As String = HttpContext.Current.User.Identity.GetUserName cmd.Parameters.AddWithValue("@Reply", value:=SqlDbType.NVarChar).Value = rComment.Text cmd.Parameters.AddWithValue("@wId", value:=SqlDbType.Int).Value = DirectCast(GridView1.Rows(0).FindControl("getwId"), TextBox).Text 'myCommand.Parameters.AddWithValue("@wYTURL", SqlDbType.NVarChar).Value = ytFormattedUrl cmd.Parameters.AddWithValue("@Id", User.Identity.GetUserId) cmd.Parameters.AddWithValue("@UserName", User.Identity.GetUserName) 'myCommand.Parameters.AddWithValue("@UserName", User.ToString) conn.Close() Dim eMessage As Label = DirectCast(GridView1.Rows(0).FindControl("eMessage"), Label) eMessage.Text = "Complete Success!" rComment.Text = String.Empty Catch exe As Exception Dim eMessage As Label = DirectCast(GridView1.Rows(0).FindControl("eMessage"), Label) eMessage.Text = exe.Message End Try End If End Sub
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim wId As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString() Dim gvOrders As GridView = TryCast(e.Row.FindControl("gvOrders"), GridView) gvOrders.DataSource = GetDataSrc(query:=$"select top 7 * from Replies WHERE wId='{wId }'") gvOrders.DataBind() End If End Sub Private Shared Function GetDataSrc(query As String) As DataTable Dim strConnString As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString Using con As New SqlConnection(strConnString) Using cmd As New SqlCommand() cmd.CommandText = query Using sda As New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using ds As New DataSet() Dim dt As New DataTable() sda.Fill(dt) Return dt End Using End Using End Using End Using End Function
Wednesday, February 21, 2018 4:00 PM
All replies
-
User-707554951 posted
Hi dvdgzzrll:
From your description, you have nested gridview.
However, I didn’t find parent gridview in your code.
From your code, you use OnRowDataBound of gridview () to query data from database,
However, the important thing you should be clear is that The OnRowDataBound event is only called if the DataBind method for the GridView has been called.
So, you could check whether this event's code been called or not by setting break point to debug.
If not, you could try call DataBind of parent gridview to call OnRowDataBound.
Besides, you also check whether the record be inserted into database or not by check your database table after clicking button
If the record has been inserted into database, the main problem could be included how to display bind record to nested gridview.
Best regards
Cathy
Thursday, February 22, 2018 9:01 AM -
User-1402564948 posted
HI Cathy Zou,
Thank you soo much for your reply. With a little help, adding the Nonquery was helpful and got me inserting records. However (you are correct by the way, I failed to share the parent gridview), my issue now is I can only insert on the latest record. I'm not fully understanding what is needed, but I'm guessing it's a matching ID on both the parent table and the nested table. I also need to know the selected index? ... so that the insert can be on the corresponding record. So, can you help with that at all? My code tries to get the wId from a hidden textbox haha.
Protected Sub PostReply_Click(sender As Object, e As EventArgs)
Dim rComment As TextBox = DirectCast(GridView1.Rows(0).FindControl("rComment"), TextBox)If rComment.Text IsNot Nothing Then
Try
Dim gvOrders As GridView = DirectCast(GridView1.Rows(0).FindControl("gvOrders"), GridView)
Dim strConn As [String] = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
Dim conn As New SqlConnection(strConn)
Dim strQ As String =
"INSERT INTO Replies(Id, UserName, Reply, wId) Values (@Id, @UserName, @Reply, @wId)"
conn.Open()
Dim cmd As New SqlCommand(strQ, conn)
Dim UserName As String = HttpContext.Current.User.Identity.GetUserName
cmd.Parameters.AddWithValue("@Reply", value:=SqlDbType.NVarChar).Value = rComment.Text
cmd.Parameters.AddWithValue("@wId", value:=SqlDbType.Int).Value = DirectCast(GridView1.Rows(0).FindControl("getwId"), TextBox).Text
cmd.Parameters.AddWithValue("@Id", User.Identity.GetUserId)
cmd.Parameters.AddWithValue("@UserName", User.Identity.GetUserName)
cmd.ExecuteNonQuery()
conn.Close()
Dim eMessage As Label = DirectCast(GridView1.Rows(0).FindControl("eMessage"), Label)
eMessage.Text = "Complete Success!"
rComment.Text = String.Empty
Catch exe As Exception
Dim eMessage As Label = DirectCast(GridView1.Rows(0).FindControl("eMessage"), Label)
eMessage.Text = exe.Message
End Try
End If
End Sub
Saturday, February 24, 2018 3:36 AM -
User-707554951 posted
Hi dvdgzzrll
Sorry for my poor understanding,
I still can’t understand your problem, may be you could provide more description and code
and description about what you want to do
Best regards
Cathy
Tuesday, February 27, 2018 7:32 AM