Make If Statments Using Gridview LinkButtons

64 views Asked by At

I have 2 Linkbuttons inside each row of my gridview. I want to know how I can use If statements to determine which changes should be made.

My current If statements(which I know are wrong) are as follows:

If LinkButton1.Text = "Update" Then

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

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

        Page.Session.Add("Admin_Updates", strSelect)
        Response.Redirect("DispAd.aspx")

ElseIf LinkButton2.Text = "Delete" Then

    Dim ClassifiedStr As New OleDbCommand

        ClassifiedStr.CommandType = CommandType.StoredProcedure
        ClassifiedStr.CommandText = "delete_classifieds"
        ClassifiedStr.Connection = conn

        'Must be organized based on Stored Procedure
        ClassifiedStr.Parameters.Add("val_id", OleDbType.Date).Value = strFilter
        conn.Open()

        ClassifiedStr.ExecuteNonQuery()
        conn.Close()
        Response.AddHeader("Refresh", "1")

End if

What do I use in place of my lines If LinkButton1.Text = "Update"

enter image description here

Update:

I added CommandName="UpdateRow" and "DeleteRow" to HTML Linkbutton and did the following:

If LinkButton1.CommandName = "UpdateRow" 

and

ElseIf LinkButton2.CommandName = "DeleteRow" Then

However, the Delete one simply Deletes the LinkButton and not the Database record which is weird?! Not sure why.

I also see that the Display button will only work once I click Delete, change page, go back to first page which has Delete Removed. So if Delete is present Display doesn't work.

UPDATED FULL VERSION THAT DOESN'T WORK VERSION 1

Protected Sub DisplayClassifieds_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DisplayClassifieds.SelectedIndexChanged

    Dim conn As OleDbConnection = New OleDbConnection("Provider=""********"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

    Dim strSelect As String
    Dim strFilter As String = " "
    ' Dim counter As Integer = 0
    ' Dim v As Integer = 0
    'cell = DisplayClassifieds[0,Row].Value


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

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

    If LinkButton1.commandName = "UpdateRow" Then
        Page.Session.Add("Admin_Updates", strSelect)
        Response.Redirect("DispAd.aspx")

    ElseIf LinkButton2.commandName = "DeleteRow" Then

        Dim ClassifiedStr As New OleDbCommand

        ClassifiedStr.CommandText = "DELETE * FROM TABLENAME WHERE Classid = '" & strFilter & "'"
        ClassifiedStr.Connection = conn

        'Must be organized based on Stored Procedure
        ClassifiedStr.Parameters.Add("val_id", OleDbType.Date).Value = strFilter
        conn.Open()

        ClassifiedStr.ExecuteNonQuery()
        conn.Close()
        Response.AddHeader("Refresh", "1")
        Response.Redirect("QRY2.aspx")
    End If


End Sub

VERSION 2

Sub LinkButton1_Click(sender As Object, e As EventArgs)
    Dim conn As OleDbConnection = New OleDbConnection("Provider=""********"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

    Dim strSelect As String
    Dim strFilter As String = " "
    Dim counter As Integer = 0
    Dim v As Integer = 0
    'cell = DisplayClassifieds[0,Row].Value


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

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

    Page.Session.Add("Update_Values", strSelect)
    Response.Redirect("DispAdUpdate.aspx")
End Sub
Sub LinkButton2_Click(sender As Object, e As EventArgs)

    Dim conn As OleDbConnection = New OleDbConnection("Provider=""*******"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)

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

    Dim ClassifiedStr As New OleDbCommand

    ClassifiedStr.CommandType = CommandType.StoredProcedure
    ClassifiedStr.CommandText = "delete_classifieds"
    ClassifiedStr.Connection = conn

    'Must be organized based on Stored Procedure
    ClassifiedStr.Parameters.Add("val_id", OleDbType.Date).Value = strFilter
    conn.Open()

    ClassifiedStr.ExecuteNonQuery()
    conn.Close()
    Response.AddHeader("Refresh", "1")
    Response.Redirect("QRY2.aspx")
End Sub
3

There are 3 answers

0
narue1992 On BEST ANSWER

It looks like doing this process is very hard. I decided to do a "select" option instead since my question seemed difficult.

enter image description here

I do this like so:

For the select option row:

Protected Sub DisplayClassifieds_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DisplayClassifieds.SelectedIndexChanged

    Dim row As GridViewRow = DisplayClassifieds.SelectedRow

End Sub

Then making a delete and update button that takes that index as so....

 Protected Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
    Dim conn As OleDbConnection = New OleDbConnection("Provider=""******"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd)


    If Page.IsValid Then
        If DisplayClassifieds.SelectedIndex = -1 Then
            Response.Write("<script language=""javascript"">alert('You must select a record.');</script>")
            Exit Sub
        End If


        Dim ClassifiedStr As New OleDbCommand
        ClassifiedStr.CommandType = CommandType.StoredProcedure
        ClassifiedStr.CommandText = "delete_classifieds"
        ClassifiedStr.Connection = conn

        'Must be organized based on Stored Procedure
        'DataKey is the DataKey that we labeled as Classid(same name as ID field in Oracle)
        ClassifiedStr.Parameters.Add("val_id", OleDbType.Numeric).Value = CInt(DisplayClassifieds.SelectedDataKey.Value)
        conn.Open()

        ClassifiedStr.ExecuteNonQuery()

....etc

The bottom "DataKey" code from my VB.net comes from the table options I made with the use of "DataKeyNames" value :

<asp:GridView ID="DisplayClassifieds" runat="server" align="center" 
                Width="100%" AllowSorting="True" AutoGenerateColumns="False" 
                AutoGenerateSelectButton="True" EnableModelValidation="True" 
                BorderColor="Black" BorderStyle="Solid" DataKeyNames="Classid" >
    <Columns>

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

        ....etc
    </Columns>

</asp:GridView>
7
user3841709 On

I'm not as familiar with using/calling stored procedures but, if it's not too much of a hassle, try typing your delete query out in the commandtext property, ex.

ElseIf LinkButton2.Text = "Delete" Then

Dim ClassifiedStr As New OleDbCommand


     ClassifiedStr.CommandText = "DELETE * FROM TABLENAME WHERE val_id = @val_id"
    ClassifiedStr.Connection = conn

    'Must be organized based on Stored Procedure
    ClassifiedStr.Parameters.AddWithValue("@val_id", strFilter)
    conn.Open()

    ClassifiedStr.ExecuteNonQuery()
    conn.Close()
    Response.AddHeader("Refresh", "1")

End if

Since I haven't ever called stored procedures I am just guessing that it has something to do with the way you are calling it for delete

4
Nitesh Kumar On

You should put strFilter = row.Cells(1).Text line above if statement (If LinkButton1.Text = "Update" Then).