PC Review


Reply
Thread Tools Rate Thread

AllowDBNull property

 
 
Tony
Guest
Posts: n/a
 
      19th Dec 2011
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

 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      21st Dec 2011
On 12/19/2011 7:32 AM, Tony wrote:
> 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



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:13 AM.