I have a form that has a table adapter for a table called costing. Im not using any of the items in this table in any textboxes etc.. on the form but I would like to use them in calculations that will give results to put in text boxes on these forms. I'm confused in how to call up the items in the costing table. If i use the name assigned to them in the table even thought the table is attached it does not seem to work.

I tried this:

totaldsgn = drawdsgnhrs (where drawdsgnhrs is the variable in the costing table)

Dani AI

Generated

: pointed you in the right direction — your costing values live in the dataset/data table and should be pulled into local variables for math rather than referenced directly by control names. The most robust approach is to extract a typed value from the DataRow, handle DBNull safely, then do the calculation in a local variable and put the result into the textbox.

Here is a concise, null-safe pattern using the DataRow.Field<T> helper (works well for typed or untyped DataTables):

DataTable cost = myDataSet.Tables["Costing"];
if (cost.Rows.Count > 0)
{
    decimal? draw = cost.Rows[0].Field<decimal?>("drawdsgnhrs");
    if (draw.HasValue)
    {
        decimal totalDsgn = draw.Value * hourlyRate;
        // assign totalDsgn to the textbox here
    }
}

If your form uses a BindingSource (common with designer-created forms), read the current row via the BindingSource to get the value the user is looking at:

var drv = costingBindingSource.Current as DataRowView;
decimal? draw = drv?.Row.Field<decimal?>("drawdsgnhrs");
if (draw.HasValue) totalDesign = draw.Value * hourlyRate;

Additional practical tips:

  • For a single scalar value, add a TableAdapter query that returns only that column (more efficient than loading the whole table).
  • If you must locate a specific row, set the DataTable PrimaryKey and use Rows.Find(key) or use DataTable.Select with a filter.
  • Always confirm the dataset column name and CLR type in the Dataset Designer; mismatches cause runtime errors.
  • Use decimal for monetary calculations and mind culture-specific decimal separators when converting from strings.
  • Changes made in-memory are not persisted until you call the appropriate TableAdapter Update method.

These patterns avoid DBNull exceptions, keep UI and logic separate, and make the calculations predictable and testable.

Recommended Answers

All 2 Replies

The table adapter fills a dataset or a datatable, you should have the DataSet on the designer of your form and possibly a binding source.

Locate the tableAdapter.Fill, more than likely in your Form's load event. It will look like

taAdjustment.Fill(dsRptCashAdjust.Adjustment);

Now to access the data, you will make a call that looks like:

dsRptCashAdjust.Adjustment[0].NewBalance

The [0] is the row index, so choose which row you need. "NewBalance" is a table variable in my application, change it to the name of the variable in your application.

Watch out for DBNull's, if you attempt to access a variable that is a null value the application will throw an exception. You need to test with:

dsRptCashAdjust.Adjustment[0].IsNewBalanceNull()

Thanks SKnake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.