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"
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
It looks like doing this process is very hard. I decided to do a "select" option instead since my question seemed difficult.
I do this like so:
For the select option row:
Then making a delete and update button that takes that index as so....
The bottom "DataKey" code from my VB.net comes from the table options I made with the use of "DataKeyNames" value :