0

I'm creating a register page . When I write connection string to connect to SQL Server 2014 , this error is shown: Error Pic

protected void btnsignup_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("server=(local);DataBase=MyDataBase;integrated security=True");
    SqlCommand cmd = new SqlCommand("insert into Users (Username,UPassword,Uname,UEmail) values ('"+ tbUname.Text + ","+ tbPass.Text + ","+tbname.Text +","+ tbemail.Text +"')");
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
} 
Thiago Ferreira
  • 661
  • 3
  • 19
  • So what is unclear to you in this error message? Make sure this user (the webserver is running with this user credentials) has access to SQL serrver. – Andrey Korneyev Mar 04 '16 at 11:44
  • post the stack trace error message. It might be helpful to find what went wrong – Kira Mar 04 '16 at 11:51
  • 2
    Also to point out, inserting users in the way you are right now (concatenated strings) leave your system fully open to SQL injection attacks. – Takarii Mar 04 '16 at 12:04
  • Check this link: **http://stackoverflow.com/questions/7698286/login-failed-for-user-iis-apppool-asp-net-v4-0** – Syed Jamil Uddin Mar 04 '16 at 12:29
  • You should use try catch blocks to catch exceptions and show meaningful message instead of crashing your server – Bhargav Mar 04 '16 at 12:30
  • 1
    You need to read about, understand and utilize parameterized queries. This is a textbook example of sql injection. Also, you should NOT store passwords in clear text. They should be salted and hashed. – Sean Lange Mar 04 '16 at 14:20

1 Answers1

3

Like the error message shows you cannot login as that user (IIS/defaultapppool) on SQL server. Ususally this happens if:

  1. User does not exist
  2. User does not have the permissions to access the database

Go to your SQL server management studio and make or edit your user. right click the security folder to make a new user, and make sure he is databaseReader, -Editor and/or databaseAdmin

You can also specify another user in your connectionstring by adding: User ID=name;Password=password

counterflux
  • 363
  • 3
  • 12
  • New Error==========================================>>>>>> An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) – kianoush dortaj Mar 04 '16 at 15:16
  • Is it possible that you're now referring to an unexisting database? The error you got now just says: "whelp, I can't even find the database with your connectionstring" while the previous error still proved that he could find the database but not access it with your user. – counterflux Mar 07 '16 at 07:13
  • I Solve it ==>> i Create IISS User in Sql Server – kianoush dortaj Mar 09 '16 at 09:55
  • Well done. Like I said in my answer the IIS user defaultapppool did not exist in SQL server yet. Could you please mark it as accepted answer if you agree it indeed specified the cause and solution for the problem? So that other developers can quickly find answers if they stumble upon the same question? – counterflux Mar 09 '16 at 10:43
  • Dear counterflux Thanks . – kianoush dortaj Mar 11 '16 at 10:44