Hello forum,
I am having issue with my delete code. I have a gridview where records are displayed. The data that are bind in the gridview are from different tables; when user selects a value from the drop-down List, data will be displayed in the gridview based on the selected value in the drop-down List.
In the gridview, delete button is shown based on the drop-down List too. For example if a user selects certificate in the dropdown, then the delete button will be hidden, but when cards is selected, then the delete button shows.
When I click on the delete button is gives me this error as shown below

Here is my HTML and code to delete
<asp:DropDownList runat="server" ID="ddlTables" Width="100%" Height="20px" AutoPostBack="true">
<asp:ListItem Text="Select Document Type" />
<asp:ListItem Text="Users" Value="Team" />
<asp:ListItem Text="ID Cards" Value="Cards" />
<asp:ListItem Text="Certificates" Value="Certificates" />
<asp:ListItem Text="Reciepts" Value="reciepts" />
<asp:ListItem Text="Tickets" Value="Tickets" />
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Font-Bold="true" Text="Start Date"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="100%" Height="20px" type="date"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Font-Bold="true" Text="End Date"></asp:Label>
<asp:TextBox ID="Dat" runat="server" Width="100%" Height="20px" type="date"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Search Records/Activities" Font-Size="Small" Width="100%" BackColor="SteelBlue" Class="btn btn-primary" OnClick="Button2_Click"/>
<br />
<br />
<div class="parent">
<div class="child" id="midcont" style="width: 100%; font-family: Constantia;">
<div class="Gridv" style="height: 380px; overflow: scroll">
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="OnRowDataBound" GridLines="None" HeaderStyle-BackColor="#D3D9E5" OnRowDeleting="OnRowDeleting"
HeaderStyle-ForeColor="#00003D" Width="100%" Height="80px" HeaderStyle-Height="30px" AllowPaging="true" PageSize="6">
<EmptyDataTemplate>
<div style="text-align: center; font-weight: bolder; font-size: medium;">
<asp:Label ID="labelTemp" runat="server" Text="No Record/Activity"></asp:Label>
</div>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Width="40px" Height="40px" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowDeleteButton="true" ItemStyle-ForeColor="#00003D" ItemStyle-Width="150" ControlStyle-Font-Size="Small"
ControlStyle-BackColor="SteelBlue" ControlStyle-BorderStyle="None" ControlStyle-ForeColor="White" ControlStyle-Width="40" HeaderText="Action" />
</Columns>
</asp:GridView>
</div>
</div>
</div>
CODE
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int Uid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string query = "";
if (ddlTables.SelectedValue.ToLower() == "team")
{
query = "DELETE FROM Users WHERE Uid = Uid";
}
if (ddlTables.SelectedValue.ToLower() == "cards")
{
query = "DELETE FROM CardTbl WHERE Uid = @Uid";
}
if (ddlTables.SelectedValue.ToLower() == "certificates")
{
query = "DELETE FROM CertTbl WHERE Uid = @Uid";
}
using (SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename =| DataDirectory |\\Dataregister.mdf; Integrated Security = True"))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Uid", Uid);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}