Monday, March 12, 2012

how to Copy/Clone a derived DataTable

I would like to add a few properties and methods to an ADO.NET DataTable. So
the normal thing would be to derive a class:
Public Class MyTable
Inherits DataTable
Private This As That
Private TheOtherThing As Whatever
Now, a DataTable has Clone and Copy methods. Is there a straight forward way
for my Derived class to implement Clone and Copy, and rely on the base class
to copy the schema and data? Or do I have to do the heavy lifting myself?
I mean, I can create Clone and Copy functions in the derived MyTable class,
mark them as Shadows to have them replace or override these functions in the
base class, but is there any easy way to copy the schema and data?
ThanksIf it's 2.0, then Serialize and Deserialize your derived DataTable.
- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
----
"Mark" <mark.bax@.blaisesoft-xxx.com> wrote in message
news:%235wOqEfSGHA.1204@.TK2MSFTNGP12.phx.gbl...
>I would like to add a few properties and methods to an ADO.NET DataTable.
>So the normal thing would be to derive a class:
> Public Class MyTable
> Inherits DataTable
> Private This As That
> Private TheOtherThing As Whatever
> Now, a DataTable has Clone and Copy methods. Is there a straight forward
> way for my Derived class to implement Clone and Copy, and rely on the base
> class to copy the schema and data? Or do I have to do the heavy lifting
> myself?
> I mean, I can create Clone and Copy functions in the derived MyTable
> class, mark them as Shadows to have them replace or override these
> functions in the base class, but is there any easy way to copy the schema
> and data?
> Thanks
>

Labels: , , , , , , , , , , ,

How to copy one datatable to another

Hi

I've got a typed Dataset class, myDatasetclass, which I generate an instance of (myDataset is the instance) and then store data locally inside the tables of this dataset instance.

Instead of "Filling" say, myDataset.tblUsers with a dataadapter directly however, my app sends this table to a windows service, which contacts a backend database, gets the data, and returns the 'myDataset.tblUsers', containing data, to my app.

Problem is, I need to be able to say something like:

Code Snippet

myDatasetclass myDataset = new myDatasetclass();

myDataset.tblUsers = myService.GetDatafromdbase(myDataset.tblUsers);

(this last part 'myDataset.tblUsers' is the table being sent to the service)

But it says myDataset.tblUsers (the first part above before the '=', is read-only, and can't be passed the new table with data which was returned from myService.

However, if I do this it works fine:

Code Snippet

Datasetclass.tblUsers mytblUsers = new Datasetclasss.tblUsers();

mytblUsers = myService.GetDatafromdbase(mytblUsers);

Only then, I'm filling a table by itself, instead of a table within the Dataset with all the relationships already established (because it's part of a class which establishes these relationships.)

It is becuase that DataTable can't be serialized and send as a result of WebService. Use Dataset for that, and you will get the table from dataset like this:

tblUsers = myService.GetDatafromdbase().Tables[0]; //if GetDatafromdbase returns a dataset that contain datatable tblUsers.

One more thing is that when you returning data, for this case table, better is to call it asynchronously, because every method has asinchronous method and event when that request is completed, it will be like this:

myService.GetDatafromdbaseCompleted += new GetDatafromdbaseCompletedEventHandler(OnUsersData);
myService.GetDatafromdbaseAsync();

for this you will create handler named OnUsersData:

private void OnUsersData(object sender, GetDatafromdbaseCompletedEventArgs e)
{
if (e.Result != null)
{
tblUsers = e.Result.Tables[0];
//do some other stuff if necessary, fill some grid, raise some other event, or any other action.
}
}


Thanks

Where you have:

Code Snippet

tblUsers = myService.GetDatafromdbase().Tables[0]

Do you mean:

Code Snippet

myDataset.tblUsers = myService.GetDatafromdbase().Tables[0]

Because tblUsers is partof the myDataset Dataset, and having this and = returns a 'read-only' error.


