Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Update to sql tabale using LINQ, WCF RSS

8 replies

Last post Nov 19, 2008 12:54 PM by maheshshinde9

(0)
  • maheshshinde9

    maheshshinde9

    Member

    14 Points

    43 Posts

    Update to sql tabale using LINQ, WCF

    Nov 19, 2008 10:27 AM | LINK

    when user click on particular record from datagrid & then it should display it's information ( means bind) the data to textboxes

    means firstname, lastname & Then I can change the values of the textboxes (means update).

    Means my requirement is binding data  to textboxes & do update on that textboxes .

    Regards,
    Mahesh
    --------------------
    click "Mark as Answer" on the post that helps you.
  • Ken Tucker

    Ken Tucker

    All-Star

    23246 Points

    3532 Posts

    Re: Update to sql tabale using LINQ, WCF

    Nov 19, 2008 11:20 AM | LINK

    Set up your textboxes to bind to the right fields with a mode of twoway.   In the datagrid's SelectedChanged event set the textboxes datacontext equal to datagrid's selecteditem

     

     

    <TextBox x:Name="txtFirstName" Text="{Binding firstame, Mode=TwoWay}"></TextBox>
      
        Private Sub dgCategories_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles dgCategories.SelectionChanged
            txtFirstName.DataContext = dgCategories.SelectedItem
        End Sub
     

  • maheshshinde9

    maheshshinde9

    Member

    14 Points

    43 Posts

    Re: Update to sql tabale using LINQ, WCF

    Nov 19, 2008 11:37 AM | LINK

    It is not working:

    I have 3 columns in datagrid. I have 3 textboxes.  

    <TextBox Width="75"  Height="25" Margin="10"  Name="txtminrange" Text="{Binding minrange,Mode=Twoway}" VerticalAlignment="Bottom"/>

    <TextBox Width="75" Height="25" Margin="10"  Name="txtmaxrange" Text="{Binding maxrange, Mode=TwoWay}" VerticalAlignment="Bottom"/>

    <TextBox  Width="75" Height="25" Margin="10" Name="txtamount" Text="{Binding amount, Mode=TwoWay}" VerticalAlignment="Bottom"/>

    private void theDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {

             tbrule selectedRow = theDataGrid.SelectedItem as tbrule;

            txtamount.DataContext = selectedRow.amount.ToString();

            txtmaxrange.DataContext = selectedRow.maxrange.ToString();

            txtminrange.DataContext = selectedRow.minrange.ToString();

    }

    But it is not showing any text in Textbox.

    Regards,
    Mahesh
    --------------------
    click "Mark as Answer" on the post that helps you.
  • Ken Tucker

    Ken Tucker

    All-Star

    23246 Points

    3532 Posts

    Re: Update to sql tabale using LINQ, WCF

    Nov 19, 2008 11:47 AM | LINK

    Change your code to this

     

     

    private void theDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    {
    
             tbrule selectedRow = theDataGrid.SelectedItem as tbrule;
    
            txtamount.DataContext = selectedRow;
    
            txtmaxrange.DataContext = selectedRow;
    
            txtminrange.DataContext = selectedRow;
    
    }
    
    
     

  • maheshshinde9

    maheshshinde9

    Member

    14 Points

    43 Posts

    Re: Update to sql tabale using LINQ, WCF

    Nov 19, 2008 11:52 AM | LINK

    Yes it is working. But on Page Load it is showing values in textboxes. Also I want to update( means change values in textbox) & submit it to the table.

    Means how to do update to table? 

    Regards,
    Mahesh
    --------------------
    click "Mark as Answer" on the post that helps you.
  • Ken Tucker

    Ken Tucker

    All-Star

    23246 Points

    3532 Posts

    Re: Update to sql tabale using LINQ, WCF

    Nov 19, 2008 12:07 PM | LINK

    You are going to have to keep track on the items that are changed and send them to a update method in your wcf service.  In the service you need to find the unchanged record via the primary key and copy all the changed properties to the orginal record and submit the changes to the database.  Kind of a pain.  It is a little easier with ado.net data services.

  • maheshshinde9

    maheshshinde9

    Member

    14 Points

    43 Posts

    Re: Update to sql tabale using LINQ, WCF

    Nov 19, 2008 12:14 PM | LINK

    can you show it by example? means how to write update method in wcf? OR just sample code
    Regards,
    Mahesh
    --------------------
    click "Mark as Answer" on the post that helps you.
  • Ken Tucker

    Ken Tucker

    All-Star

    23246 Points

    3532 Posts

    Re: Update to sql tabale using LINQ, WCF

    Nov 19, 2008 12:35 PM | LINK

     

    I will put a complete example on my blog within the next few days but maybe this will help

     

        Public Function UpdateEntity(ByVal en As node) As Boolean
            ' Add your operation implementation here
            If Not IsLoggedIn() Then Return Nothing
            Dim bDeleted As Boolean = True
            Dim db As New YouthMapDataContext(ConfigurationManager.ConnectionStrings("LocalSqlServer").ToString)
            Try
                Dim q = (From e In db.nodes Select e Where e.id = en.id).FirstOrDefault
                With q
                    .name = en.name
                    .description = en.description
                    .address_line_1 = en.address_line_1
                    .address_line_2 = en.address_line_2
                    .city = en.city
                    .description = en.description
                    .email_address = en.email_address
                    .state = en.state
                    .zip = en.zip
                    .name = en.name
                    .IconName = en.IconName
                    .lat = en.lat
                    .long = en.long
                    .tag = en.tag
                    .X = en.X
                    .Y = en.Y
                End With
                db.SubmitChanges()
            Catch ex As Exception
                bDeleted = False
                Debug.Print(ex.ToString)
            End Try
            Return bDeleted
        End Function
     
  • maheshshinde9

    maheshshinde9

    Member

    14 Points

    43 Posts

    Re: Update to sql tabale using LINQ, WCF

    Nov 19, 2008 12:54 PM | LINK

    Thanks for your code. I will try it. Basically I am using C#. & I want code through WCF means I want to define method [oerationcontract] & do update to database table.

    Regards,
    Mahesh
    --------------------
    click "Mark as Answer" on the post that helps you.