AllowDBNull property

T

Tony

Hello!

I read a book and it says the following.
"You can use the AllowDBNull property to control whether the DataColumn will
accept null values. By default, this property is set to true when you create
new DataColumn objects.
Using a SqlDataAdapter object's Fill method to create new DataColumn object
will not set the AllowDBNull property
to True even if the corresponding column in the database does not accept
null values. The sqlDataAdapter will not fetch this schema information from
your database when you call the Fill method. calling the FillSchema method
instead will fetch this information and supply it to new columns in your
DataTable"

When I make a Test for example with the Customers table in the Northwind
database by using the code snippet below. I can see that all column
col1,col2,col3... to col10 has property AllowDBNull set to True.
I can even set the CustomerID to null which is the primary key in the
database.

According to the text in the book it say "Using a SqlDataAdapter object's
Fill method to create new DataColumn object will not set the AllowDBNull
property to True" but this is wrong because the value for AllowDBNull is set
to True.

So my question is if I misunderstand something here or if the book is wrong
in this matter ?

I used this code snippet
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionString);
string strSQL = "select * from Employees";
SqlDataAdapter da = new SqlDataAdapter(strSQL, con);
DataTable tbl = new DataTable();
da.Fill(tbl);

DataRow row = tbl.Rows[0];
row["CustomerID"] = DBNull.Value;

DataColumn col0 = tbl.Columns[0];
DataColumn col1 = tbl.Columns[1];
DataColumn col2 = tbl.Columns[2];
DataColumn col3 = tbl.Columns[3];
DataColumn col4 = tbl.Columns[4];
DataColumn col5 = tbl.Columns[5];
DataColumn col6 = tbl.Columns[6];
DataColumn col7 = tbl.Columns[7];
DataColumn col8 = tbl.Columns[8];
DataColumn col9 = tbl.Columns[9];
DataColumn col10 = tbl.Columns[10];
}

//Tony
 
A

Arne Vajhøj

I read a book and it says the following.
"You can use the AllowDBNull property to control whether the DataColumn
will accept null values. By default, this property is set to true when
you create new DataColumn objects.
Using a SqlDataAdapter object's Fill method to create new DataColumn
object will not set the AllowDBNull property
to True even if the corresponding column in the database does not accept
null values. The sqlDataAdapter will not fetch this schema information
from your database when you call the Fill method. calling the FillSchema
method instead will fetch this information and supply it to new columns
in your DataTable"

When I make a Test for example with the Customers table in the Northwind
database by using the code snippet below. I can see that all column
col1,col2,col3... to col10 has property AllowDBNull set to True.
I can even set the CustomerID to null which is the primary key in the
database.

According to the text in the book it say "Using a SqlDataAdapter
object's Fill method to create new DataColumn object will not set the
AllowDBNull property to True" but this is wrong because the value for
AllowDBNull is set to True.

So my question is if I misunderstand something here or if the book is
wrong in this matter ?

I think there is a typo in the book.

I think they wanted to say:

"Using a SqlDataAdapter object's Fill method to create new DataColumn
object will not set the AllowDBNull property to False"

because that actually matches the following text:

"even if the corresponding column in the database does not
accept null values"

Arne
 

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