.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

ObjectDataSource where SelectMethod returns DataTable has Delete problems

Posted By:      Posted Date: April 10, 2011    Points: 0   Category :ASP.Net

Hello experts,


I have an ObjectDataSource that is configured to use my data access layer class. The select method returns a DataTable. The DeleteMethod takes a System.Guid as a parameter. I am using a FormView object on my page that shows information from the Row of the table and allows paging. In the FormView, I also have a "delete" button with the CommandName attribute set to "Delete".

Without setting anything up, clicking the delete button would send an empty Guid to my delete method. So, I thought it was because my DataKeyNames property of the FormView was empty. So, I set the DataKeyNames property to "SystemId" which is the value from the DataRow of the current item that I would like to use as the primary key. This caused all sorts of problems basically saying that DataObjectTypeName of the the ObjectDataSource did not have a property called "SystemId". I tried adding a DeleteParameters section to the ObjectDataSource and specifying one but had no success.

Obviously, I'm missing a simple step here but I have not been able to figure it out. Below is the markup:


<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    DataObjectTypeName="System.Guid" DeleteMethod="DeletePicture" 
    onobjectcreating="ObjectDataSource1_ObjectCreating" SelectMethod="GetPictures" 

View Complete Post

More Related Resource Links

SqlCommand.ExecuteNonQuery() returns -1 when doing Insert / Update / Delete

Sometimes you end up with a return value of -1 when using the SqlClient.SqlCommand.ExecuteNonQuery method.

Why is that?

Well, the ExecuteNonQuery method is there for statements for changing data, ie. DELETE / UPDATE /INSERT, and the returned value are the number of rows affected by that statement.

When checking the documentation we can see that there are some conditions that return -1.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of

rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

ListView calls ObjectDataSource SelectMethod when setting InsertItemPosition


I have a ListView that is bound to an ObjectDataSource. I want to create a new insert item when the user clicks on a button on the form. I do this by reacting to the button click event on the postback. In the button click event, I set ListView.InsertItemPosition = InsertItemPosition.FirstItem.


        protected void NewButton_Click(object sender, EventArgs e)
            // This line causes the ListView's ObjectDataSource to call its SelectMethod
            // Removing this line, the SelectMethod is NOT called.
            itemsListView.InsertItemPosition = InsertItemPosition.FirstItem;


This works, but for some reason the ObjectDataSource's SelectMethod is executed again.

Is there a reason why the SelectMethod of the ObjectDataSource is being called when the ListView's InsertItemPosition is set from None to FirstItem?


delete a row in datatable using linq to dataset

hihow can i delete a row in datatable using linq to dataset?I have a dataset in my project, need to delete some rows filtered by IDhow can i do?

Crystal Report with DataSet, DataTable (Having Problems)

Hello All,     I am a bit new with Crystal Reporting and I was given the Job to take an exisitng report , modify it and load it on a website... the report part is done but the loading is been a pain. So here is what i did,    protected void Button1_Click(object sender, EventArgs e) { SqlConnection cn = new SqlConnection("server=ss;uid=sss;pwd=ss;database=sss"); cn.Open(); #region quer //Query Here, This is working fine #endregion ReplaceEscape(qry); SqlCommand cmd = new SqlCommand(qry, cn); cmd.CommandTimeout = 1000; cmd.ExecuteNonQuery(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet myDataSet = new DataSet(); da.Fill(myDataSet, "DSAllStatQuery"); ReportDocument cryRpt = new ReportDocument(); cryRpt.SetDataSource(myDataSet); cryRpt.Load(@"C:\Users\erodriguez\Documents\Visual Studio 2010\WebSites\AllStats\CrystalReport.rpt"); CrystalReportViewer1.ReportSource = cryRpt; cn.Close(); } public static string ReplaceEscape(string str) { str = str.Replace("'", "''"); return str; } I crea

Binding DataTable data to ObjectDataSource

Hi all, I am trying to bind a DataTable into a ObjectDataSource. This ObjectDataSource data will be used to bind into my LocalReport in ReportViewer. I have already successfully binded my data into the DataTable. My problem now is how do i bind the data in my DataTable into the ObjectDataSource so that it can be used in my report? Here is my current codes: ASPX:<rsweb:ReportViewer ID="ReportViewer1" runat="server"> <LocalReport ReportPath="Modules\Report.rdlc"> <DataSources> <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" /> </DataSources> </LocalReport> </rsweb:ReportViewer> <asp:ObjectDataSource runat="server" ID="ObjectDataSource1"> </asp:ObjectDataSource> VB:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Try Dim dtable As DataTable = SetReportDataSource() If dtable.Rows.Count > 0 Then Dim rds As New ReportDataSource() ReportViewer1.LocalReport.DataSources.Clear() ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("Data

Delete row from datatable based on column value



I am populating a datatable with data from MSSQL and I have a need to then remove all rows in the datatable that have a certain value in a specific column.

Is there any way I can remove rows from a datatable by specifying the value in the column it should identify the rows by that should be removed? (eg like in MSSQL how you can say delete from tablename where field=value)?

many-to-many mssql schema, linq2sql, objectdatasource with SelectedIndex and delete woes


Hi All,

Here's the setup (excuse no code the setup doesn't post well):


