Create SQL statement on another page based on LinkButton

165 views Asked by At

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
2

There are 2 answers

0
narue1992 On BEST ANSWER

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

1
user3841709 On

I am a bit rusty with my vb.net GridView as I have been using DataGridViews with WinForms but they are basically the same. Something like this should get you in the right direction. This should go in your template button click event

 dim ID as string =  DataGridView1.Rows(e.RowIndex).Cells("ID").Value

 dim Date as string = DataGridView1.Rows(e.RowIndex).Cells("Date").Value

Continue To do this for all of the columns that you need data from, Then you can pass your ID, Date variables to the next page using session variables