CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2008
    Posts
    10

    Not able to save a column added dynamically to an existing table

    I need to add a new column in the database but cannot. Can someone help me. My code is as below

    My code:

    private void UpdateDatabase()
    {
    DataTable dtprojectinfo = GetTable("ProjectInfo");
    DataTable dtparticipants = m_DBClient.GetTable(TABLETYPE.PARTICIPANTS);
    DataColumn dc = new DataColumn("PaymentStatus");
    dc.DataType = System.Type.GetType("System.Boolean");
    dc.DefaultValue = false;
    dtparticipants.Columns.Add(dc);
    SaveTable(dtparticipants);
    }

    public DataTable GetTable(string TableName)
    {
    OleDbDataAdapter dataAdapter;
    DataTable datatable = null;
    OpenConnection();
    dataAdapter = new OleDbDataAdapter();
    datatable = new DataTable();
    dataAdapter.SelectCommand = new OleDbCommand(@"SELECT * FROM [" + TableName + "]", m_DBConnection);
    dataAdapter.SelectCommand.Parameters.Add("@Icon", OleDbType.Binary);
    dataAdapter.Fill(datatable);
    datatable.TableName = TableName;
    return datatable;
    }

    Public void SaveTable(DataTable dataTable)
    {
    OleDbDataAdapter dataAdapter;
    dataAdapter = new OleDbDataAdapter();
    dataAdapter.SelectCommand = new OleDbCommand(@""ALTER TABLE [" + ActualTableName + "] ADD COLUMN Payment TEXT(25)", m_DBConnection);
    OleDbCommandBuilder cb = new OleDbCommandBuilder(dataAdapter);
    dataAdapter.AcceptChangesDuringUpdate = true;
    OpenConnection();
    dataAdapter.Update(dataTable);

    }

  2. #2
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Not able to save a column added dynamically to an existing table

    I think you messed up Function SaveTable. The first thing you should do is add one column in database table. Only after this step is done, you can do the second step -- use DataAdapter and CommandBuilder to update data in that table. About the second step, you'd better check msdn help about CommandBuilder class.
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

  3. #3
    Join Date
    Apr 2006
    Posts
    220

    Re: Not able to save a column added dynamically to an existing table

    "dataAdapter.Update(dataTable);" does not update the table's schema, it only updates table's data.

    You will have to run ALTER TABLE query through SqlCommand.ExecuteNonQuery() or other similar methods for your OLEDB.
    Last edited by nabeelisnabeel; September 10th, 2008 at 10:11 PM.

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

Featured