我试图为我的数据库中的表创建一个简单的gridview。我正在尝试启用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();
}
}
}
答案 0 :(得分:2)
您正在使用SQL Server特定的类连接到Oracle服务器。使用Oracle OracleConnection
或更通用的方法IDbConnection
使用Oracle clases的示例:
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString3"].ConnectionString))