views:

27

answers:

3

I first want to add a database column in DataColumn..and then i have to add datacoloumn in datatable.. please guide me;

SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from  exam_cyctstmarkdet",con);
DataColumn c = new DataColumn();                      
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");
A: 

Is the bit you're missing

DataTable DT = new DataTable;
d2.Fill(DT);
DT.Columns.Add(c);

???

Justin Wignall
A: 

If you wish to create a DataTable, and add your own columns to it, you can try

DataTable dt = new DataTable("MyTable");
DataColumn col = dt.Columns.Add("parta", typeof(int));

You only have to keep a reference to the column if you plan to use it somewhere, else you can try

DataTable dt = new DataTable("MyTable");
dt.Columns.Add("parta", typeof(int));
astander
A: 

I would suggest that you use the SqlDataAdapter to populate a DataSet, which will contain a DataTable. I assume you want to add the new column to this DataTable.

Here's how to do that:

SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from  exam_cyctstmarkdet",con);
DataSet dst = new DataSet();
d2.Fill(dst); // now you have a populated DataSet containing a DataTable
DataTable dt = dst.Tables[0]; // I created this variable for clarity; you don't really need it

DataColumn c = new DataColumn();                      
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");

dt.Add(c);

After you add the Column to the DataTable, the Column will be empty.

You can populate it in code.

DOK