locked
Problem with Dynamic Data RRS feed

  • Question

  • User1207475660 posted

    Hello Everyone

    I am new to Visual Studio and new to Dynamic Data. I have created this Dynamic site using Dynamic Data Web Site template. The language I have chosen is Visual Basic, also have chosen LINQ To SQL classes & Northwind Database for this project.

    I have written a validation that wants the Customer.CompanyName to start with ‘AMI’. Now even if the value starts with ‘AMI’, I get the validation error.

    So to check what’s happening in the background I have set a MsgBox() to fire which returns me the field value.

    Message box get’s fired twice, once with the updated value and then with the old value. So here’s the code I have written in customer class.

    Kindly help me with your suggestions.

    Imports Microsoft.VisualBasic<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>Imports System.ComponentModel<o:p></o:p>Imports System.ComponentModel.DataAnnotations<o:p></o:p><o:p> </o:p><MetadataType(GetType(CustomerMetaData))> _<o:p></o:p>Partial Public Class Customer<o:p></o:p>    Private Sub OnCompanyNameChanging(ByVal value As String)<o:p></o:p>        MsgBox(value)<o:p></o:p>        If Not value.StartsWith("AMI") Then<o:p></o:p>            Throw New ValidationException("Company Name must start with AMI")<o:p></o:p>        End If<o:p></o:p>    End Sub<o:p></o:p>End Class<o:p></o:p><o:p> </o:p>Public Class CustomerMetaData<o:p></o:p>

        Private _CompanyName As Object

        <DisplayName("Company Name")> _<o:p></o:p>

        Public Property CompanyName() As Object

            Get<o:p></o:p>            Return _CompanyName<o:p></o:p>        End Get<o:p></o:p>        Set(ByVal value As Object)<o:p></o:p>            _CompanyName = value<o:p></o:p>        End Set<o:p></o:p>    End Property<o:p></o:p>

    End Class

    Thursday, December 4, 2008 6:40 AM

Answers

  • User-1005219520 posted

    Your partial class should not have  Private _CompanyName As Object or the get/set. That's why the method is being fired twice. The getter/setter is in the data model - you shouldn't duplicate it in your partial class.

    See How to: Customize Data Field Validation in the Data Model which has VB/C# samples.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 5, 2008 4:50 PM

All replies

  • User-330204900 posted

    I've tidied up the class:

     

    <MetadataType(GetType(Customer.CustomerMetaData))> _
    Partial Public Class Customer
    
        Private Sub OnCompanyNameChanging(ByVal value As String)
            If Not value.StartsWith("AMI") Then
                Throw New ValidationException("Company Name must start with AMI")
            End If
        End Sub
    
        Public Class CustomerMetaData
            Private _CompanyName As Object
    
            <DisplayName("Company Name")> _
            Public Property CompanyName() As Object
                Get
                    Return _CompanyName
                End Get
                Set(ByVal value As Object)
                    _CompanyName = value
                End Set
            End Property
        End Class
    End Class

    Hope this helps.

    My thought is; and I've tested it and it does get called twice, should this happen and if so why?

    The other way you could do what you want is via the OnValidate event in the DataContext and then throw your exception there, the only drawback there is that you will not get an error next to the field just in the summary [:(]

    Thursday, December 4, 2008 7:38 AM
  • User1207475660 posted

    Thanks very much for the help. I will certainly try out your solution and will come back with the result. Sorry for the code, showed properly in the preview.

    Regards, Jitendra Shahani

    Friday, December 5, 2008 6:15 AM
  • User-1005219520 posted

    Your partial class should not have  Private _CompanyName As Object or the get/set. That's why the method is being fired twice. The getter/setter is in the data model - you shouldn't duplicate it in your partial class.

    See How to: Customize Data Field Validation in the Data Model which has VB/C# samples.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 5, 2008 4:50 PM
  • User1207475660 posted
    Firstly thanks for all the help. Here is the modified code. Even now this code is running twice and gives the error. Imports Microsoft.VisualBasic Imports System.ComponentModel.DataAnnotations Partial Public Class Product Private Sub OnProductNameChanging(ByVal value As String) If Not value.StartsWith("AMI") Then Throw New ValidationException("Error...") End If End Sub End Class Regards & Thanks Jitendra Shahani
    Wednesday, December 10, 2008 5:57 AM
  • User-1005219520 posted

    Can you get the VB sample at http://msdn.microsoft.com/en-us/library/cc488527.aspx working. That should show you what you are doing wrong.

    Wednesday, December 10, 2008 1:13 PM