Page 1 of 1

Data Validation in database

Posted: Wed Jan 23, 2013 3:39 pm
by omayer
What am i missing on this code, can't pass ther error, --No parameterless constructor defined for this object.
Thank you in Advance



Show/Hide Stacktrace
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at Ranorex.Core.Testing.TestModuleActivator.DefaultFactoryMethod.CreateFactory() at Ranorex.Core.Testing.TestSuiteModule.InitializeTestModuleInstance() at Ranorex.Core.Testing.TestSuiteModule.RunWithRepeats(DataContext parentDataContext, ErrorBehavior errorBehavior) at Ranorex.Core.Testing.TestSuiteModule.Run(DataContext parentDataContext, Boolean childSkip)

"
code is used

Code: Select all

 public class MSSQLConnector : ITestModule
    {
    	
    	private string server, database, uid, password, connectionString;

        	//construct	
        public MSSQLConnector(string _server, string _database, string _username, string _password)
        {
         	this.server = _server;
			this.database = _database;
			this.uid = _username;
			this.password = _password;
			this.connectionString = "user id="+this.uid+";"+"password="+this.password+";"+@"server="+this.server+";"+"database="+this.database+";"+"connection timeout=30";

        	
        }


		public DataTable RunQuery(string SQL)
		{

			SqlConnection connection = new SqlConnection(connectionString);
			SqlDataReader reader = null;
			DataTable dt = new DataTable();
			try{
				SqlCommand command = connection.CreateCommand();
				command.CommandText = SQL;
				connection.Open();
				reader = command.ExecuteReader();
				dt.Load(reader);
				}
			catch(Exception e){
				Console.WriteLine("Exception is MSSQLConnector::RunQuery – " + e.ToString());
				}
			finally{
				reader.Close();
				connection.Close();
				}
				return dt;
		}
  void ITestModule.Run()
        {
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;            
      //connector object
     MSSQLConnector db = new MSSQLConnector("NQ08\\NQ08", "QA", "generic", "password");
		
		
			//validate middle name 			
DataTable dt= db.RunQuery("Select * from candidate where firstname='Sam'");
			foreach(DataRow row in dt.Rows)
			{
				Ranorex.Validate.IsTrue(row["middlename"].ToString() == "S", "Validating Sam middle name S.");


		}
    }
}
}

Re: Data Validation in database

Posted: Thu Jan 24, 2013 2:33 pm
by Support Team
Hi,

This is because if you execute the specific CodeModule "MSSQLConnector" in Ranorex, Ranorex internally calls the standard constructor of the CodeModule in order to run it and as there is no such constructor Ranorex throws the mentioned error.
In order to fix this you have to define the standard constructor:
public MSSQLConnector(){
			
		}
or, I would prefer this solution, you could create a normal, a not runnable class (C# Class template), for your SQLConnector.
This SQLConnector class can then be called from your UserCodeModule or your UserCode method of a Recording.

Regards,
Markus

Re: Data Validation in database

Posted: Thu Jan 24, 2013 4:41 pm
by omayer
Thank you Markus for the expalanantion and solution, I will go with C# Class template

Re: Data Validation in database

Posted: Thu Jan 24, 2013 5:31 pm
by Support Team
You're welcome!

This is a good choice ;).

Regards,
Markus