I have currently two forms in mine website..I m also posting their codes--

FrmRegistration.aspx

public partial class FrmRegistration : System.Web.UI.Page
{
   
    SqlConnection conn = new SqlConnection("Data Source=SONIA-B408A4159\\SQLEXPRESS;Initial catalog=sonia;Integrated Security=true");
    string query;
    SqlCommand cmd;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSubmit.Attributes.Add("onclick", "Javascript: return Validations() ");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        query = "Insert into UserInfo values(@name,@email)";
        cmd = new SqlCommand(query, conn);
        conn.Open();
        cmd.Parameters.AddWithValue("@name", txtName.Text);
        cmd.Parameters.AddWithValue("@email", txtEmail.Text);
        cmd.ExecuteNonQuery();
        conn.Close();
        Session["Name"] = txtName.Text; 
        Response.Redirect("FrmUserDetails.aspx");
      
    }
}

FRMUSERDERATILS-

public partial class FrmUserDetails : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        lblUserName.Text = "SONIA";
        //lblUserName.Text = Session["Name"].ToString ();
                
    }
}

I publish the website using the foll. steps -

1. go to your root directory (in which your IIS is installed....assuming that you are using winxp sp2 and if you installed ur OS in "C" drive then usually your IIS's root folder does exist in -->"C:\Inetpub\WWWRoot")
2. Create a new folder there with any name you wish...e.g. TESTSITE for an example
3. Come back to VS2005 IDE....open your website
4. Goto "Build" menu and select "Publish Website" option.
5. Now click on ellipse(...) button and click to select "Local IIS" from the left panel of the dialog box that appears
6. Expand the "Default Web Site" node...there you can see the folder("TESTSITE") that you just created...Select the folder and click Open (if you see any message click on YES to proceed)
7. Make sure that the option "Allow this precompiled site to be updateable" is checked
8. Now click OK....publishment of your site will begin...notice the process in the outout window
9. If all goes well then you will see a message "Publish succeeded"....this means that you have successfully passed the hardle from VS2005

I type - http://localhost/testsite/FrmRegistration.aspx
Form is successfully opened ...
Suppose I enter sonia in txt name & [email address] in txtEmail & when i press submit - Error is dere.


Server Error in '/TESTSITE' Application.
--------------------------------------------------------------------------------

Cannot open database "sonia" requested by the login. The login failed.
Login failed for user 'SONIA-B408A4159\ASPNET'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Cannot open database "sonia" requested by the login. The login failed.
Login failed for user 'SONIA-B408A4159\ASPNET'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

When i run my website.by pressing F5,everything is working fine,Can somebody tell me y the error is dere??

Dani AI

Generated

Quick diagnosis: the error "Login failed for user 'SONIA-B408A4159\ASPNET'" means IIS is trying to connect with a Windows account (the machine's ASP.NET/IIS process account) and that account has no permission on the database. That explains why F5 in Visual Studio worked (the app ran as your user) but the published site under IIS failed. pointed to the same root cause; 's suggestion to move the connection string into web.config is a good next step for manageability.

Practical fixes (pick one):

  • Use SQL authentication (simpler for many deployments). Enable "SQL Server and Windows Authentication mode" on the server, create a SQL login and map it to the sonia database, then use a connection string that supplies User ID and Password and does NOT use Integrated Security. Example pattern:
Data Source=YourServer\InstanceName;Initial Catalog=sonia;User ID=yourSqlUser;Password=yourPassword;

Restart SQL service if you change authentication mode.

  • Or use Windows authentication: give the IIS process account access to the database. On XP IIS this is the machine account MACHINE_NAME\ASPNET; on newer IIS it may be NETWORK SERVICE or the application-pool identity. Create a SQL login for that Windows account and map it to the database, for example:
CREATE LOGIN [MACHINE_NAME\ASPNET] FROM WINDOWS;
USE sonia;
CREATE USER [MACHINE_NAME\ASPNET] FOR LOGIN [MACHINE_NAME\ASPNET];
EXEC sp_addrolemember 'db_datawriter', 'MACHINE_NAME\ASPNET';

Other checks and tips: confirm the correct SQL instance name (SQLEXPRESS vs default) and point Data Source accordingly; test the exact connection from the server using Management Studio or a UDL file; put the final connection string in web.config and read it with ConfigurationManager so you can swap auth modes without changing code (as suggested). Do not leave Integrated Security=true in a string that also supplies User ID/Password — that forces Windows auth and will cause the same login failure. Uninstalling SQL Server 2005 is usually unnecessary; the problem is the account/auth mismatch, not leftover files. Finally, always specify column names in INSERT statements and wrap DB calls in try/catch to capture the real error if something else goes wrong.

Recommended Answers

All 4 Replies

Because on Windows Server IIS is run by an unprivelaged user account. When you're running it in the VS IDE you're running as your local user. You should use SQL Authentication and create a user on the SQL Server to log in the ASPNET process.

-or-

You can use identity impersonation in your web.config file:

<?xml version="1.0"?>

<configuration>

	<appSettings>
		<add key="CalcDelta" value ="Y"/>
		<add key="VerboseLogging" value="Y"/>
	</appSettings>

	<connectionStrings />
  
	<system.web>
		<identity impersonate="true" userName="administrator" password="@@@@@@@@@@@@" />
		<compilation debug="true" defaultLanguage="c#" />
		<authentication mode="None" />
		<sessionState 
				timeout="1" 
				mode="InProc"
				sqlCommandTimeout="15"
		/>
		<httpRuntime
				executionTimeout="45"
				enable="true"
		/>
		
	</system.web>

	
</configuration>

hi there....cam back after long time...still banned..

one question....why don't you put u r connection string in the web.config file..

one last question...do u have u r database in the appdata folder..

or you creating from sqlserver ...then connecting..

I have earlier SQL 2005 Express Edition install..I remove it from control panel.But its not completely remove,can somebody tell me how to completely remove it. Cz now I m using SQL Server 2000 ,even if i craete the user & connect to DB using SQL server authentication,I m still getting the same error..My frnd told me to completely uinstall SQL 2005 might solve this errror!!!!!

Can somebody tell y i m getting the same error even if I connect using SQL authentication???? Is the reason same told by mine frnd!!!

I have earlier SQL 2005 Express Edition install..I remove it from control panel.But its not completely remove,can somebody tell me how to completely remove it. Cz now I m using SQL Server 2000 ,even if i craete the user & connect to DB using SQL server authentication,I m still getting the same error..My frnd told me to completely uinstall SQL 2005 might solve this errror!!!!!

Can somebody tell y i m getting the same error even if I connect using SQL authentication???? Is the reason same told by mine frnd!!!

Tell me one thing more ,Suppose while creating a user , Suppose i give username A & default databse is sonia..& there is also other user named B whose default DB is sardana.

SO can i use the username A to connect to DB sardana or only username A can be used to connect to DB sardana .

Mine update Code,Still getting the same Error

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; 

public partial class FrmRegistration : System.Web.UI.Page
{

    SqlConnection conn = new SqlConnection("Data Source=(local);Initial catalog=sonia;User ID=sonia;Password=sonia;Integrated Security=true");
    string query;
    SqlCommand cmd;

    //http://www.daniweb.com/forums/thread213382.html
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSubmit.Attributes.Add("onclick", "Javascript: return Validations() ");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        query = "Insert into UserInfo values(@name,@email)";
        cmd = new SqlCommand(query, conn);
        conn.Open();
        cmd.Parameters.AddWithValue("@name", txtName.Text);
        cmd.Parameters.AddWithValue("@email", txtEmail.Text);
        cmd.ExecuteNonQuery();
        conn.Close();
        Session["Name"] = txtName.Text;
        Session ["Email"]=txtEmail .Text ; 
        Response.Redirect("FrmUserDetails.aspx");
      
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.