DataGridview and SQL Insert - VS2005 Beta 2

  • Thread starter Thread starter Necqui Teja
  • Start date Start date
N

Necqui Teja

I'm getting the following error when inserting a new row...

DataGridView Default Error Dialog
System.Data.NoNullAllowedException: Column 'ADTBAS' does not allow nulls.

The DataGridView was created by dragging the table from the Data Source.
Column ADTBAS was removed from the DataGridView by way of Edit Columns.

The InsertCommand in the Properties window of my AdSignsTableAdapter (.xsd
file) looks as follows:

INSERT INTO AdSigns
(ADTSTO, ADTUPC, ADTPRI, ADTDTE, ADTLIM, ADTDS1,
ADTDS2, ADTDS3, ADTSIZ, AFTFMT, AFTCOP, ADTMLT, ADTBAS, ADTDIS, ADTITM,
AFTSTO,
AFTUPC)
VALUES
(@ADTSTO,@ADTUPC,@ADTPRI,@ADTDTE,@ADTLIM,@ADTDS1,@ADTDS2,@ADTDS3,@ADTSIZ,@AFTFMT,@AFTCOP,@ADTMLT,@ADTBAS,@ADTDIS,@ADTITM,@AFTSTO,@AFTUPC)

what do I have to do to insert a nonnull value into the ADTBAS field when
ADTBAS in not defined in my DataGridView?
 
Hi Necqui,

Thanks for your post.

Does this exception throw out when databinding update the underlying
DataSet, or when you use inser command to update the value back into
database?

Based on the NoNullAllowedException from MSDN, we can see:
"Represents the exception that is thrown when attempting to insert a null
value into a column where AllowDBNull is set to false"

This is because you have deleted 'ADTBAS' column in DataGridView level,
then when adding a new row, winform databinding will update the new row
into the DataSet. However, we did not provide value for 'ADTBAS' column, if
this column's AllowDBNull value is set to false, this exception will throw.

So I think you should explicitly set this column's AllowDBNull to true.(Or
provide a default value through DefaultValue property) Also, you may need
to change this constraint in underlying DataBase for 'ADTBAS' field.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jeffrey, Thanks for your reply.

The exception is thrown when databinding updates the DataSet (as soon as I
press the tab key past the last column in the DataGridView in insert mode).

How do I set the column's AllowDBNull to true or provide a DefaultValue?
where will I find the property for these settings?

Necqui.
 
Hi Necqui,

Thanks for your feedback.

I think you may click the DataSet1.xsd. In the Schema designer, we can
click the column row we want to change property(then the focus caret will
go into that row). Then in the right PropertyBrowser, we can change the
column's schema property.

For example, we can "nillable" property to true to indicate that this
column accept a null value. Or you may set a valid value for "default"
property in the PropertyBrowser.

Note: if you set nillable to true, then when you update the new added
record back to database, you have to change in the database that it also
accepts null value, or a ADO.net exception will throw.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top