Click here to Skip to main content
15,397,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I created a C# windows app program using visual studio and connect it to SQl Server Database already using SQL server authentication and place my connection code:
namespace ContactDirectory
{
    class connection
    {
        SqlConnection conn;
        public SqlConnection getConnect()
        {
            conn = new SqlConnection("Server=ITSTAFF\\ACOSTA,1433; Database=Contacts; User ID=ACOSTA Password=itacosta; Trusted_Connection=True");
            return conn;
        }
    }
}


The program I created is more on searching from what I input in the database.
I published the program. First I Installed it in my computer where my database is and it runs okay. I installed it to the computer(connected same router and both already did firewall rules) the program runs okay but after utilizing search box error occurred =Login failed for user 'ITSTAFF/Guest'.

Can Anyone help me.
please

What I have tried:

namespace ContactDirectory
{
    class connection
    {
        SqlConnection conn;
        public SqlConnection getConnect()
        {
            //conn = new SqlConnection("Data Source=ITDEPT\\ACOSTA; Initial Catalog=Contacts;Integrated Security=True");
            //return conn;
            //connectionString="Data Source=ACER\SQLEXPRESS;Initial Catalog=Contacts;Integrated Security=True"
            //conn = new SqlConnection("Server=ITDEPT\\ACOSTA,1433; Database=Contacts; User ID=itstaffacosta Password=acostait; Trusted_Connection=True");
            conn = new SqlConnection("Server=ITSTAFF\\ACOSTA,1433; Database=Contacts; User ID=ACOSTA Password=itacosta; Trusted_Connection=True");
            return conn;
        }
    }
}
Posted
Updated 14-Feb-20 3:00am

The error message is pretty clear - or at least as clear as it can be without compromising security.
The server you are connecting to does not have a SQL user with that userID and password combination.

We can't fix that for you: we have no access to the server and it's user tables. And neither - I suspect - do you, according to the error message!
   
Comments
Kinneth Custodio Tianchon 14-Feb-20 3:27am
   
yes it has. thats why it worked to the computer where I program it. but after published it and installed to the other computer that error occured.
F-ES Sitecore 14-Feb-20 4:11am
   
What account is that computer logged in as?
All of the connection strings you've shown are using Windows authentication, not SQL authentication.

SqlConnection.ConnectionString Property (System.Data.SqlClient) | Microsoft Docs[^]

Integrated Security=True and Trusted_Connection=True mean the same thing: use Windows authentication.

You're also missing a semicolon between the user ID and the password.

And finally, it's a really bad idea to store a connection in a class-level field. Create the connection as late as possible, and dispose of it as soon as you've finished with it. Where possible, wrap it in a using block to make sure it's always disposed of.
C#
namespace ContactDirectory
{
    static class connection
    {
        public static SqlConnection getConnect()
        {
            return new SqlConnection("Server=ITSTAFF\\ACOSTA,1433; Database=Contacts; User ID=ACOSTA; Password=itacosta;");
        }
    }
}
Microsoft SqlClient Data Provider for SQL Server Connection Strings - ConnectionStrings.com[^]
   
Comments
Kinneth Custodio Tianchon 17-Feb-20 22:15pm
   
THANK YOU GREAT HELP!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900