asp:Button
should be cast using following line:
Dim Btn As ImageButton = CType(sender, Button)
As Button
not as ImageButton
.
Secondly, you will get user name from Btn.CommandArgument
but concatenating CommandArgument
with SQL query is not a very nice idea. If not stored procedures or some ORM framework at least try using query with parameters. Use the following to connect to database and execute query:
Using con As SqlConnection = New SqlConnection(<CONNECTION_STRING>)
con.Open()
Dim sql As String = "Delete FROM Intranet.dbo.Gn_ISCoordinators where" & _
" cUserName= @UserName"
Dim cmd As SqlCommand = New SqlCommand(sql, con)
cmd.Parameters.Add(New SqlParameter("@UserName", Btn.CommandArgument))
cmd.ExecuteNonQuery()
con.Close()
End Using
PS:- My VB.Net is at best rustic. Also please take care that you might get exception when executing code against database. So please use exception handling etc.