0

I was trying to connect database to my ASP.NET website and encountered this error:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Cannot open database "airplane_reservation" requested by the login. The login failed.

Login failed for user 'PELim\PeiErn'.

I create the database in SQL Management Studio 2014. I am using Visual Studio Coommunity 2015, this is my C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class CompleteOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Random rnd = new Random();
        int ordernum = rnd.Next(3, 10000);
        string Name = Request.QueryString["name"];
        string IC = Request.QueryString["ic"];
        string Contact = Request.QueryString["contact"];
        string Date = Request.QueryString["date"];
        string Seats = (string) (Session["seats"]);

        Label7.Text = Convert.ToString(ordernum);
        Label4.Text = Name;
        Label8.Text = IC;
        Label10.Text = Contact;
        Label12.Text = Date;
        Label14.Text = Seats;

        SqlConnection con = new SqlConnection(@"Data Source=PELim;Initial Catalog=airplane_reservation;Integrated Security=True;Pooling=False");
        con.Open();
        SqlCommand cmd =new SqlCommand("INSERT INTO Airplane_Reservation(Order_Number,Name,Identification_Number, Contact_Number, Departure_Date, Seats)VALUES('" + ordernum + "','" + Name + "','" + IC + "','" + Contact + "', '" + Date + "', '" + Seats + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Main.aspx");
    }
}

Please help me, thank you.

Silver Archer
  • 167
  • 3
  • 3
  • 14

2 Answers2

0

It looks like you did not give the correct permissions to the user.

If you use the username/pass PELim\PeiErn as the login credentials in the SQL Management Studio 2014, can you access the database airplane_reservation?

See this other question it seems to discuss a similiar problem.

Community
  • 1
  • 1
berserck
  • 499
  • 4
  • 19
  • I already check the user mapping for the airline reservation database in PELim\PeiErn user, it shows db_owner and public. Also I'm using Windows Authentication – Silver Archer Nov 05 '16 at 11:53
0

From your above ConnectionString i noticed

  1. Your Data Source: i think it should be PELim-PC or use "." (period) for the default server
  2. Use sql server authentication and add password="your server pwd"
Prince Tegaton
  • 222
  • 1
  • 4
  • 14