Results 1 to 2 of 2

Thread: [2.0] c# ado.net

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    [2.0] c# ado.net

    why this code doesnt work?

    string ConStr = "Provider=Microsoft.Jet.OleDB.4.0; Data Source =" + Application.StartupPath + "\\Employee.mdb";
    OleDbConnection Conn = new OleDbConnection(ConStr);

    Conn.Open();

    OleDbCommand Cmd = new OleDbCommand("Select * from Employee", Conn);
    OleDbDataAdapter dAdapter = new OleDbDataAdapter(Cmd);
    DataSet dDataSet = new DataSet();

    DataRow AddNewRow = dDataSet.Tables["Employee"].NewRow();
    AddNewRow["EmployeeID"] = textBox1.Text;
    AddNewRow["EmployeeName"] = textBox2.Text;

    dDataSet.Tables["Employee"].Rows.Add(AddNewRow);
    dAdapter.Update(dDataSet, "Employee");

    Conn.Close();

    Use the "new" keyword to create an object instance.

  2. #2
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: [2.0] c# ado.net

    Quote Originally Posted by arshesander
    why this code doesnt work?

    string ConStr = "Provider=Microsoft.Jet.OleDB.4.0; Data Source =" + Application.StartupPath + "\\Employee.mdb";
    OleDbConnection Conn = new OleDbConnection(ConStr);

    Conn.Open();

    OleDbCommand Cmd = new OleDbCommand("Select * from Employee", Conn);
    OleDbDataAdapter dAdapter = new OleDbDataAdapter(Cmd);
    DataSet dDataSet = new DataSet();

    DataRow AddNewRow = dDataSet.Tables["Employee"].NewRow();
    AddNewRow["EmployeeID"] = textBox1.Text;
    AddNewRow["EmployeeName"] = textBox2.Text;

    dDataSet.Tables["Employee"].Rows.Add(AddNewRow);
    dAdapter.Update(dDataSet, "Employee");

    Conn.Close();

    Use the "new" keyword to create an object instance.
    What error are you getting?? If any??

    try changing your code to incorporate a using block, and a try catch block...

    Code:
    string ConStr = "Provider=Microsoft.Jet.OleDB.4.0; Data Source =" + Application.StartupPath + "\\Employee.mdb";
    using (OleDbConnection Conn = new OleDbConnection(ConStr)) {
    
                try {
    
                Conn.Open();
    
                OleDbCommand Cmd = new OleDbCommand("Select * from Employee", Conn);
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(Cmd);
                DataSet dDataSet = new DataSet();
    
                DataRow AddNewRow = dDataSet.Tables["Employee"].NewRow();
                AddNewRow["EmployeeID"] = textBox1.Text;
                AddNewRow["EmployeeName"] = textBox2.Text;
    
                dDataSet.Tables["Employee"].Rows.Add(AddNewRow);
                dAdapter.Update(dDataSet, "Employee");
    
                } catch (Exception ex) {
                     throw new Exception(ex.Message);
                }
    }
    This will intantiate your OleDbConnection object, and at the close of the block, it will properly close and dispose the object...

    The try {} catch{} will catch an an error, and throw a new exception...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width