我試圖爲我的數據庫中的表創建一個簡單的網格視圖。我試圖啓用gridview的刪除功能,但當我點擊刪除其中一行時,出現一個奇怪的錯誤。有誰知道我爲什麼會遇到這個問題?我通過服務器瀏覽器連接到我的Oracle數據庫,並且連接字符串是正確的。以下是我運行該頁面並單擊刪除鏈接時出現的錯誤屏幕截圖。提前致謝! 當試圖刪除一行時,我試圖打開連接時出現登錄失敗
的Web.config -
<configuration>
<connectionStrings>
<add name="ConnectionString3" connectionString="Data Source=obiwan;User ID=ac9555f;Password=*******"
providerName="System.Data.OracleClient" />
AvatarView源代碼 -
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AvatarView.aspx.cs" Inherits="DatabaseTest.AvatarView" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="Gridview1_RowDeleting" AllowSorting="True" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" DataKeyNames="AVATARID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="None" AllowPaging="True" Height="422px" Width="1020px">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:BoundField DataField="AVATARID" HeaderText="AVATARID" SortExpression="AVATARID" ReadOnly="True" />
<asp:BoundField DataField="AVATARNAME" HeaderText="AVATARNAME" SortExpression="AVATARNAME" />
<asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
<asp:BoundField DataField="STRENGTH_CURR" HeaderText="STRENGTH_CURR" SortExpression="STRENGTH_CURR" />
<asp:BoundField DataField="GENDER" HeaderText="GENDER" SortExpression="GENDER" />
<asp:BoundField DataField="HOARD" HeaderText="HOARD" SortExpression="HOARD" />
<asp:BoundField DataField="SPECIESID" HeaderText="SPECIESID" SortExpression="SPECIESID" />
<asp:BoundField DataField="USERID" HeaderText="USERID" SortExpression="USERID" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>" ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>" SelectCommand="SELECT * FROM "AVATAR""></asp:SqlDataSource>
</div>
</form>
</body>
</html>
AvatarView代碼背後 -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.OracleClient;
namespace DatabaseTest
{
public partial class AvatarView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string avatarID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string deleteSql = "DELETE FROM Avatar WHERE AvatarID = :AvatarID; ";
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString3"].ConnectionString))
using (var cmd = new OracleCommand(deleteSql, con))
{
cmd.Parameters.Add(":AvatarID", OracleType.VarChar).Value = avatarID;
con.Open();
int deleted = cmd.ExecuteNonQuery();
}
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
}
檢查您的用戶名或pasword –
@ErsinGülbahar它們是正確的.. – ACostea