I am Ultrawebgrid control which is associated with a SQL datasource for displaying and inserting data. Currently the newly the newly added rows to the webgrid are saved automatically in the database on postback(i.e of click of a button). How I disable this automatically saving of data.
I need to have more control over the timing database updation.
Thanks,Rajiv
Hello,
In a typical asp:sqldatasource control you can autogenerate the SELECT, UPDATE, DELETE and INSERT statements for the respective table using the asp:sqldatasource wizard. In this case I believe the easiest way to proceed would be to just delete the auto-generate INSERT statement and proceed with your custom code in the AddRow event of the grid.
Alternatively, you can just hook the AddRow event and set the eventArgs.Cancel property to true so that the event is cancelled and then add your custom code, e.g.
protected void UltraWebGrid1_AddRow(object sender, Infragistics.WebUI.UltraWebGrid.RowEventArgs e) { e.Cancel = true; // custom code here }
Also, I recommend reading this excellent blog post by Tony Lombardo on databinding and automatic updates here:
http://blogs.infragistics.com/blogs/tony_lombardo/archive/2008/01/29/demystifying-infragistics-webgrid-databinding.aspx
It provides many clues and insights on how databinding / autoupdates works and how they can be customized.
HTH,
Thank you for the Reply.
I am using the the Stored procedures for selecting and inserting data in the SQLDataSource. I tried to use the UltraWebGrid1_AddRowBatch event to cancel the insert operation using e.Cancel = true;. But for some reason the insert opertaion is not cancelled and the data gets inserted in the database.
Could you please let me know why this is happening and how to fix this?
Thanks & Regards,
Rajiv
Hello Rajiv,
If you are using declarative SqlDataSource, can't you just delete the InsertCommand that is probably autogenerated by the SqlDataSource wizard (the one in bold in the example below)
<asp:SqlDataSource ID="SqlDataSource" runat="server" DataFile="~/App_Data/Nwind.mdb" DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = ?" InsertCommand="INSERT INTO [Employees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country], [HomePhone], [Extension], [Photo], [Notes], [ReportsTo]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" SelectCommand="SELECT * FROM [Employees]" UpdateCommand="UPDATE [Employees] SET [LastName] = ?, [FirstName] = ?, [Title] = ?, [TitleOfCourtesy] = ?, [BirthDate] = ?, [HireDate] = ?, [Address] = ?, [City] = ?, [Region] = ?, [PostalCode] = ?, [Country] = ?, [HomePhone] = ?, [Extension] = ?, [Photo] = ?, [Notes] = ?, [ReportsTo] = ? WHERE [EmployeeID] = ?">
Also, are you sure that you need AddRowBatch event (works only if you add multiple rows on the client at once and then postback at server)? Can you verify this event fires by placing a breakpoint on it? Can you check the same e.Cancel logic with AddRow event instead?
Hello Rumen,
My Data source looks something like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
InsertCommandType="StoredProcedure" InsertCommand="InsertTimeSheet">
<InsertParameters>
<asp:Parameter Name="LaborID" Type="Int16" />
<asp:Parameter Name="Weekend" Type="DateTime" />
<asp:Parameter Name="STHrs" Type="Decimal" />
<asp:Parameter Name="OTHrs" Type="Decimal" />
<asp:Parameter Name="PRHrs" Type="Decimal" />
</InsertParameters>
I can't really remove the InsertCommand as I want the data to beinserted in the database but I don't want the Insert to happen automatically.
I need to use the AddRowBatch event as I have to insert multiple items together in one go on the postback.
I debugged and found that the AddRowBatch event is triggered but the data is still inserted in the database. I checked the AddRow event but even that doesn't seems to work for me.
That is weird, why cannot you remove the InsertCommand from the SqlDataSource if you are manually calling the stored procedure? In any case, then I believe you can cancel the event directly from the datasource itself, rather from the grid, in the SqlDataSource.Inserting event, e.g.
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e) { e.Cancel = true; }