The myDataSet.tblUsers property is ReadOnly and can't be assigned to a new DataTable.

Instead you could try Merge:

myDataset.tblUsers.Merge(myService.GetDatafromdbase().Tables[0])

this will bring in all rows from the datatable.


How come I can still 'Fill' the table using an adapter if it is read-only?

eg:

adap.fill(myDataset.tblUsers);


What I did is auto-generated a typed dataset from my database, which also created a C# class file with all the tables and properties and relationships, whcih I intended to just create an instance of and write data to, but instead of using Fill, I'd use the

Code Snippet

myDataset.tblUsers = myService.GetDatafromdbase(myDataset.tblUsers);

Where the section in brackets is the table being sent to the service empty to be returned containing data.

Would it be best then to just 'rebuilt' all of this dataset in code (thats a lot of code for 50+ tables and relationships), and redefine all the relationships in code, seeing as I can't make an instance of the class mentioned above to write to?

Eg:

Code Snippet

myDataset newdataset = new myDataset();

myDataset.tblUsers newtblUsers = new myDataset.tblUsers();

myDataset.tblUsers newtblUsers = new myDataset.tblUsers();

...etc...

then put the tables in the dataset and define all the relationships?

Seems strange I can't just create an instance of my entire dataset class and then write table data into it with:

myDataset.tblUsers = myService.GetDatafromdbase(myDataset.tblUsers);

when using an adapter I could fill the myDataset.tblUsers table with data regardless of it being read-only.

If i create an instance of the table, but not the table within the dataset, the above works too, but then its a table by itself without relationships.

Labels: , , , , , , , , , , , , ,

How to copy DataRows between DataTables?

