Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / October 2003

Tip: Looking for answers? Try searching our database.

SqlDataAdapter.Update doesn't delete, will however insert and update, what gives?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andy Baldwin - 21 Oct 2003 22:52 GMT
Even though the Update, Insert and Select portions of my DataAdapter
function fine using Adapter.Update, the delete functionality never
works. I have tried multiple things and frankly I am lost at this
point. It will be a pretty handy dandy class when I am done, but I
need help to figure out this last problem.

I have a database class (a c# component class so that I could use the
designer for the datasets and adapters etc...). I am using MSDE (MS
Database Engine) on a win2k box. I can connect to it via access and
VS.Net fine, as well as through my code in the way that I can insert,
select and edit existing records through my dataset. In my following
code, you will see that there are three methods, all three of which
work with a minor exception. Both getDataGridData and addRecord work
consistantly, add record uses the Adapter.Update method successfully
(the select doesn't need to update anything). The last method,
updateDataset, works when I pass in a dataset that has records marked
with a rowstate of changed, but not delete (or remove). I must be
missing something silly here any help will be greatly appreciated, and
I apologize for my short naming conventions and long nested
statements!

using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;

namespace EMS {
public class DataBase : System.ComponentModel.Component {
    private System.Data.SqlClient.SqlCommand InsertCommand;
    private System.Data.SqlClient.SqlCommand DeleteCommand;
    private System.Data.SqlClient.SqlCommand UpdateCommand;
    private System.Data.SqlClient.SqlCommand SelectCommand;
    private System.Data.SqlClient.SqlConnection Conn;
    private System.Data.SqlClient.SqlDataAdapter Adapter;
    public static bool insertCheckboxColumn;
    private EMS.DataSet1 ds;
    private System.ComponentModel.Container components = null;
    public DataBase(System.ComponentModel.IContainer container) {
     container.Add(this);
     InitializeComponent();
    }
    public DataBase() {
     InitializeComponent();
     Conn.Open();
    }
    ~DataBase() {
        Conn.Close();
    }

//Snipped the initialize component chunk, waaaaay to big to put it all
here. I
// am only putting in the delete command specific things

        this.DeleteCommand = new System.Data.SqlClient.SqlCommand();
        this.Adapter.DeleteCommand = this.DeleteCommand;
        this.DeleteCommand.CommandText = "DELETE FROM Offenses WHERE
IP = @IP";
        this.DeleteCommand.Connection = this.Conn;
        this.DeleteCommand.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@IP",
System.Data.SqlDbType.VarChar, 15,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "IP", System.Data.DataRowVersion.Original, null));
           

    public static void addRecord(Record r) {
     DataBase db = new DataBase();
     DataRow dr;
     db.Adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
     try {
       db.Adapter.Fill(db.ds, "Offenses");
     } catch (System.Exception e) {
       System.Console.WriteLine(e.Message);
     }
         //get a new blank row to populate
     dr = db.ds.Tables["Offenses"].NewRow();

     dr["field1"] = r.field1;
     dr["field2"] = r.field2;
     //etc..
         //etc...

        db.ds.Tables["Records"].Rows.Add(dr);
     db.Adapter.Update(db.ds,"Records");
    }
    public static DataSet getDataGridData() {           
     DataBase db = new DataBase();
     DataSet temp = new DataSet();
     try {
       db.Adapter.Fill(temp, "Offenses");
     } catch (System.Exception e) {
       System.Console.WriteLine(e.Message);
     }
     if(insertCheckboxColumn) {
       DataSet junk = new DataSet();
        DataTable table = new DataTable("Offenses");
       junk.Tables.Add(table);
       DataColumn col = new DataColumn("Select", typeof(bool));
       col.DefaultValue = false;
       junk.Tables["Offenses"].Columns.Add(col);
       int i=1;
       db.Adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
       junk.Merge(temp);
       temp = junk;
     }
     return temp;
    }
    public static void updateDataset(DataSet data) {
     DataBase db = new DataBase();
     data.Tables["Offenses"].Columns.Remove("Select");
         db.Adapter.MissingSchemaAction=
ystem.Data.MissingSchemaAction.AddWithKey;
     db.Adapter.Update(data,"Offenses");
     db.Conn.Close();
    }
   }
}
Andy Baldwin - 22 Oct 2003 19:12 GMT
Oops, sorry guys, needed to do a "dr.Delete()" instead of removing it
from the table object...

Thanks anyways!!

AB

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.