Which code segment should you use?

A Windows Forms application contains the following code segment.

string SQL = @”SELECT OrderID, ProductID, UnitPrice, Quantity FROM [Order Details]”;
SqlDataAdapter da = new SqlDataAdapter(SQL, connStr);
DataTable dt = new DataTable();
da.Fill(dt);

You need to add a new column to the data table named ItemSubtotal. The ItemSubtotal column must contain the value of the UnitPrice column multiplied by the value of the Quantity column.
Which code segment should you use?

A Windows Forms application contains the following code segment.

string SQL = @”SELECT OrderID, ProductID, UnitPrice, Quantity FROM [Order Details]”;
SqlDataAdapter da = new SqlDataAdapter(SQL, connStr);
DataTable dt = new DataTable();
da.Fill(dt);

You need to add a new column to the data table named ItemSubtotal. The ItemSubtotal column must contain the value of the UnitPrice column multiplied by the value of the Quantity column.
Which code segment should you use?

A.
DataColumn col = new DataColumn(“ItemSubtotal”);
col.DataType = typeof(decimal);
col.Expression = “UnitPrice * Quantity”;
dt.Columns.Add(col);

B.
dt.Compute(“UnitPrice * Quantity”, “ItemSubtotal”);

C.
DataColumn col = new DataColumn(“ItemSubtotal”);
col.DataType = typeof(decimal);
dt.Columns.Add(col);
dt.Compute(“UnitPrice * Quantity”, “ItemSubtotal”);

D.
DataColumn col = new DataColumn(“ItemSubtotal”);
col.DataType = typeof(decimal);
col.DefaultValue = “UnitPrice * Quantity”;
dt.Columns.Add(col);



Leave a Reply 0

Your email address will not be published. Required fields are marked *