Hi all,
I want to copy or move an existing DataRow wich belongs to a DataTable to
another DataTable, but I got the exception "This row already belongs to
another table".
I tried to use the ImportRow method, but this method inserts the row at the
end of the DataTable, and I need to put it at an specific position. The
method InsertAt returns the expeption ahead.
There's a way to copy a DataRow in a specific position? Or, maybe, a way to
detach the row from the original DataTable?
Thanks in advance,
Fabrício de Novaes Kucinskis.Well, I found a solution, copying the source row data to a new row, using
the ItemArray property, and then attaching the new row to the destination
table via InsertAt.
The problem now is that the row is attached to the end of the DataTable, not
in the position I set in InsertAt. My DataTable has a binded DataGrid.
I search the web, and it appears to be a .NET bug. Is it correct? If no,
what can I do? If yes, WHAT CAN I DO'?
Thanks again,
Fabrício.
"Fabrício de Novaes Kucinskis" <fabricio.nk@.uol.com.br> wrote in message
news:eF3NfRvPDHA.2480@.tk2msftngp13.phx.gbl...
> Hi all,
> I want to copy or move an existing DataRow wich belongs to a DataTable to
> another DataTable, but I got the exception "This row already belongs to
> another table".
> I tried to use the ImportRow method, but this method inserts the row at
the
> end of the DataTable, and I need to put it at an specific position. The
> method InsertAt returns the expeption ahead.
> There's a way to copy a DataRow in a specific position? Or, maybe, a way
to
> detach the row from the original DataTable?
> Thanks in advance,
>
> Fabrício de Novaes Kucinskis.
>
I am unsure if this is a bug or not. How you may get around this is use a sorted DataView on the DataTable and bind your grid to the DataView.
>--Original Message--
>Well, I found a solution, copying the source row data to a new row, using
>the ItemArray property, and then attaching the new row to the destination
>table via InsertAt.
>The problem now is that the row is attached to the end of the DataTable, not
>in the position I set in InsertAt. My DataTable has a binded DataGrid.
>I search the web, and it appears to be a .NET bug. Is it correct? If no,
>what can I do? If yes, WHAT CAN I DO'?
>Thanks again,
>
>Fabr=EDcio.
>
>"Fabr=EDcio de Novaes Kucinskis" <fabricio.nk@.uol.com.br> wrote in message
>news:eF3NfRvPDHA.2480@.tk2msftngp13.phx.gbl...
>> Hi all,
>> I want to copy or move an existing DataRow wich belongs to a DataTable to
>> another DataTable, but I got the exception "This row already belongs to
>> another table".
>> I tried to use the ImportRow method, but this method inserts the row at
>the
>> end of the DataTable, and I need to put it at an specific position. The
>> method InsertAt returns the expeption ahead.
>> There's a way to copy a DataRow in a specific position? Or, maybe, a way
>to
>> detach the row from the original DataTable?
>> Thanks in advance,
>>
>> Fabr=EDcio de Novaes Kucinskis.
>>
>
>.
>
Hi, David. Thanks for your answer.
I cannot use a DataView because the user wants to edit the data in DataGrid.
"David Brenchley" <brenchld@.iomega.com> wrote in message
news:052e01c33f50$e8771e00$a101280a@.phx.gbl...
I am unsure if this is a bug or not. How you may get
around this is use a sorted DataView on the DataTable and
bind your grid to the DataView.
>--Original Message--
>Well, I found a solution, copying the source row data to
a new row, using
>the ItemArray property, and then attaching the new row
to the destination
>table via InsertAt.
>The problem now is that the row is attached to the end
of the DataTable, not
>in the position I set in InsertAt. My DataTable has a
binded DataGrid.
>I search the web, and it appears to be a .NET bug. Is it
correct? If no,
>what can I do? If yes, WHAT CAN I DO'?
>Thanks again,
>
>Fabrício.
>
>"Fabrício de Novaes Kucinskis" <fabricio.nk@.uol.com.br>
wrote in message
>news:eF3NfRvPDHA.2480@.tk2msftngp13.phx.gbl...
>> Hi all,
>> I want to copy or move an existing DataRow wich
belongs to a DataTable to
>> another DataTable, but I got the exception "This row
already belongs to
>> another table".
>> I tried to use the ImportRow method, but this method
inserts the row at
>the
>> end of the DataTable, and I need to put it at an
specific position. The
>> method InsertAt returns the expeption ahead.
>> There's a way to copy a DataRow in a specific
position? Or, maybe, a way
>to
>> detach the row from the original DataTable?
>> Thanks in advance,
>>
>> Fabrício de Novaes Kucinskis.
>>
>
>.
>
>I cannot use a DataView because the user wants to edit the data in DataGrid.
And why do you think you can't edit your data anymore, if you use a
DataView' You still can!
Check it out - every DataTable has a "DefaultView" property, which
exposes the default view of the table - you can use that to set the
Sort property of the view, and then use the view as your grid's
source, instead of your table directly.
Read up on ADO.NET in a good book - it has *LOTS* of goodies to offer!
DataView also allow you to specify e.g. a RowFIlter - very powerful
stuff. And everything stays editable!
Marc
================================================================Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Oh my God. I don't believe I lost a lot of time, based on this supposition.
Marc, thanks for your answer. I'll try your tips.
"Marc Scheuner [MVP ADSI]" <m.scheuner@.inova.SPAMBEGONE.ch> wrote in message
news:c5h7gvkjkoq9ap4m9t3pdr1h41gbr9bp0h@.4ax.com...
> >I cannot use a DataView because the user wants to edit the data in
DataGrid.
> And why do you think you can't edit your data anymore, if you use a
> DataView' You still can!
> Check it out - every DataTable has a "DefaultView" property, which
> exposes the default view of the table - you can use that to set the
> Sort property of the view, and then use the view as your grid's
> source, instead of your table directly.
> Read up on ADO.NET in a good book - it has *LOTS* of goodies to offer!
> DataView also allow you to specify e.g. a RowFIlter - very powerful
> stuff. And everything stays editable!
> Marc
> ================================================================> Marc Scheuner May The Source Be With You!
> Bern, Switzerland m.scheuner(at)inova.ch

