Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Business Rules that access the data
Message
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Environment versions
Environment:
C# 1.1
OS:
Windows 2000 SP4
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01061497
Message ID:
01062102
Views:
12
Antonio,

>I am still having problems with the DB access from the rules object. For one thing, in the detail screen, the dataset has only one record (either a new empty of an existing record), so I can't search for duplicates there.
>
>Then I tried to use the HostObject, using the syntax shown on the documentation, and I get a type conversion error:
>
>C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\MM Classic Business Library1\ARTISTRules.cs(83): Cannot convert type 'MM_CD_Tracker.ARTISTRules' to 'OakLeaf.MM.Main.Business.mmBusinessObject'

It looks like you're trying to convert the rules object to mmBusinessObject rather than the associated business object.

The following example checks if the CustomerID value is unique when saving a new Customer record.

In C#:
public string ValidateNewCustomerID(string customerID)
{
 	string message = null;
	mmDataAccessBase dao = ((mmBusinessObject)this.HostObject).GetDataAccessObject();
	IDbConnection Connection;

	IDataReader Reader =
		dao.ExecReader("SELECT * FROM Customers WHERE CustomerID = @CustomerID",
		out Connection,
		new IDbDataParameter[] { dao.CreateParameter("@CustomerID", customerID) });

	if (Reader.Read())
	{
		message = "Customer ID must be unique";
		if (this.DataSet != null)
		{
			this.AddErrorProviderBrokenRule("CustomerID",
				message);
		}
	}
	Reader.Close();
	return message;
}
And in VB .NET:
Public Function ValidateNewCustomerID(customerID As String) As String
   Dim message As String = Nothing
   Dim dao As mmDataAccessBase = DirectCast(Me.HostObject, mmBusinessObject).GetDataAccessObject()
   Dim Connection As IDbConnection
   
   Dim Reader As IDataReader = dao.ExecReader("SELECT * FROM Customers WHERE CustomerID = @CustomerID", Connection, New IDbDataParameter() {dao.CreateParameter("@CustomerID", customerID)})
   
   If Reader.Read() Then
      message = "Customer ID must be unique"
      If Not (Me.DataSet Is Nothing) Then
         Me.AddErrorProviderBrokenRule("CustomerID", message)
      End If
   End If
   Reader.Close()
   Return message
End Function
Note this code assumes you are saving a new record. You can determine if a record is new by checking if DataRow.RowState is equal to DataRowState.Added.
Kevin McNeish
Eight-Time .NET MVP
VFP and iOS Author, Speaker & Trainer
Oak Leaf Enterprises, Inc.
Chief Architect, MM Framework
http://www.oakleafsd.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform