2

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
narue1992
  • 1,143
  • 1
  • 14
  • 40

3 Answers3

1

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

Nitesh Kumar
  • 1,774
  • 4
  • 19
  • 26
1

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>
narue1992
  • 1,143
  • 1
  • 14
  • 40
0

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

user3841709
  • 386
  • 6
  • 28
  • My Stored Procedure is the same as your sql statement. Also your version does the same thing. I click it... go back a web page and go back to the main page and `Delete` linkbutton is missing. Doesn't delete the actual record. – narue1992 Jun 24 '15 at 15:37
  • Can you set a breakpoint on Your parameters.add line for the delete. I'm wondering if that is actually getting a correct value for the parameter? – user3841709 Jun 24 '15 at 15:42
  • I edited my answer above, try that and see if it actually deletes the record – user3841709 Jun 24 '15 at 15:44
  • Still deletes the linkbutton. I've tried making a `Sub LinkButton1_Click(sender As Object, e As EventArgs)` for each individual button when clicked but same issues still happens. – narue1992 Jun 24 '15 at 16:02
  • but do you know for sure if you are actually getting the "val_id" value for your parameters? I guess this would probably be throwing an error for you if it wasn't getting a value – user3841709 Jun 24 '15 at 16:09
  • val_id is from my Stored Procedure so yes – narue1992 Jun 24 '15 at 16:10
  • If the row is never actually getting deleted though, that means there is still something wrong with your delete command whether the button is disappearing or whatever, If you can get the actual record to delete then you can figure out the button issue. I don't mean to keep on about it... I just think something is wrong with the query since you say it doesn't actually delete the record. – user3841709 Jun 24 '15 at 16:35