.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

gridview.rowcommand + sqldatasource.delete()

Posted By:      Posted Date: October 04, 2010    Points: 0   Category :ASP.Net

I have gridview+sqldatasource. In gridview i have button DELETE. But i want on click this button for some rows execute DELETE command other rows UPDATE. I did:

protected void GridMain_RowDataBound(object sender, GridViewRowEventArgs e)
  if (e.Row.RowType == DataControlRowType.DataRow)
     if (DataBinder.Eval(e.Row.DataItem, "DelFlag").ToString() == "True")
               btnDelete.CommandName = "Delete";
               btnDelete.CommandName = "DeleteVirtual";

protected void GridMain_RowCommand(object sender, GridViewCommandEventArgs e)
        if (e.CommandName == "Delete")
           sourceMain.DeleteCommand = "DELETE FROM [Main] WHERE 
        else if (e.CommandName == "DeleteVirtual")
            sourceMain.DeleteCommand = "UPDATE [Main] SET DelFlag = 'True' WHERE  
@index - DeleteParametrs in sqldatasource. But DELETE works and DELETEVIRTUAL doesn't. Then i insert sourceMain.Delete() in rowcommand after sourceMain.DeleteCommand ... But it's doesn't help m

View Complete Post

More Related Resource Links

Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality

In my previous examples, I have demonstrated on how to add dynamic rows in GridView control with TextBoxes and how to save the values into the database. Now, seems that most of the developers are asking if how to add a delete functionality with it. So in this example, I'm going to show on how to delete a certain row in the dynamic GridView with TextBoxes.

Binding GridView with SqlDataSource at runtime?


