2

I have this code in Transaction.cs

using (TransactionScope scope = new TransactionScope())
{
        // Setup nhibernate configuration
        Configuration config = new Configuration();            
        config.SetProperty("hibernate.connection.connection_string", GlobalVar.TRUECONNSTRING);
        config.SetProperty("hibernate.command_timeout", "3600");
        config.AddAssembly(typeof(ProductionMovein).Assembly);

        // Setup nhibernate session
        ISessionFactory factory = config.BuildSessionFactory();
        ISession session = factory.OpenSession();
        ITransaction transaction = session.BeginTransaction();

        //Recalculate Number
        PairData pairCabang = (PairData)comboCabang.SelectedItem;
        textNo.Text = FormFunction.getNumber(2, pairCabang.key, dtpTanggal.Value);

        // Insert data
        try
        {
            //ProductionMoveIn
            ProductionMovein productionMoveIn = new ProductionMovein();
            productionMoveIn.Nomor = textNo.Text;
            session.Save(productionMoveIn);    

            transaction.Commit();
            session.Close();
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            session.Close();
            MessageBox.Show(ex.InnerException.Message);
            return 1;
        }

    scope.Complete();
}

And the error is started from

textNo.Text = FormFunction.getNumber(2, pairCabang.key, dtpTanggal.Value);

i have this code in Formfunction.cs

public static string getNumber(int formID, int cabangID, DateTime date)
{
    string formNumber = "";
    string strQuery = "";

        formNumber += formNames[formID, 0] + "/" + + date.ToString("yy") + date.ToString("MM") + "/";

    // Setup nhibernate configuration
    NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();        
        config.SetProperty("hibernate.connection.connection_string", GlobalVar.TRUECONNSTRING);
        config.SetProperty("hibernate.command_timeout", "3600");
    config.AddAssembly(typeof(Login).Assembly);

    //// Setup nhibernate session
    ISessionFactory factory = config.BuildSessionFactory();
    ISession session = factory.OpenSession();

            strQuery = "SELECT MAX(REVERSE(SUBSTRING(REVERSE(a.Nomor), 1, 5))) as 'latest' FROM " + formNames[formID, 1] +
                   " a WHERE a.cabang = " + cabangID +
                   " AND YEAR(a.tanggal) = '" + date.ToString("yyyy");

    Object result = session.CreateSQLQuery(strQuery)
        .AddScalar("latest", NHibernateUtil.Int32)
        .UniqueResult();
    session.Close();

    int nRow;
    if (result == null)
        nRow = 0;
    else
        nRow = (int)result;
    formNumber += (nRow + 1).ToString("d5");

    return formNumber;
}

I have tried to change the Server to 10.10.7.10 (my ip) and it works. but, when i change to other ip, it cannot open connection. I have tried to turn on msdtc on my computer and the other server i tried to connect, but still get the same error. enter image description here

enter image description here

Can anybody help me how to solve this error?

NoOne
  • 171
  • 1
  • 12

1 Answers1

2

Which database are you using? (I've assumed MS SQL) Can you post the exception detail please?

Here's an approach.

  1. First comment out the using (TransactionScope) / scope.Complete and try connect to the remote database - i.e. to ensure that your local Sql client is configured for TCP/IP, the remote server allows remote TCP/IP connections (usually port 1433), and that your login credentials and access are correct.
  2. DTC is usually only required when 2 or more connections are used. If you reinstate the TransactionScope, but then remove the NHibernate transaction, then DTC might not be required at all. Also note that TransactionScopes default to Read Serializable isolation - TransactionScope functions
  3. Ensure that DTC is configured on both your PC and the Server to allow remote connections etc - picture.

    You also need to tackle firewall issues DTC firewall requirements?

EDIT

By default, SQL Express isn't open to remote connections - on the remote server, you will need to enable TCP/IP on SQL Configuration Manager, open up the firewall for 1433 / 1434, and as @Özgür mentions, ensure that the SQL the browser service is running (or change your instance name, or change your connection string to use ip, port). More on this here : http://www.sevenforums.com/system-security/58817-remote-access-sql-server-express-2008-windows-7-a.html

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • i am using MS SQL EXPRESS 2005. I am using 2 databases. The example i make only using one database to make it look more simple. I have tried the number 3 approach in the server i want to connect and also in my computer, but I still get the same error. – NoOne Apr 10 '12 at 06:28
  • 2
    For sql express ensure the SQL Server Browser service is running on server which you are trying to connect. – Özgür Kara Apr 10 '12 at 06:47
  • @NoOne I've updated the answer - you can check whether your SQL Express box is open for remote connections by TELNET MySQLExpressHost 1433 from the command line. Telnet will tell you if it rejects, or allow you to type chars if it connects. – StuartLC Apr 10 '12 at 07:35
  • @nonnb i have turned off the firewall in my computer and the server computer i want to connect. I can connect the server using SQL Server Management Express. And if i use only one database and connect to only the server, the code works fine. – NoOne Apr 10 '12 at 09:20
  • solved. i forget to change the radio button to no authentication required. – NoOne Apr 12 '12 at 02:38