table > link table > table (M:M)

link table is composite key with constraints


linq2sql DataContext of above schema


object datasource bound to Business Logic class (exposing the LINQ2SQL class methods) (ObjDs1)

ListView with DataSourceId to above objds (Lv1)

object datasource with control parameter aimed at above ListView SelectIndex (ObjDs2)

ListView aimed at above objds (Lv2)


select item in Lv1 > postback > Lv2 displays correct records for the M:M

On Delete (heres the fun) the delete fires, the data is deleted and constraints work (no error) BUT Lv2 still displays the deleted data...

I have called Page_PreRender Page.DataBind() just to make sure every control in the heirachy is rebound but to no avail...

I also noted that the OnDeleted event in the ObjDs2 has an AffectedRows of -1.... but the data is definitely deleted.


How to bind DataTable to ObjectDataSource



is it possible to bind DataTable to ObjectDataSource? 


ObjectDataSource SelectMethod needs to be updated at runtime


<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="CustomGetAllUsers"

MembershipUserCollection userCollection = System.Web.Security.Membership.GetAllUsers();

 protected DataSet CustomGetAllUsers( )
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt = ds.Tables.Add("Users");

            dt.Columns.Add("UserName", Type.GetType("System.String"));
            dt.Columns.Add("Email", Type.GetType("System.String"));
            dt.Columns.Add("CreationDate", Type.GetType("System.DateTime"));
            dt.Columns.Add("LastActivityDate", Type.GetType("System.DateTime"));

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.

Top .NET Performance Problems and how to avoid them

Every time I work with one of our .NET customers to help them with managing their application performance I come across the same problems as seen with other clients before: lots of ADO.NET queries, many hidden exceptions in core or 3rd party .NET libraries, slow 3rd party components, inefficient custom code

How to bind GridView and DropDownList controls using DataTable object

In .net framework 2.0, you can load a DataTable in memory by consuming a data source using a DataReader. In this version you will find the flexibility in accessing the DataReader means write access code once and use again and again using DataReader. To see the functionality of DataTable consider an example that is created using VS2005.

DataTable - Load from IDataReader - CreateDataReader - ReadXml WriteXml - Free ADO.NET Tutorials

The DataTable in ADO.NET 2.0 has been seriously beefed up and I absolutely love it. Not everything we consume deals with multiple resultsets and requires the ever popular DataSet. With the cool new features in the ADO.NET 2.0 DataTable, we can get many of the cool features in the DataSet without all the extra baggage and memory footprint particular to multiple resultsets.

Load DataTable from IDataReader

Visual Studio 2008 Memory problems


My memory in task manager reaches about 900,000K  and I don't know why it does this. Definately slows everything down, especially when I rebuild my tableAdapters, takes about 30 seconds sometimes to rebuild the project.

Problems with Forms Authentication in DD 4 site


Hello,  I am seeing a strange problem with Forms Authentication in my DD site.   A user logs into and can view/edit/delete data all day, but when they execute a Custom Filter against data (for example , a control DynamicData/Filters/CustomerLastNameSearch.ascx ) then the site auth fails, and redirects to the log in screen.

in web.config I have

     <authentication mode="Forms">
            <forms name=".Star" loginUrl="~/Login.aspx" protection="All" defaultUrl="~/Default.aspx" path="/" timeout="43200" cookieless="UseCookies" />     

Offhand, I am thinking two things : that DynamicData/Filters path requires some special handling for some reason, or the control extension ascx is causing auth to get confused.   Has anyone else experienced this or have any suggestions?  Thanks!

Datatable in session


I am creating a simple shopping cart using a datatable that is passed into a session object.  I have a small mini cart that displays the total number of items in the cart as well as the running total - this is displayed in a web control.  When the web control starts up I run a try/catch to see if the session object exists, if it doesn't I create the datatable for the cart and store it in the session.  I then have a datalist control that contains all the information for my products including an "add to cart" button.  When the page loads everything looks fine.  If I click on an "add to cart" button nothing happens to the session information.  If I click on it again, the session information updates - displaying only one item is in the cart and then counts up correctly there after.  Additionally, after adding an item, if I select a different item in the list, the same issue occurs.  I am not sure why this is happeing, if anyone can give me an idea I would appreciate it. 

holding datatable value using session



iam insert record using datable in gridivew,iam using  session to hold the record,

there is any other way to hold the record in datable.


        DataRow row;
        row = dtempty.NewRow();
        row["ratetypeid"] = ddlratetype.SelectedValue;
        row["ratedesc"] = ddlratetype.SelectedItem.Text;
        row["slabs"] = ddlslabs.SelectedValue;   

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