1540

delete row from selected gridview and database

Question:

i am trying to delete a row from the gridview and database... It should be deleted if a delte linkbutton is clicked in the gridview.. I am gettin the row index as follows:

protected void LinkButton1_Click(object sender, EventArgs e) { LinkButton btn = (LinkButton)sender; GridViewRow row = (GridViewRow)btn.NamingContainer; if (row != null) { LinkButton LinkButton1 = (LinkButton)sender; // Get reference to the row that hold the button GridViewRow gvr = (GridViewRow)LinkButton1.NamingContainer; // Get row index from the row int rowIndex = gvr.RowIndex; string str = rowIndex.ToString(); //string str = GridView1.DataKeys[row.RowIndex].Value.ToString(); RemoveData(str); //call the delete method } }

now i want to delete it... so i am having problems with this code.. i get an error

Must declare the scalar variable "@original_MachineGroupName"... any suggestions

private void RemoveData(string item) { SqlConnection conn = new SqlConnection(@"Data Source=JAGMIT-PC\SQLEXPRESS; Initial Catalog=SumooHAgentDB;Integrated Security=True"); string sql = "DELETE FROM [MachineGroups] WHERE [MachineGroupID] = @original_MachineGroupID; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@original_MachineGroupID", item); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } <blockquote>

Blockquote

</blockquote> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SumooHAgentDBConnectionString %>" SelectCommand="SELECT MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted, COUNT(Machines.MachineName) AS Expr1, DATENAME(month, (MachineGroups.TimeAdded - 599266080000000000) / 864000000000) + SPACE(1) + DATENAME(d, (MachineGroups.TimeAdded - 599266080000000000) / 864000000000) + ', ' + DATENAME(year, (MachineGroups.TimeAdded - 599266080000000000) / 864000000000) AS Expr2 FROM MachineGroups FULL OUTER JOIN Machines ON Machines.MachineGroupID = MachineGroups.MachineGroupID GROUP BY MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted" DeleteCommand="DELETE FROM [MachineGroups] WHERE [MachineGroupID] =@original_MachineGroupID" > <DeleteParameters> <asp:Parameter Name="@original_MachineGroupID" Type="Int16" /> <asp:Parameter Name="@original_MachineGroupName" Type="String" /> <asp:Parameter Name="@original_MachineGroupDesc" Type="String" /> <asp:Parameter Name="@original_CanBeDeleted" Type="Boolean" /> <asp:Parameter Name="@original_TimeAdded" Type="Int64" /> </DeleteParameters> </asp:SqlDataSource>

I still get an error : Must declare the scalar variable "@original_MachineGroupID"

Answer1:

You're not adding an @original_MachineGroupName parameter to your query. Your query references multiple variables, and you've only added one of them.

cmd.Parameters.AddWithValue("@original_MachineGroupID", item); cmd.Parameters.AddWithValue("@original_MachineGroupName", itemName); cmd.Parameters.AddWithValue("@original_MachineGroupDesc", itemDesc); cmd.Parameters.AddWithValue("@original_CanBeDeleted", true); cmd.Parameters.AddWithValue("@original_TimeAdded", time);

You'll need to find the extra information to satisfy your query, or else change your query to remove the need for them.

Answer2:

You have several parameters in you query i.e. @original_MachineGroupName but are only adding one actual parameter to you command object. You will need one for each parameter in the query.

Answer3:

The following code will delete the row from the database if the string you are passing to method is primary or unique key in table:

string sql = "DELETE FROM [MachineGroups] WHERE [MachineGroupID] = @original_MachineGroupID"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@original_MachineGroupID", item);

Answer4:

Watch your databinding also. If you delete a row from the database, that doesn't delete the data from the gridview. You will need to re-fetch your data and databind it again.

Recommend

  • localhost cannot find stored procedure 'dbo.aspnet_CheckSchemaVersion'
  • My select command doesn't work
  • Python - Pyodbc Connection error
  • Servicestack ORMLite/Massive managing multiple DataTables with Expandos / Dynamic?
  • LightSwitch Beta 1 looking for SQL Express instead of full SQL instance
  • How can I fix CA2100 Review SQL queries for security vulnerabilities issue
  • Parse returned C# list in AJAX success function
  • Execute Success but num_rows return 0 [PHP-MySQL]
  • How to export MS Access table into a csv file in Python using e.g. pypyodbc
  • Background image does not show on iPhone/iPad
  • Displaying Data From Multiple MySQL Tables
  • Embedding a Google map
  • The symbol you provided is not a function
  • SqlCommand back up Database
  • Converting query results into DataFrame in python
  • blade.php method outputting it's result to the form
  • xcode don't localize specific strings
  • Counter field in MS Access, how to generate?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Javascript + PHP Encryption with pidCrypt
  • Websockets service method fails during R startup
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Acquiring multiple attributes from .xml file in c#
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Memory offsets in inline assembly
  • embed rChart in Markdown
  • How can I remove ASP.NET Designer.cs files?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?