Labels: , , , , , , , , , , ,

How to copy a row in a Dataview based on user selection?

I have a datatable that I'm displaying in a Winform Datagrid via a
DataView, and I want the user to be able to right-click on any of the rows
and select a "copy row" command from a context menu. Then I need to make
the copy happen.
I've got the context menu up and responding appropriately, but I'm having
difficulty dealing with the copying:
Firstly, how do I identify what row the user wants to copy?
Second, how do I make a copy of the row? There is no "Clone" method for
a row. Do I need to do an element-by-element copy of the row? And if so,
can I do this with an enumerator or must I use column names?
I've been struggling with this for a half a day; I'd sure appreciate any
help.
--
MichaelMichael,
To find the current dataRow try
DataView dataView = (DataView)((CurrencyManager)
dataGrid1.BindingContext[ dataGrid1.DataSource, dataGrid1.DataMember
]).List;
DataRow dataRow = dataView[ dataGrid1.CurrentRowIndex ].Row;
To copy the row into another dataTable (with the same structure) try
destinationTable.Rows.Add(dataRow.ItemArray);
All the Best,
Phil.
"Michael Kellogg" wrote:
> I have a datatable that I'm displaying in a Winform Datagrid via a
> DataView, and I want the user to be able to right-click on any of the rows
> and select a "copy row" command from a context menu. Then I need to make
> the copy happen.
> I've got the context menu up and responding appropriately, but I'm having
> difficulty dealing with the copying:
> Firstly, how do I identify what row the user wants to copy?
> Second, how do I make a copy of the row? There is no "Clone" method for
> a row. Do I need to do an element-by-element copy of the row? And if so,
> can I do this with an enumerator or must I use column names?
>
> I've been struggling with this for a half a day; I'd sure appreciate any
> help.
> --
> Michael
>
<PhilWilliams@.discussions.microsoft.com> wrote:
> Michael,
> To find the current dataRow try
> DataView dataView = (DataView)((CurrencyManager)
dataGrid1.BindingContext [dataGrid1.DataSource,
dataGrid1.DataMember]).List;
> DataRow dataRow = dataView[ dataGrid1.CurrentRowIndex ].Row;
> To copy the row into another dataTable (with the same structure) try
> destinationTable.Rows.Add(dataRow.ItemArray);
> All the Best,
> Phil.
Thanks for the help, Phil. I'm still trying to understand exactly what
you're doing, here. I've never really understood BindingManagerBase
objects and their function. I like the way you added a copy of a row
without really cloning it; I would not have thought of that.
One issue I'm having is that, when capturing the right-click that brings
up the context menu, if you click on the row itself (the technical term
escapes me; the little box on the left side of a row), the
CurrentRowIndex property does NOT seem to update with that row. You have
to actually click on a cell inside the row. Consequently, the row
returned is wrong. I tried doing a SELECT on the row identified in the
MouseDown event handler (where the use of HitTest will give me the
correct row index), but this doesn't seem to work, either. Is there a
way to "activate" a row so the CurrentRowIndex property is guaranteed to
be right?
Thanks again for the help.
MK
Michael,
In contextMenu_Popup, try
DataGrid.HitTestInfo hti = dataGrid1.HitTest(
dataGrid1.PointToClient(Control.MousePosition));
if (hti.Row != -1)
{
DataView dataView = (DataView)((CurrencyManager)dataGrid1.BindingContext[
dataGrid1.DataSource, dataGrid1.DataMember ]).List;
DataRow dataRow = dataView[ hti.Row ].Row;
}
Cheers,
Phil.
"Michael Kellogg" wrote:
> <PhilWilliams@.discussions.microsoft.com> wrote:
> > Michael,
> > To find the current dataRow try
> >
> > DataView dataView = (DataView)((CurrencyManager)
> dataGrid1.BindingContext [dataGrid1.DataSource,
> dataGrid1.DataMember]).List;
> > DataRow dataRow = dataView[ dataGrid1.CurrentRowIndex ].Row;
> >
> > To copy the row into another dataTable (with the same structure) try
> > destinationTable.Rows.Add(dataRow.ItemArray);
> >
> > All the Best,
> > Phil.
>
> Thanks for the help, Phil. I'm still trying to understand exactly what
> you're doing, here. I've never really understood BindingManagerBase
> objects and their function. I like the way you added a copy of a row
> without really cloning it; I would not have thought of that.
> One issue I'm having is that, when capturing the right-click that brings
> up the context menu, if you click on the row itself (the technical term
> escapes me; the little box on the left side of a row), the
> CurrentRowIndex property does NOT seem to update with that row. You have
> to actually click on a cell inside the row. Consequently, the row
> returned is wrong. I tried doing a SELECT on the row identified in the
> MouseDown event handler (where the use of HitTest will give me the
> correct row index), but this doesn't seem to work, either. Is there a
> way to "activate" a row so the CurrentRowIndex property is guaranteed to
> be right?
> Thanks again for the help.
> MK
>
"=?Utf-8?B?UGhpbCBXaWxsaWFtcw==?="
<PhilWilliams@.discussions.microsoft.com> wrote:
> Michael,
> In contextMenu_Popup, try
> DataGrid.HitTestInfo hti = dataGrid1.HitTest(
> dataGrid1.PointToClient(Control.MousePosition));
> if (hti.Row != -1)
> {
> DataView dataView => (DataView)((CurrencyManager)dataGrid1.BindingContext[
> dataGrid1.DataSource, dataGrid1.DataMember ]).List;
> DataRow dataRow = dataView[ hti.Row ].Row;
> }
That is very clever; I like it. Any chance I can do something like this
in the MenuItem CLICK handler instead (obviously the mouse will have
moved by the time I select a context menu item)? I can get the row using
your method either in the POPUP, as you suggest, or in the datagrid CLICK
handler, as well. Then my thought was to stash the row number in a
member variable and use it in the MenuItem CLICK handler. However, this
didn't seem so elegant to me and I thought it'd be nice to actually
determine the row inside the handler. Am I obsessing?
--
Michael
Michael,
Add the following lines
private int clickedRow = -1;
.
.
.
private void contextMenu_Popup(object sender, System.EventArgs e)
{
DataGrid.HitTestInfo hti = dataGrid1.HitTest(
dataGrid1.PointToClient(Control.MousePosition));
clickedRow = hti.Row;
menuItemCopyRow.Enabled = clickedRow != -1;
}
private void menuItemCopyRow_Click(object sender, System.EventArgs e)
{
if (clickedRow == -1) return;
DataView dataView = (DataView)((CurrencyManager)dataGrid1.BindingContext[
dataGrid1.DataSource, dataGrid1.DataMember ]).List;
DataRow dataRow = dataView[ clickedRow ].Row;
}
All the best,
Phil.
> >
> > if (hti.Row != -1)
"Michael Kellogg" wrote:
> "=?Utf-8?B?UGhpbCBXaWxsaWFtcw==?="
> <PhilWilliams@.discussions.microsoft.com> wrote:
> > Michael,
> > In contextMenu_Popup, try
> >
> > DataGrid.HitTestInfo hti = dataGrid1.HitTest(
> > dataGrid1.PointToClient(Control.MousePosition));
> >
> > if (hti.Row != -1)
> > {
> > DataView dataView => > (DataView)((CurrencyManager)dataGrid1.BindingContext[
> > dataGrid1.DataSource, dataGrid1.DataMember ]).List;
> > DataRow dataRow = dataView[ hti.Row ].Row;
> > }
> That is very clever; I like it. Any chance I can do something like this
> in the MenuItem CLICK handler instead (obviously the mouse will have
> moved by the time I select a context menu item)? I can get the row using
> your method either in the POPUP, as you suggest, or in the datagrid CLICK
> handler, as well. Then my thought was to stash the row number in a
> member variable and use it in the MenuItem CLICK handler. However, this
> didn't seem so elegant to me and I thought it'd be nice to actually
> determine the row inside the handler. Am I obsessing?
> --
> Michael
>
<PhilWilliams@.discussions.microsoft.com> wrote:
> Michael,
> Add the following lines
> private int clickedRow = -1;
> .
> .
> .
> private void contextMenu_Popup(object sender, System.EventArgs e)
> {
> DataGrid.HitTestInfo hti = dataGrid1.HitTest(
> dataGrid1.PointToClient(Control.MousePosition));
> clickedRow = hti.Row;
> menuItemCopyRow.Enabled = clickedRow != -1;
> }
> private void menuItemCopyRow_Click(object sender, System.EventArgs e)
> {
> if (clickedRow == -1) return;
> DataView dataView => (DataView)((CurrencyManager)dataGrid1.BindingContext[
> dataGrid1.DataSource, dataGrid1.DataMember ]).List;
> DataRow dataRow = dataView[ clickedRow ].Row;
> }
> All the best,
> Phil.
Thanks, Phil!
-mk

