Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

when i press submit_btn it's open error page tell me the login failed
where i'm trying to open connection .
any one can help me how to fix it .
my code :

C#
protected void submit_btn_Click(object sender, EventArgs e)
    {
        string sql = "select * from User";
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=ecommerceDBConnectionString1;Integrated Security=True;");
        conn.Open();
        SqlCommand command = new SqlCommand(sql, conn);
        SqlDataReader dataReader = command.ExecuteReader();
     
        string name = "deego";
        string password = "123";
        while (dataReader.Read())
        {
            if (name == dataReader.GetString(1).ToString().Trim())
            {
                Response.Write("Name found");

                if (password == dataReader.GetValue(2).ToString().Trim())
                {
                    Response.Write("i's true");

                }
                else
                    Response.Write("Not true");


                break;
            }

        }

   }
Posted
Updated 5-Sep-16 5:59am
v2
Comments
Dave Kreskowiak 4-Sep-16 19:28pm    
What's the exact error message? Right now, it's not clear if the login failure is to SQL Server or if it's just into your application.
alaa deego 5-Sep-16 9:23am    
Cannot open database "ecommerceDBConnectionString1" requested by the login. The login failed.
[no name] 4-Sep-16 19:44pm    
Your website does not have a login for your SQL Server.

You are not seriously saving passwords as plain text into your database are you?
alaa deego 5-Sep-16 9:27am    
i have used before using database with the same syntax and it's run , but i don't know why now its not working
[no name] 5-Sep-16 16:45pm    
Not sure what you did not understand. I just told you why you got that error.

OK, you seem to be confused about a ton of stuff.

First, NEVER put a connection string inside your code. ALWAYS put it in the applications .config file. This is because WHEN the SQL Server name changes, like when you go from a dev machine and deploy to a production machine, the server name WILL CHANGE.

Next, you don't seem to understand the parameters in a connection string. For an SQL Server connection string, "Data Source" is the name of the server that is running SQL Server and, optionally, the name of an SQL Server instance. In your case, the "." means that the SQL Server is expected to be running on the same machine as your code. Is that the case? Probably not.

Next, the "Initial Catalog" is the name of the database you're connecting to. Is your database on the SQL Server called "ecommerceDBConnectionString1". Probably not.

Lastly, in your case, "Trusted Connection" is an option telling the connection to authenticate to the SQL Server using the account that your code is running under. On your dev machine, the code runs as YOU. On a server, it runs as whatever account is setup specifically to the the code, usually the default ASP.NET account. This account rarely ever has a valid login to an SQL Server.

For you, a connection string would probably look more like this:
Server=serverName;Database=databaseName;User Id=sqlServerAccount;Password=password;


But, the exact syntax depends on what Provider you're using.

Bottom line is you REALLY need to do your homework on what a connection string is and what your options are for connection to an SQL Server.
 
Share this answer
 
Please post the error message when you press submit button to identify where the problem occured.
Or also check the Sql Server service is running on your/remote computer.
 
Share this answer
 
It clearly seems that you application is not able to connect to the database using the windows authentication. Though you are using . (dot) which should point to the localhost. Perhaps you need to use proper connection string to connect to the database.

You can create a data link file(UDL file) first to check if you able to connect the database from it. Then you can copy the connection string from there. please check below the steps to crate the UDL file and use it.


Step 1: Right click on desktop and select New>Text Document.

Step 2: Change the file name to conn.udl (yes, change the file extension as well)

Step 3: Now you can check the icon for the file changed from normal txt file. Double click on the file and you can see a window named “Data linked properties”.

Step 4: Go to the first tab, I mean the provider tab. There we can find number of OLE DB providers. Choose the suitable provider name. At you case you can select “Microsoft OLE DB provider for SQL Server”.

Step 5: Then Click “Next>>” button, that will take us to the connection tab. There we need to select or provide a server name. In your case, you can provide just a . or write localhost or your system local IP.

Step 6: As you are interested for the Windows auth, you can select "Use Windows NT Integrated security"

Step 7: Then choose "Select the database on the server" (default). Click on the dropdown to see all the database present. If you see any error while getting the database name, then there is some problem on connecting the database. Better provide the correct server name and use the proper user name and password to connect the database and repeat from Step 5.

Step 8 : Once you see the databases, click "Test connection" button and clock ok.

Step 9: Now we are ready with the connection string. Close the window and right click on the Conn.UDL file. Open the file with notepad. We should having the required connection string.

BTW, keep the connection string inside the configuration file
 
Share this answer
 
v2

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