2017-04-03 41 views
-1

我試圖爲我的數據庫中的表創建一個簡單的網格視圖。我試圖啓用gridview的刪除功能,但當我點擊刪除其中一行時,出現一個奇怪的錯誤。有誰知道我爲什麼會遇到這個問題?我通過服務器瀏覽器連接到我的Oracle數據庫,並且連接字符串是正確的。以下是我運行該頁面並單擊刪除鏈接時出現的錯誤屏幕截圖。提前致謝! Login error當試圖刪除一行時,我試圖打開連接時出現登錄失敗

的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 &quot;AVATAR&quot;"></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"].ConnectionStr‌​ing)) 
     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

檢查您的用戶名或pasword –

+0

@ErsinGülbahar它們是正確的.. – ACostea

回答

2

您正在使用SQL Server特定的類來連接到Oracle服務器。使用Oracle的人OracleConnection或更通用的方法IDbConnection

示例使用Oracle clases:

using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString3"].ConnectionStr‌​ing)) 
+0

感謝布拉德伯裏,確切位置在哪裏我會把「的OracleConnection 「? – ACostea

+0

@ACostea編輯添加一個示例。 – bradbury9

+0

謝謝,但是當我嘗試添加命名空間'using.system.data.oracleclient'時,什麼也沒有顯示出來。沒有使用正確的名稱空間,oracleconnection線顯示錯誤? – ACostea

相關問題