Hi, I want to create a generic page which contains a gridview and sqldatasource. I want to put these two controls on .aspx (C#) page but bind them at runtime. It should have selectcommand, updatecommand, deletecommand and insertcommand. Stored procedures are created for that. Help is needed to make it. Please post some link or code. Regards, ap.

Add, Edit, update, Delete gridview

The article Add, Edit, update, Delete gridview was added by anjankant on Friday, July 09, 2010.

Thousandas operation we can do on Gridview control. Here I as explaining how we can add new recored and edit of existing record and delete a record in Gridview. Step1 : You can add this gridview in your aspx form. Here some events of Gridview are fired

Gridview Update outside of SqlDatasource


Is it possible to use a SQLDataSource to populate a gridview but not use the SQLDataSource for Updating?

I wanted to add the update code in the Gridview_RowUpdating event since it is a complex update I need to do.

I get this message when i click the update button

Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand is specified.

Edin, New and Delete buttons in gridview?


Hi guys. I have gried view that shows me all info from the table i made. So my SelectCommand works fine. I'v add 3 additional buttons "new", "edit" and "delete" i'v made their query but still have mistake by clicking it in browser. I follow from the book Begginer to Novice 3.5 ASP C# but that's what it's shown. I know something missing. I'v check in the net but not found how to fixed, because i use SqlDataSource. Can you advise how to proceed from here? Here's what i did:

<asp:SqlDataSource ID="SqlDataSource5" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:ThisIsMyConnection%>"


              SelectCommand = "SELECT * FROM [Resident] WHERE (([FNAME] + ' ' + [LNAME] LIKE '%' + @FANDLNAME + '%'))"


              UpdateCommand = "UPDATE Resident SET FNAME=@FNAME, LNAME=@LNAME, MobileNum=@MobileNum, HomeNum=@HomeNum, OfficeNum=@OfficeNum, FaxNum=@FaxNum WHERE FNAME=@original_FNAME AND LNA

Delete from gridview

I have read about 20 articles and I can't figure out what I have done wrong.  I am trying to delete from a gridview.  Below is the code.  Any help is appreciated. <form runat='server'> <asp:SqlDataSource  ID='dsTest'  Runat='server'  ConnectionString="<%$ ConnectionStrings:myCnn %>"  SelectCommand="SELECT [ID], [Name], [Enabled], [FavoriteColor] FROM tblTest"  UpdateCommand="UPDATE tblTest SET [Name] = @Name, [Enabled] = @Enabled, [FavoriteColor] = @FavoriteColor WHERE ID = @ID"  DeleteCommand="DELETE FROM tblTest WHERE ID = @ID">  <DeleteParameters>   <asp:Parameter    name="ID" />  </DeleteParameters> </asp:SqlDataSource> <asp:GridView  ID="gvTest"  Runat='server'  DataKeyNames='ID'  AutoGenerateColumns='true'  AutoGenerateEditButton='true'  AutoGenerateDeleteButton='true'  DataSourceID="dsTest" /></form>Exception Details: System.Data.SqlClient.SqlException: Must declare the variable '@ID'.

Return value from SqlDataSource in GridView ?

Hi, everyone. I have textbox for searching a Names in My database. I return the information from the Select Statement in  GridView, but i want to know the number of affecter rows. How to do that?

Problem with "Edit" and "Delete" buttons in gridview!

Hi everyone. I have problem with the buttons Edit and Delete in Gridview for very long time and i really need help about it. So i have gridview and sqldatasource. First i try to make this buttons manually and as you will see in sqldatasource 5 and 6 i have these buttons my records show up but the buttons not working. After that i was advised to do it automatically with gridview smart tag options and Generate columns - this is what i did in sqldatasource7 and gridview7. But both ways i need to change the statements with my own because that what they create not works. So select statement works. Now the error that i get in both ways is the same:The GridView 'GridView(the number could be 5,6 or 7)' fired event RowEditing which wasn't handled.See how looks my page:Now here's my code in aspx:<%@ Page Language="C#" Title="Specific Search in Phone Directory" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="serv

Can not set delete up at GridView

Hello:I am trying to add delete, update function to the gridview.After set up sqldatasource, configure the select statement, click next, it showed: test query, then finish.I did not get Advanced SQL generation Optionson the Configure the select statement, there is Advanced button at the right side. However, I clicked, it showed: Advanced SQL Generation Options, but it is disabled.How to enable Advanced SQL Generation Options so that I could set up delete, update etc functions.I am usingMicrosoft visual studio 2005Versioin 8.0.50727.762 (SP.050727-7600)Your help is highly appreciated.

RowCommand not firing inside a GridView

Hello,  I'm trying to fire the RowCommand event on a DataGrid control.  I've previously done that, but not in this precise case.  Actually I'm working on someone else code so I can't really change the structure of his page excepted if it's really necessary. The code of the page is heavy, so I've summarized the problem in a small project and the page behaves the same way too.   Basically I have a GridView which I bind to a DataSet.  According to those data, I sometimes need to add, at the end of a row, a "Details" button (a LinkButton).  By pushing this button, I want to show a second GridView containing those details.  Let's add to this that the columns number of the first GridView is variable, those LinkButton need to be added dynamically.   The first GridView is generated without any problem and when my mouse is over one of the LinkButton, I can see that the link is something like "javascript:__doPostBack('dataGridView$ctl02$lnkDetail','')".  When I click on this link and debug step by step : Page_Init, Page_Load, OnPrerender, RowDataBound but no RowCommand, and I don't know why!   In the exemple, I've explicitly set the EnableViewState to true from what I've read here and there, but nothing's changed.   Here's my summarized problem and

Gridview RowCommand Event returning null values

HiI'm trying to retreive values entered by users in the textboxes in the footer of the gridview and insert these values into another sql table.My code is as follows :-protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)    {       if(e.CommandName.Equals("addnew"))       {                       TextBox t1 = (TextBox)GridView2.FooterRow.FindControl("tfrm");            string frm = t1.Text;            TextBox t2 = (TextBox)GridView2.FooterRow.FindControl("tsub");            string sub = t2.Text;            string cmd = "insert into suggestions values('" + frm + "','" + sub + "','c','c')";            con.Open();            SqlCommand com = new SqlCommand(cmd,con);            int a = com.ExecuteNonQuery();&n

Gridview and delete option - Button click displays message

Hi, I have a gridview connected to a sql database that allows the user to delete the record. I used the ASP.net automatic "options" for this. However, I would like when the user deletes a record, a message (textbox) appears in the form to say the record has been deleted. Please can anyone provide some sample codes?   Thanks in advance. Nijojo.

Gridview Checkbox Delete

this is a duplicate of my other post. The reason for this is I can't view my first topic about this. I tried in my laptop, pc, and also my friend's pc but I can't view the topic. It slows down the browser and will hang.So here it is.My gridview is being generated at runtime by a dataset. My first column is a template field with checkbox, how to delete a selected row? C# please.This is my code but it doesn't work.protected void DeleteSelected_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { CheckBox cb = (CheckBox)row.FindControl("RegistrySelector"); { if(cb.Checked) { string id = GridView1.DataKeys[row.RowIndex].Value.ToString(); string del = "delete from EnrollTbl where Register_ID="+id; da = new SqlDataAdapter(del,con); ds = new DataSet(); ds.Clear(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } } }

You have specified that your delete command compares all values on SqlDataSource 'SqlDataSource1',

hi.am getting the following error .... i have a grid view with a command field ADD(i e INSERT)..... EDIT and DELETE.....edit button works fine but when i click on DELETE its throwing the following errorYou have specified that your delete command compares all values on SqlDataSource 'SqlDataSource1', but the dictionary passed in for values is empty.  Pass in a valid dictionary for delete or change your mode to OverwriteChanges.so can any one help me in this regard...

The delete and edit buttons not working in gridview!

Hi everyone. I have problem with the buttons Edit and Delete in Gridview for very long time and i really need help about it.  So the when i click edit i get in edit mode and when click update the values in the row i was changing disappear but only in the gridview. When check in the Database nothing happens. The delete button not works at all, i mean when click on it only refresh the table. If see something wrong in my code please help. See how looks my page:Now here's my code in aspx:<%@ Page Language="C#" Title="Specific Search in Phone Directory" ... ... ... <asp:SqlDataSource ID="SqlDataSource7" runat="server" ConnectionString="<%$ ConnectionStrings:ThisIsMyConnection2 %>" OldValuesParameterFormatString="original_{0}" DeleteCommand="DELETE FROM [Resident] WHERE [MobileNum] = @MobileNum" InsertCommand="INSERT INTO [Resident] ([FNAME], [LNAME], [MobileNum], [HomeNum], [OfficeNum], [FaxNum]) VALUES (@FNAME, @LNAME, @MobileNum, @HomeNum, @OfficeNum, @FaxNum)" SelectCommand = "SELECT * FROM [Resident] WHERE (([FNAME] + ' ' + [LNAME] LIKE '%' + @FANDLNAME + '%'))" UpdateCommand=

Textbox -> value -> gridview -> Sqldatasource?

Hi guys. I have textbox that searches for first or last name. Result show in gridview. But when add value in text box and push my button Find nothing happen. Here code: aspx:   ........ <asp:GridView runat="server" AllowPaging="True"                   AutoGenerateColumns="False" DataKeyNames="MobileNum"                   DataSourceID="SqlDataSource5" style="font-family: Verdana"                          onselectedindexchanged="GridView1_SelectedIndexChanged">         <Columns>                         <asp:BoundField DataField="FNAME" HeaderText="FNAME" SortExpression="FNAME" />             <asp:BoundField DataField="LNAME" HeaderText="LNAME" SortExpression="LNAME" />          &nb

Dynamic templatefield disappear during gridview rowcommand

Hi,   I already implemented the dynamic templatefield which will re-created in every postback. Everytime I make a postback in the page I see no problem but when I click my imagebutton inside the gridview then the dynamic templatefield disappear. To double check if it is really gone at all I just select the second page of the gridview using my customize paging using dropdownlist outside the gridview and the templatefield with its correct data reappear again. I already put a variable inside the rowcommand to see if the number of columns are the same with the number of columns the gridview render together with the dynamic templatefield during debugging and it is there but cannot render that templatefield in the client.   Any ideas? 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend