Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manually adding data to a datacolumn in a datatable

Status
Not open for further replies.

intrex

IS-IT--Management
Apr 14, 2002
70
0
0
US
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";
DtParam.Columns.Add(dc);

//add a new row with datavalues

DtParam.Rows[0].BeginEdit();
DtParam.Rows[0].ItemArray.SetValue(txtOrder.Text,2);
DtParam.Rows[0].ItemArray[2] = txtOrder.Text;
DtParam.Rows[0][2] = txtOrder.Text;
DtParam.Rows[0].AcceptChanges();
DtParam.Rows[0].EndEdit();

DtParam.AcceptChanges();

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?
 
The following code works:

DataTable dtParams=new DataTable("parameters");
dtParams.Columns.Add("MyString",System.Type.GetType("System.String"));
DataRow dr=dtParams.NewRow();
dr["MyString"]="Hello";
dtParams.Rows.Add(dr);
 
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.
 
The following code works depending when you set the AllowDBNull property:
Code:
dc = new DataColumn("par_value");
dc.AllowDBNull = false;  // Here is the problem
dc.DataType = System.Type.GetType("System.String");
dc.Caption = "par_value"; 
DtParam.Columns.Add(dc);
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.
Example:
dtCbx is a DataTable with some columns and rows.
Code:
DataColumn dc = new DataColumn("par_value");
dc.AllowDBNull = false;
dc.DataType = System.Type.GetType("System.String");
dc.Caption = "par_value"; 
dtCbx.Columns.Add(dc);
DataRow dr = dtCbx.NewRow();
dr["par_value"]="Gigi";
dtCbx.Rows.Add(dr);
Or
Code:
DataColumn dc = new DataColumn("par_value");
dc.AllowDBNull = false;
dc.DataType = System.Type.GetType("System.String");
dc.Caption = "par_value"; 
dtCbx.Columns.Add(dc);
DataRow dr = dtCbx.NewRow();
dtCbx.Rows.Add(dr);
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;

-obislavu-
 
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.

DtParam.Rows[0].ItemArray[2]

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);
OR
DtParam.Rows[0].ItemArray[2] = "new data";
OR
DtParam.Rows[0][2] = "new data";



 
Assume the DtParam table has minimum two rows .

Add new column with setting AllowDBNull to false.
Code:
DataColumn dc = new DataColumn("par_value");
dc.DataType = System.Type.GetType("System.String");
dc.Caption = "par_value"; 
dc.DefaultValue="";
DtParam.Columns.Add(dc);

// 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
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top