Labels: , , , , , , , , , , , , , ,

How to copy a row from one DataTable to another DataTable?

Hi all,
Assume that both DataTable are the same schema, I want to copy a row from
one DataTable to another DataTable by
m_dtItem.Rows.Add(m_dtOrgItem.Rows(0))
However, it said that
Exception Details: System.ArgumentException: This row already belongs to
another table.
So, How can copy a row from one DataTable to another DataTable?
Thank you.use m_dtItem.ImportRow instead of m_dtItem.Rows.Add
"angus" <angus@.angus.com> wrote in message
news:ODVqYLWwEHA.1292@.TK2MSFTNGP10.phx.gbl...
> Hi all,
> Assume that both DataTable are the same schema, I want to copy a row from
> one DataTable to another DataTable by
> m_dtItem.Rows.Add(m_dtOrgItem.Rows(0))
> However, it said that
> Exception Details: System.ArgumentException: This row already belongs to
> another table.
> So, How can copy a row from one DataTable to another DataTable?
> Thank you.
>
Hi -
You have to use the ItemArray to copy the row
m_dtItem.Rows.Add(m_dtOrgItem.Rows(0).ItemArray)
Shankar
11/3/2004 10:36:04 AM
angus <angus@.angus.com> wrote in message
<ODVqYLWwEHA.1292@.TK2MSFTNGP10.phx.gbl>
> Hi all,
> Assume that both DataTable are the same schema, I want to copy a
row from
> one DataTable to another DataTable by
> m_dtItem.Rows.Add(m_dtOrgItem.Rows(0))
> However, it said that
> Exception Details: System.ArgumentException: This row already
belongs to
> another table.
> So, How can copy a row from one DataTable to another DataTable?
> Thank you.
Right
To prevent that everybody reads everything again and againg
:-)
> use m_dtItem.ImportRow instead of m_dtItem.Rows.Add
>

