Click here to Skip to main content
15,909,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am re-writing a sample application from VB.net to C#, its a sample tutorial about using DataSets in n-tier applications. I succeded in all but this line of code:

C#
Dim changes = CType(Me.OrdersDataSet.GetChanges, NorthwindEntities.OrdersDataSet)


My understanding of CType as used in VB is to cast one object to another. How do i cast this in C#?

The complete code for the Sub is:


VB
Private Sub OrdersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersBindingSource.EndEdit()

        Using proxy As New NorthwindService.ServiceClient
            Dim changes = CType(Me.OrdersDataSet.GetChanges, NorthwindEntities.OrdersDataSet)

            If changes IsNot Nothing Then
                If proxy.SaveOrders(changes) Then

                    Dim addedrows = From row In Me.OrdersDataSet.Orders _
                                    Where row.RowState = DataRowState.Added

                    For Each row In addedrows.ToArray
                        row.Delete()
                    Next

                    Me.OrdersDataSet.Merge(changes)
                    Me.OrdersDataSet.AcceptChanges()

                    MsgBox("saved")
                Else
                    MsgBox("Not saved")
                End If
            End If
        End Using
    End Sub


Thank you in advance.
Posted
Updated 19-Aug-10 11:09am
v2

In C# you cast as in C++ - Bitmap b = (Bitmap)n; Or you can use 'as', Bitmap b = n as Bitmap. the difference is that as will return null if it fails and a failed cast will throw an exception.

C#
NorthwindEntities.OrdersDataSet changes = this.OrdersDataSet.GetChanges() as NorthwindEntities.OrdersDataSet;


would be my guess.
 
Share this answer
 
Comments
TheyCallMeMrJames 19-Aug-10 17:12pm    
Reason for my vote of 5
doh! beat me by a fraction of a second!
coommark 19-Aug-10 19:48pm    
Hmmm... Christian your answer is really great. Sorry I pushed 'answer' to ask a question. I am just kind of stuck and needed help. Would not do that again as you said. Thank you. I appreciate your help. I really do!
I haven't tested, but you should try this:

NorthwindEntities.OrdersDataSet changes = this.OrdersDataSet.GetChanges() as NorthwindEntities.OrdersDataSet;


This allows you to test for null before entering the next block.

Cheers.

(oops on the Me, should be this)
 
Share this answer
 
v2
Thank you James and Christian. Thank you so very much for the quick response. Your answers were right.

But if you dont mind let me just pose another question. I was actually trying to re-code a tutorial from VB to C# about using DataSets in an n-tier architecture from here: http://windowsclient.net/learn/video.aspx?v=14625[]. Everything works fine, but I have not been able to get Updates, even though I can fetch.

Please will you take a look and help me with what I need to do to make it update?

Thank you. Thank you. Thank everyone here who helps reply when we ask questions. Thank you all.

Waiting and hoping someone will help.
 
Share this answer
 
Comments
Christian Graus 19-Aug-10 19:36pm    
You should not push 'answer' to ask a question. You'd be better off asking a new question, which this is. Sorry, I don't have time to work through a tutorial for you.
coommark 19-Aug-10 19:49pm    
Hmmm... Christian your answer was really great. Sorry I pushed 'answer' to ask a question. I am just kind of stuck and needed help. Would not do that again as you said. Thank you. I appreciate your help. I really do! Sorry if i asked to much. Thank you for your answers. Thank you.
Toli Cuturicu 20-Aug-10 7:01am    
Reason for my vote of 1
fake answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900