4

EDIT: It only took a week but I eventually found out the issue, primarily due to pure luck and another error with a more specific fix. The issue was with the connStr I had made, which for some reason on this machine gave me the error randomly of "System.ArgumentException: Keyword not supported: 'datasource'." during runtime. I then found out a fix for that was to rename the connStr as follows:

 connStr = @"server = (server name); Initial Catalog = AutoTestDB; Integrated Security = true";

Should you have this error as I have, try that method of connection. END EDIT

I'm currently working on Automated Testing using Katalon Automated Testing, essentially Selenium for Chrome, and whenever I'm attempting to add the results of the test to our Test Results Database the SQL Exception "A network-related or instance-specific error occurred while establishing a connection to SQL Server. " keeps popping up. TCP/IP is open, as is firewall and remote connections, and I have the SQL-SMS open and running while I run the database with the SQL connection.

However it only happens whenever I'm using a certain machine to access the database which is stored within the machine itself, as it is with every other machine that I use and they all work perfectly fine. The only difference I can think of for this machine is that it uses SQL Express while all the others that I use have the full version of Microsoft SQL-SMS-17.

It's a genuine case of "It works on my machine", except with the caveat that it works on several others and even across different users as we are all working on this automated testing, this machine is the lone exception for this code not working, with the only difference being that it uses SQL Express which should be accounted for with the \\SQLExpress.

C# code with SQL connetions to edit the values into an already made table within the database.

public void testDBAdd(String testName, Boolean pass, String testComment)
    {
        SqlConnection con;
        SqlDataAdapter daAutoTest;
        DataSet dsAutoTestDB = new DataSet();
        SqlCommandBuilder cmdBAutoTest;
        String connStr, sqlAutoTest;

        connStr = @"datasource = .\\sqlexpress; Initial Catalog = AutoTestDB; Integrated Security = true";
        con = new SqlConnection(connStr);

        sqlAutoTest = @"SELECT * FROM TestResults";
        daAutoTest = new SqlDataAdapter(sqlAutoTest, connStr);
        cmdBAutoTest = new SqlCommandBuilder(daAutoTest);
        daAutoTest.FillSchema(dsAutoTestDB, SchemaType.Source, "AutoTest");
        daAutoTest.Fill(dsAutoTestDB, "AutoTest");

        foreach (DataRow drAutoTest in dsAutoTestDB.Tables["AutoTest"].Rows)
        {
            if (pass == true && drAutoTest["testName"].ToString() == testName)
            {
                drAutoTest.BeginEdit();

                drAutoTest["testName"] = testName;
                drAutoTest["testResult"] = 1;
                drAutoTest["testComment"] = testComment;

                drAutoTest.EndEdit();
                daAutoTest.Update(dsAutoTestDB, "AutoTest");
            }
            else if (pass == false && drAutoTest["testName"].ToString() == testName)
            {
                drAutoTest.BeginEdit();

                drAutoTest["testName"] = testName;
                drAutoTest["testResult"] = 0;
                drAutoTest["testComment"] = "Exception: " + testComment;

                drAutoTest.EndEdit();
                daAutoTest.Update(dsAutoTestDB, "AutoTest");
            }
        }
    }

Code which runs the actual test and gathers if it has passed or failed due to the presence of certain elements, in this case is a certain page displayed when the user logs in and clicks a button.

public void settingTest<TestNumber>()
    {
        IWebDriver driver = new ChromeDriver();
        ChromeOptions options = new ChromeOptions();
        options.AddArguments("--start-maximized");
        driver = new ChromeDriver(options);
        String testName = "<Test Number>", testComment = "";
        Boolean pass = false;
        try
        {
            settingsLogin(driver);
            settingsClick(driver);

            Assert.IsTrue(driver.FindElement(ElementLocator).Displayed);

            if (driver.FindElement(ElementLocator).Displayed == true)
            {
                testComment = "Pass";
                pass = true;
                testDBAdd(testName, pass, testComment);
            }
        }
        catch (Exception ex)
        {
            testComment = "" + ex.TargetSite + "" + ex.Message;
            testDBAdd(testName, pass, testComment);
        }
        finally
        {
            driver.Close();
        }
    }
  • 4
    Your connect code is looking for an sql express instance ".\\sqlexpress" .... you will need to change that if you dont have an instance called that on the local machine – BugFinder May 14 '18 at 14:08
  • and you probably need to setup permission stuffs + enable tcp connection. Used to be a thing in the past not sure if the newer version did that by default. – Steve May 14 '18 at 14:10
  • Could be one of about a half dozen things. Check TCP is enabled, the connection string works on the machine, that DB is online, etc. etc. – Liam May 14 '18 at 14:11
  • Does sound like the problem is that you are expecting the user to have a local instance of SQL Server (Express). Normally an applcation would connect to a specific machine (Server) which hosts the SQL Server Instance. If they User is expected to have an installation of SQL Server (express), then the user needs to firstly have that installed, and then have the correct database on the server, and have their Domain User added as a login and User on the Server and database respective (as you're using Integrated Security). I imagine, really, you wanted them to connect the correct server. – Larnu May 14 '18 at 14:15
  • To clarify this server still hasn't gone live onto the network yet and is still being exclusively hosted locally with each machine we use with an instance being active while the testing is being tested. This will be changed once the database goes live onto the network and handed off to another team for that. Will update further should these check yield fruitful results. – John McGlinchey May 14 '18 at 14:27
  • Possible duplicate of [Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?](https://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci) – Alejandro May 14 '18 at 19:01
  • To all who helped here I thank you, the issue was eventually figured out, the answer can be found in the edit to the main post and the accepted answer below. – John McGlinchey May 18 '18 at 11:24

2 Answers2

1

Not sure, but I think your connection string has an extraneous backslash. You've prefaced the string with a "@" but then used "\\" in the Data Source. You might also try "(localdb)\SQLExpress" as the data source.

BoCoKeith
  • 595
  • 6
  • 14
1

It only took a week but I eventually found out the issue, primarily due to pure luck and another error with a more specific fix. The issue was with the connStr I had made, which for some reason on this machine gave me the error randomly of "System.ArgumentException: Keyword not supported: 'datasource'." during runtime. I then found out a fix for that was to rename the connStr as follows:

 connStr = @"server = (server name); Initial Catalog = AutoTestDB; Integrated Security = true";

Should you have this error as I have, try that method of connection. And thanks to the users who tried to help in both the comments of the post and in the answers section of this post.