Labels: , , , , , ,

How to copy a datatable to another specify column mapping

Hello,

Can any one show me how to copy the data in one datatable to another datatable? For example:

say from tableA .ColumnX to tableB.ColumnY

Thanks in advance for any suggestions.

You may wish to look into the bulk copy class featured in .Net 2.0 this can only be used with SQL Server. You can read morehere

You could also read from your current table and insert ino the new table using another command object. If you are unsure about doing this, reply to this thread and let me know which language you prefer and I'll post an example.

Good Luck


First make sure that the two columns are of the same size.Then may you use the sql statements--"Insert" to add the data column from Table B into Table A.

Maybe what I said was not correct,Please tell me.

Labels: , , , , , , , , , , ,

how to convert to sqltype?

For example there is a DataTable dt and the first column in the firstrow is type of string, I want to assign it to SqlString as following:

SqlString s = (SqlString)dt.Rows[0][0];

The above code throws "Invalid cast" exception. My question is how tocast it to SqlString in this case? In general, how about cast othertypes? such as if the second column in the first row is type ofdecimal, then how to cast it to SqlDecimal?

SqlDecimal d ? dt.Rows[0][1]

Thanks.

I think you could do:

SqlString s = new SqlString(dt.Rows[0][0].ToString());

I'm curious why you're wanting to work with it as a SqlString?

