Alien2012
Alien2012

Reputation: 23

Delete a table in the database mdb form

My goal for a project due is to get the Instructors to delete from the MS Access database table Instructor where ID = get id

Now I get an error on the form that says

Use of unassigned local variable 'iD' C:\Users\Tina\documents\visual studio 2013\Projects\Students\Students\DeleteInstructor.cs 29 24 Students

Instructor class:

 class Instructor : Person
 {
        private int iD;
        private String office;
        private String eMail;
        private String message;

        public Instructor() : base()
        {
            this.iD = 0;
            this.office = "";
            this.eMail = "";
        }

        public Instructor(int i, String off, String eM) : base()
        {
            this.iD = i;
            this.office = off;
            this.eMail = eM;
            InsertDB();
        }

        public Instructor(int iD)
        {
            SelectDB(iD);
        }

        //++++++++++++++++  DATABASE Data Elements +++++++++++++++++
        public System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter;
        public System.Data.OleDb.OleDbCommand OleDbSelectCommand;
        public System.Data.OleDb.OleDbCommand OleDbInsertCommand;
        public System.Data.OleDb.OleDbCommand OleDbUpdateCommand;
        public System.Data.OleDb.OleDbCommand OleDbDeleteCommand;
        public System.Data.OleDb.OleDbConnection OleDbConnection;
        public string cmd;

        public void DBSetup(){
        // +++++++++++++++++++++++++++  DBSetup function +++++++++++++++++++++++++++
        // This DBSetup() method instantiates all the DB objects needed to access a DB, 
        // including OleDbDataAdapter, which contains 4 other objects(OlsDbSelectCommand, 
        // oleDbInsertCommand, oleDbUpdateCommand, oleDbDeleteCommand.) And each
        // Command object contains a Connection object and an SQL string object.
            OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
            OleDbSelectCommand = new System.Data.OleDb.OleDbCommand();
            OleDbInsertCommand = new System.Data.OleDb.OleDbCommand();
            OleDbUpdateCommand = new System.Data.OleDb.OleDbCommand();
            OleDbDeleteCommand = new System.Data.OleDb.OleDbCommand();
            OleDbConnection = new System.Data.OleDb.OleDbConnection();


            OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand;
            OleDbDataAdapter.InsertCommand = OleDbInsertCommand;
            OleDbDataAdapter.SelectCommand = OleDbSelectCommand;
            OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand;

OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Reg"+
"istry Path=;Jet OLEDB:Database L" +
"ocking Mode=1;Data Source=c:\\RegistrationMDB.accdb;J" + 
"et OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System datab" + 
"ase=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=S" + 
"hare Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet " + 
"OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" + 
"r=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";

        }  

        public void SelectDB(int id) 
        { //++++++++++++++++++++++++++  SELECT +++++++++++++++++++++++++
            DBSetup();
            cmd = "Select * from Instructors where ID = " + iD;
            OleDbDataAdapter.SelectCommand.CommandText = cmd;
            OleDbDataAdapter.SelectCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  {
                    OleDbConnection.Open();
                    System.Data.OleDb.OleDbDataReader dr;
                    dr = OleDbDataAdapter.SelectCommand.ExecuteReader();

                    dr.Read();
                    id=iD;
                    setOffice(dr.GetValue(1)+"");
                    setEMail(dr.GetValue(2)+"");

            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        } //end SelectDB()

            public void InsertDB() {
        // +++++++++++++++++++++++++++  INSERT +++++++++++++++++++++++++++++++

            DBSetup();
            cmd = "INSERT into Instructors values(" + getID() + "," +
                             "'" + getOffice() + "'," +
                             "'" + getEMail() + ")";

            OleDbDataAdapter.InsertCommand.CommandText = cmd;
            OleDbDataAdapter.InsertCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.InsertCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Inserted");
                else
                    Console.WriteLine("ERROR: Inserting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                
        } 

        public void updateDB() 
        {
        //++++++++++++++++++++++++++  UPDATE  +++++++++++++++++++++++++

            cmd = "Update Instructors set ID = '" + getID() + "'," + 
                        "Office = '" + getOffice() +    "', " +
                        "EMail = '" + getEMail() +   
                         " where ID = " + getID();

            OleDbDataAdapter.UpdateCommand.CommandText = cmd;
            OleDbDataAdapter.UpdateCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);

            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.UpdateCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Updated");
                else
                    Console.WriteLine("ERROR: Updating Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        } //end UpdateDB()

        public void deleteDB(int iD) 
        {
            //++++++++++++++++++++++++++  DELETE  +++++++++++++++++++++++++

            cmd = "Delete from Instructors where ID = " + getID();
            OleDbDataAdapter.DeleteCommand.CommandText = cmd;
            OleDbDataAdapter.DeleteCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.DeleteCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Deleted");
                else
                    Console.WriteLine("ERROR: Deleting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }

        public void setID(int iD)
        {
            this.iD = iD;
        }

        public void setOffice(String office)
        {
            this.office = office;
        }
        public void setEMail(String eMail)
        {
            this.eMail = eMail;
        }

        public int getID()
        {
            return iD;
        }

        public String getOffice()
        {
            return office;
        }

        public String getEMail()
        {
            return eMail;
        }

        public String getMessage()
        {
            return this.message;
        }

        public void displays(){

        System.Console.WriteLine("ID =  "+ getID());
        System.Console.WriteLine("Office =   "+ getOffice());
        System.Console.WriteLine("Email =  " + getEMail());
    }
}

Form:

namespace Students
{
    public partial class DeleteInstructor : Form
    {
        public DeleteInstructor()
        {
            InitializeComponent();
        }

        private void InstructorIDText_TextChanged(object sender, EventArgs e)
        {

        }

        private void Delete_Click(object sender, EventArgs e)
        {
            int iD;
            Instructor s = new Instructor(iD);
            s.deleteDB(iD);
        }
    }
}

Upvotes: 1

Views: 398

Answers (2)

Abdul Saleem
Abdul Saleem

Reputation: 10612

The error is absolutely right. You are not assigning any value to the variable iD before calling it..

private void Delete_Click(object sender, EventArgs e)
{
    int iD;       //   ->   here it is unassigned
    Instructor s = new Instructor(iD);
    s.deleteDB(iD);
}

You should assign a value like

int iD = 0;

Or take value from somewhere like a DataGrid or TextBox or Combobox

int iD = Convert.ToInt32(textBox1.Text);

Upvotes: 0

Praveen Paulose
Praveen Paulose

Reputation: 5771

The error is pretty clear. You have not assigned a value to the variable iD. You need to set it before using it in your delete method.

private void Delete_Click(object sender, EventArgs e)
    {
        int iD = 1;
        Instructor s = new Instructor(iD);
        s.deleteDB(iD);
    }

I have put "1" as an example here. It could be fetched from a control or a selection made by the user, basically some input received from the user.

Upvotes: 1

Related Questions