Smart questions
Smart answers
Smart people
Join Tek-Tips Forums

Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Manually adding data to a datacolumn in a datatable

intrex (IS/IT--Management) (OP)
23 Sep 04 18:19
I don't understand why I can't add data using any of the following calls.

dc = new DataColumn("par_value");
            dc.AllowDBNull = false;
            dc.DataType = System.Type.GetType("System.String");
            dc.Caption = "par_value";

            //add a new row with datavalues
            DtParam.Rows[0].ItemArray[2] = txtOrder.Text;
            DtParam.Rows[0][2] = txtOrder.Text;

I thought I should be able to change the value just by setting it without any of the beginedit or acceptchanges methods.  I can't get anything to work.  Is there no way to manually add data to one colunmn in a datarow that exists in a datatable?  Do I have to break the datarow out of the table and then add it again?
alfer (Programmer)
24 Sep 04 2:35
The following code works:

DataTable dtParams=new DataTable("parameters");
DataRow dr=dtParams.NewRow();
intrex (IS/IT--Management) (OP)
24 Sep 04 8:27
I haven't had any problems adding data to a row then adding that to a table.  The problem I have is that I already have a table structure with data built.  I then want to add a new column to that existing table and add values to the column that I just created while leaving the existing data intact.  
obislavu (Programmer)
24 Sep 04 9:39
The following code works depending when you set the AllowDBNull property:


dc = new DataColumn("par_value");
dc.AllowDBNull = false;  // Here is the problem
dc.DataType = System.Type.GetType("System.String");
dc.Caption = "par_value";
The dc.AllowDBNull = false; will throw an exception if there is an attempt to set this column to null.
There are two cases:
a) The table has no rows. Maybe some columns and you want to add new column.
   -Use the above code to add the columns but with AllowDBNull=true (this is the reason the default value in fact is true).
   -Add row(s) to the table: DataRow dr = DtParam.NewRow();DtParam.Rows.Add(dr);
   -Now set the AllowDbNull = false; for the columns with not nulls
   -Set column data. An exception will be thrown if null is assigned to columns with AllowDBNull  false. So, you should use try-catch.
b) The table already has rows. That means there are columns but you want to add a new column with AllowdBNull = false.
   -Use above code example to add the columns but with AllowDBNull=false;
   -Set column data on existing rows or add new rows as explained in a)
     An exception will be thrown if null is assigned to columns with AllowDBNull false.
dtCbx is a DataTable with some columns and rows.


DataColumn dc = new DataColumn("par_value");
dc.AllowDBNull = false;
dc.DataType = System.Type.GetType("System.String");
dc.Caption = "par_value";
DataRow dr = dtCbx.NewRow();


DataColumn dc = new DataColumn("par_value");
dc.AllowDBNull = false;
dc.DataType = System.Type.GetType("System.String");
dc.Caption = "par_value";
DataRow dr = dtCbx.NewRow();
dtCbx.Rows[dtCbx.Rows.Count -1]["par_value"]="Gigi";

An exception is thrown e.g. "Column 'par_value' does not allow nulls" if :
dr["par_value"]=null; or dtCbx.Rows[dtCbx.Rows.Count -1]["par_value"]=null;

intrex (IS/IT--Management) (OP)
24 Sep 04 10:16
Thanks for good post.  Unfortunatly none of the examples or explanations fix my problem.  The real problem is that this call will not set a value in a column that is is already added to a Datatable.  


The reason why I have to use this call is because I need to add data to an existing row (not add a new row).  The row that I am adding data to already has data in its first 2 columns.  I added another column to this datatable then tried to set values in my added column.  I can find no way to do this.  

I can even put a watch on  DtParam.Rows[0].ItemArray[2]

When I try to change the value through the watch it goes imediately back to DBNull (if i don't use a default value) or a string (if I set the defaultvalue of the column to a string).

If you build a datatable with two columns.  Then add two rows to it with data.  Now try to add another datacolumn to your datatable.  Finally Try to add data to your new column in the rows that you created in the first step like so.  You will find that "new data" will never be added to the column.  

 DtParam.Rows[0].ItemArray.SetValue("new data",2);
 DtParam.Rows[0].ItemArray[2] = "new data";
 DtParam.Rows[0][2] = "new data";

obislavu (Programmer)
25 Sep 04 11:35
Assume the DtParam table has minimum two rows .

Add new column with setting AllowDBNull to false.


DataColumn dc = new DataColumn("par_value");
dc.DataType = System.Type.GetType("System.String");
dc.Caption = "par_value";

// Set AllowDBNull to false
DtParam.Columns["par_value"].AllowDBNull = false;

// Now try to update "par_value" column
DtParam.Rows[0]["par_value"]="Gigi";  // OK
DtParam.Rows[1]["par_value"]="Giorgio"; // OK

// Try to put NULL in "par_value" column. Exception will be thrown  
DtParam.Rows[0]["par_value"]=null; // Exception
DtParam.Rows[0]["par_value"]=null; // Exception

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close