Marcie

Labels: , , , , , , , , , , , ,

How to convert SqlDatareader to DataTable

Hello all,

Is there a way to convert a SqlDatareader to DataTable?

I'm not talking about looping through the datareader and add each value into the datatable. For example the dataset, you use a DataAdapter to fill the data into the dataset.

I've read on something like a ovrride the fill function and of the DbDataAdapter ... but it doesn't seem to work ...

protected overridable function(dataTable as DataTable, dataReader as IDataReader) as integer
return Fill(dataTable, dataReader)
end function

Has anyone done this before? Any input is appreciated.

Unfortunately, as far as I know, the answer is no - due to the inherent nature of a DataReader itself. It's a forward only/read once process. If you don't do something with it at the time it's being created, it's gone.

The only way I can think of to take care of that, would be to manually insert the objects into the datatable, as the datareader was being read


David, your icon is scary ... lol. thanks for the respond. please take a peek at this URL:http://msdn2.microsoft.com/en-us/library/497b1k4t.aspx

and also this sample:http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=628

I tried but somehow I can't get it to run. Well, I didn't copy all of the author's code. What do you think?

Labels: , , , , , , , ,

How to convert empty string to Null of Datetime data type

Hi,
This is very annoying to me.
I have a datatable which has two fields of DateTime data type. Both of them are nullable. When I do the update to that datatable, in some cases, Null values are going be put through TextBox and DropDownList's selected Item. But the updated value in database is 01/01/1900 and 12:00 AM which is not the real null value, can anyone help me with this?
My update code:

Dim inputDateAsNewDate
Dim inputSessionAsNewDate

If Trim(txtDate.Text) <> ""Then
inputDate = Convert.ToDateTime(txtDate.Text)
Else
inputDate = System.Data.SqlTypes.SqlDateTime.Null -->01/01/1900
EndIf

If ddlSession.SelectedItem.Text <> ""Then
inputSession = Convert.ToDateTime(ddlSession.SelectedItem.Text)
Else
inputSession = System.Data.SqlTypes.SqlDateTime.Null -->12:00 AM
EndIf

UpdateDT(AgendaItemID, inputDate, inputSession)

Just out of interest, why aren't inputSession and input of type Date System.Data.SqlTypes.SqlDateTime?
If you use the standard CLR DateTime structure, it will also run back to its default value as that's all it can hold.
Make the variables of the appropriate type and then convert the input into SqlDateTime, not the other way around, and your problem should go away.

Labels: , , , , , , , , , , , ,

How to convert a DataView with RowFilter to a new DataTable?

