简体   繁体   中英

Getting Error while read the data from table

I am displaying all the data in a gridview from database table. I have attached the database file in app folder. But while I am running the application I'm getting error like

Cannot open database requested in login 'Employee'. Login fails. Login failed for user 'HOME-47F64BE31D\\Administrator'

Here is all my code:

aspx page:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
    <asp:BoundField DataField="LastName" HeaderText="LastName" />
    <asp:BoundField DataField="HireDate" HeaderText="HireDate" />      
    </Columns>        
</asp:GridView>    
<asp:label ID="lblStatus" runat="server"></asp:label>

aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        GetData();
}

private void GetData()
{
    string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlDataAdapter da = new SqlDataAdapter("GetEmp", str);
    da.SelectCommand.CommandType = CommandType.StoredProcedure;
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

Please help me. What is the error?

Your error is exactly what it's looks like: a database access error. That HOME-47F64BE31D\\Administrator user doesn't have access to your database. You have two choices:

  • To grant access to that user into your database. You can define that permissions through your SQL Management Studio.
  • To inform a user with permission into your querystring; edit your configuration file to add a User ID=aaa;Password=bbb with a user with permissions

As side note, you shouldn't to use a sa user into your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM