?
Solved

How to programatically create a DataRowView in ADO.NET

Posted on 2009-07-13
4
Medium Priority
?
1,043 Views
Last Modified: 2013-11-08
My problem is when I want to create a new row in a typed DataTable and edit it using DataBound WPF controls. Using the (typed) DataTable method: NewTABLERow() I can get a new instance of a typed datarow for TABLE. The particular database table I'm refering to has several columns that allow Null values.
When I bind, for example, a textbox to a Propery from the (Typed) DataRow that contains a Null value, an exception is thrown (StrongTypingException).

Instead of using the typed DataRow, if I used instead a DataRowView of the typed DataRow I should be ok, because no exception would be thrown... The problem is that I'm not finding a way of creating the DataRowView out of the typed DataRow...

Thanks in advance,
Rui
0
Comment
Question by:rui_dias
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 150 total points
ID: 24849714
If you are working with String data in the typed DataSet designer, you can change the field to not throw an exception for null data.


Typed-DataSet-Designer----Null-S.png
0
 

Author Comment

by:rui_dias
ID: 24859289
That does work, thank you.

The way I ended up doing it was, using a view of the DataTable I wanted to add a DataRow to, I used the DataRowView that is returned when you add a new row to the DataView.
MyDataSet.MyDataTable dataTable = new MyDataSet.MyDataTable();
 
//Fill the data table...
 
DataView dataView = dataTable.DefaultView;
 
//To get the new DataRowView:
DataRowView dataRowView = dataView.AddNew();
dataRowView.BeginEdit();
 
//edit the dataRowView here...
 
//Accept(EndEdit) or reject(CancelEdit) the changes: if rejected 
//the DataRowView is not added to the DataTable!
 
dataRowView.EndEdit();
 
//or: dataRowView.CancelEdit();

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24859439
I usually don't go down that route of adding to the DataView with typed DataSets, since you can add to the underlying DataTable, and the changes would be reflected in the DataView.
0
 

Author Comment

by:rui_dias
ID: 24861706
That is true, but the problem is, when you create the typed dataset using the designer from a database with lots of fields that allow null, you get that "Throw Exception" behavior by default on all of them...

I was just trying to find a solution that didn't involve changing the "throw exception" behavior on all of them "by hand", in the designer. Using the views apparently works fine and has the added advantage of not being necessary to do anything to the dataset that is automatically generated using the designer.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question