Hello,
I have this:
Dim dv As New DataView(GetUsersAsDataTable())
dv.RowFilter = "brithday >= 1970"
Dim dt As New DataTable
dt = **how to bring my rowfiltered DataView to a new DataTable** ?
When I do
dt = dv.Table
I get my original DataTable so that is unfortunatly not the solution.
Thanks for any help in advance!!
AndreasHi Andreas,
I don't think that there is a built-in way.
Do it manually in a loop, perhaps.
--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
"Andreas Klemt" <aklemt68@.hotmail.com> wrote in message
news:ufJReiBuDHA.540@.tk2msftngp13.phx.gbl...
> Hello,
> I have this:
> Dim dv As New DataView(GetUsersAsDataTable())
> dv.RowFilter = "brithday >= 1970"
> Dim dt As New DataTable
> dt = **how to bring my rowfiltered DataView to a new DataTable** ?
> When I do
> dt = dv.Table
> I get my original DataTable so that is unfortunatly not the solution.
> Thanks for any help in advance!!
> Andreas
>
>
Andreas Klemt wrote:
> Hello,
> I have this:
> Dim dv As New DataView(GetUsersAsDataTable())
> dv.RowFilter = "brithday >= 1970"
> Dim dt As New DataTable
> dt = **how to bring my rowfiltered DataView to a new DataTable** ?
> When I do
> dt = dv.Table
> I get my original DataTable so that is unfortunatly not the solution.
Why do you need it back to a DataTable? You can bind to the DataView
just as easily.

Labels: , , , , , , , ,

How to convert a datareader to datatable?

How to convert a datareader to datatable?DataTable.Load?
--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
"abc my vclass" <myabcclass@.myclass.com> wrote in message
news:eu$Ato6AGHA.2544@.TK2MSFTNGP11.phx.gbl...
> How to convert a datareader to datatable?
>
ABC,
In the same way as that you convert an engine to a wheel of a car.
A datareader is a class that is used by the dataadapter to load a datatable.
I hope this gives an idea.
Cor
Check this blog post:
Convert a DataReader into a DataSet
http://weblogs.asp.net/rosherove/archive/2004/01/22/61541.aspx
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"abc my vclass" <myabcclass@.myclass.com> wrote in message
news:eu$Ato6AGHA.2544@.TK2MSFTNGP11.phx.gbl...
> How to convert a datareader to datatable?
>
Teemu,
Why do you think that as written on the blog page that it is a fast
solution. We have almost as long as our website exist this sample on our
website, however I will never tell that it is fast. It is slow, but can be
used if someone wants a progressbar. And than he/she should have a huge
amount of data and accept that it is going a little bit slower.
http://www.vb-tips.com/default.aspx?ID=49f2cff5-56ad-44fc-a4c6-fc0d5c470f53
Just my thougth,
Cor
Cor,
> Why do you think that as written on the blog page that it is a fast
> solution. We have almost as long as our website exist this sample on our
> website, however I will never tell that it is fast.
Did I say something like that? Which one of those few implementations I've
said is fast? There are quite many variations on that blog, if you check
them (comments also) Not sure if I follow your question, though.
> It is slow, but can be used if someone wants a progressbar. And than
> he/she should have a huge amount of data and accept that it is going a
> little bit slower.
> http://www.vb-tips.com/default.aspx?ID=49f2cff5-56ad-44fc-a4c6-fc0d5c470f53
Ah, If you mean which one I'd use, I'd take the one inheriting from
DBDataAdapter or the one using BeginLoadData and LoadDataRow (DBDataAdapter
uses that internally, so the selection goes to scenario preference). Why?
Because using BeginLoadData turns off constraints , index maintaining etc
when loading data.
Regards,
Teemu
Teemu,
> Ah, If you mean which one I'd use, I'd take the one inheriting from
> DBDataAdapter or the one using BeginLoadData and LoadDataRow
> (DBDataAdapter uses that internally, so the selection goes to scenario
> preference). Why? Because using BeginLoadData turns off constraints ,
> index maintaining etc when loading data.
>
I like just the DBDataadapter.
The slight performance that is gained by other solutions, is in my opinion
lost by wors maintainability.
Cor
Like Miha mentions, Datatable.Load works great ifyou're using the 2.0
Framework, but out of curiousity, what is the challenge your wanting to
address by doing this?
"abc my vclass" <myabcclass@.myclass.com> wrote in message
news:eu$Ato6AGHA.2544@.TK2MSFTNGP11.phx.gbl...
> How to convert a datareader to datatable?
>

Labels: , , , ,