Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete Query in asp.net using C#
    primarykey
    data
    text
    <p>In my application am allowing the user to delete an entire row from the grid view when he clicks on delete button. but am getting an error in delete query. Error is "Invalid column name" my code is as follows:</p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" DataKeyNames="Username" AutoGenerateColumns="false" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" &gt; &lt;Columns&gt; &lt;asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="true" /&gt; &lt;asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true" /&gt; &lt;asp:BoundField DataField="Password" HeaderText="Password" ReadOnly="true" /&gt; &lt;asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="true" /&gt; &lt;asp:CommandField ShowDeleteButton="true" /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre> <p>C# code is:</p> <pre><code>SqlConnection conn = new SqlConnection(@"Data Source=CHINNU-PC\SQLEXPRESS; Initial Catalog= CarDetails; Integrated Security=True"); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { gvbind(); } } protected void gvbind() { conn.Open(); SqlCommand cmd = new SqlCommand("Select * From [User]", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); conn.Close(); if (ds.Tables[0].Rows.Count &gt; 0) { GridView1.DataSource = ds; GridView1.DataBind(); } } public void Delete(string UserName) { string sql = "Delete From [User] Where UserName=" + UserName; conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); conn.Dispose(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { Delete(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); gvbind(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload