Search code examples
sqlasp.netvb.netgridviewasplinkbutton

Create SQL statement on another page based on LinkButton


I have a simple table as shown below:

enter image description here

When user clicks the Template Field Link button, it should send to another page and fill in the information into the following text boxes:

enter image description here

I want to be able to populate an SQL statement that uses the table's information to populate the information from database:

Below is my gridview:

<Columns>

   asp:TemplateField HeaderText="Action" SortExpression="Date">
        <ItemTemplate>
          <asp:LinkButton ID="LinkButton1" runat="server" CommandName="view" >Display</asp:LinkButton> 
        </ItemTemplate>
           <ControlStyle Width="45px" />
           <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
        </asp:TemplateField>

    <asp:BoundField DataField="Classid" HeaderText="ID" 
                            SortExpression="Date" > 
        <ItemStyle cssClass="grid_padding" />
    </asp:BoundField>

    <asp:BoundField DataField="Addate" HeaderText="Date" SortExpression="Date" 
                            DataFormatString="{0:d}" >
        <ItemStyle cssClass="grid_padding" />
    </asp:BoundField>

    <asp:BoundField DataField="username" HeaderText="User Name" 
                            SortExpression="Date" > 
        <ItemStyle cssClass="grid_padding" />
    </asp:BoundField>

    <asp:BoundField DataField="category" HeaderText="Category" SortExpression="Date"> 
        <ItemStyle cssClass="grid_padding" />
    </asp:BoundField>

    <asp:BoundField DataField="description" HeaderText="Description" 
                            SortExpression="Date" >                
         <ItemStyle CssClass="grid_padding2" />
    </asp:BoundField>

</Columns>

This is where I am at so far for the Link button (UPDATE: all commented out lines give me errors so they don't work):

Protected Sub DisplayClassifieds_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DisplayClassifieds.SelectedIndexChanged
    Dim strSelect As String
    Dim strFilter As String = " "
    Dim counter As Integer = 0
    Dim v As Integer = 0
    'cell = DisplayClassifieds[0,Row].Value

    'cell = DisplayClassifieds.Rows(e.NewSelectedIndex).Cells(0).Text
    'strFilter = DisplayClassifieds.SelectedRowStyle(0).Value

    strSelect = "SELECT Classid, Addate, Username, Category, Description, Fulldescription FROM TABLENAME WHERE Classid = '" & strFilter & "' "


    Page.Session.Add("Display_Values", strSelect)
    Response.Redirect("DispAd.aspx")
End Sub

Solution

  • Took so many attempts but having SelectedRow.Cells(1).Text... or .Value gave me issues so I split up my command:

    Dim row As GridViewRow = DisplayClassifieds.SelectedRow
    strFilter = row.Cells(1).Text
    

    With this I got my "ID" value which is enough to use for my SQL