SqlException was unhandled by user code

Last post 08-27-2008 7:20 AM by bmwz9. 1 replies.

Sort Posts:

  • SqlException was unhandled by user code

    08-27-2008, 6:42 AM
    • Member
      175 point Member
    • shahzadkhan34
    • Member since 06-25-2008, 5:30 AM
    • Islamabad
    • Posts 76

    Hi,

    I have a web form which takes fname, lname, age and address from a user. User enters the values and clicks submit button. I want to insert the values entered by user into the database. But when i run the project. it gives the following error at conn.Open();

    SqlException was unhandled by user code:

    Cannot open database "EmployeeRecordSystem" requested by the login. The login failed.
    Login failed for user 'FAISALPC\ASPNET'.

    Below is my code of connecting to database and storing values into it.

    string fname = TextBox1.Text;

    string lname = TextBox2.Text;

    int age = int.Parse(TextBox3.Text);

    string address = TextBox4.Text;

    SqlConnection conn = new SqlConnection();conn.ConnectionString = @"Data Source=FAISALPC\SQLEXPRESS;Initial Catalog=EmployeeRecordSystem;Integrated Security=True";

    conn.Open();

    string query = "INSERT INTO Employee (Fname, Lname, Age, Address) VALUES (' " + fname + "', '" + lname + "', '"+ age + "', '" + address + "')";

    SqlCommand cmd = new SqlCommand(query, conn);

    cmd.ExecuteNonQuery();

    conn.Close();

  • Re: SqlException was unhandled by user code

    08-27-2008, 7:20 AM
    Answer
    • Participant
      1,262 point Participant
    • bmwz9
    • Member since 10-04-2007, 11:34 PM
    • Egypt
    • Posts 186

     You need to add the FAISALPC\ASPNET user to your sql server and grant db_access.

    There are three ways to do this.  Enterprise manager / Management Studio, TSQL, DMO.

    Use enterprise manager / Management Studio it is the simplest.

    1. Open SQL Enterprise Manager.
    2. Expand your SQL Server Node
    3. Expand the Security Node
    4. Right Click the Logins node. select new login
    5. Add the FAISALPC\ASPNET set the domain to FAISALPC, on the database access tab grant access to the required database.

    This will give the ASPNET account access to the database using the Public role.

    Mohamed Abb@s



    ------------------------

    Please remember to click “Mark as Answer” on the post(s) which helps you !
Page 1 of 1 (2 items)