DataView and Bindings question

T

tawright915

I created a tabbed app that has over a dozen checkboxes on the second
tab. When I add a new record thru dataview, and click on the secodn
tab I get and error Object cannot be cast from other datatype DBNull.

Here is my code for creating my dataview:
try
{
da.Fill(ds);
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
dt = ds.Tables[0];
SqlCommandBuilder cb = new SqlCommandBuilder(da);
dv.Table = dt;

cm = (CurrencyManager)this.BindingContext[dv];



Then I set my bindings:

this.chkbOperationsRpt.DataBindings.Add("Checked", dv,
"OperationsReport");
this.chkbDrugTask.DataBindings.Add("Checked", dv, "DrugTaskForce");
this.chkbCADFire.DataBindings.Add("Checked", dv, "CADFire");
this.chkbNCIC.DataBindings.Add("Checked", dv, "E911NCIC");


Then I add new record:
try
{
dv.RowFilter = string.Empty;
cm.AddNew();
cm.Refresh();
cm.EndCurrentEdit();
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}


I've got my database set where these fields do not allow nulls and the
default value is 0.

So where do I check to see if there are nulls or how can I fix this?

Thanks
Tom
 
A

Alvin Bruney

the best way to do this is to use a strongly typed dataset. if you can't
afford to you, you can always use the IsNull("name of field") property to
test. However it seems that this is not the problem you are experiencing
since your query forbids nulls and you are getting a null on a databind. It
stands to reason that the null is coming from outside the query if i
understand you correctly.


btw don't catch exception, that's bad. Try formatexception or some other
more specific exception.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
T

tawright915

There were some nulls in those fields in SQL, but I removed them and
set the default value of 0. However when do an AddNew record from the
currency manager and then a refresh I still get the error. How can I
make it so that zeros are put in place as the default value.

Can you give me some more info on strongly type datasets. An example of
one field would be great.

Thanks
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top