2012-02-11 114 views
0

我知道我很明显缺少/不正确地声明了一个变量,但我不太确定如何解决这个问题。当我单击“btnUpdate”更新数据库中的信息时(基于文本字段中的内容),我收到此错误。我已经发布了下面的代码进行审查,感谢您的帮助!ASP.NET错误:必须声明标量变量

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"  Inherits="WebApplication1.WebForm1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 

<div class="floater"> 
<h3>Books</h3> 
<asp:DropDownList 
    id="DropDownList_Books" 
    DataSourceId="srcBooks" 
    DataTextField="Title" 
    DataValueField="Id" 
    Runat="server" /> 
    <asp:Button ID="btnSelect" runat="server" Text="View Detials" Width="106px" 
     onclick="btnSelect_Click" /> 
</div> 

<asp:GridView 
    id="grdBooks" 
    DataSourceID="srcBooks_Description" 
    Runat="server" Visible="False" /> 

    <asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" /> 

</div> 

<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click" 
    style="z-index: 1; left: 464px; top: 153px; position: absolute; height: 26px" 
    Text="Cancel" Visible="False" /> 

<asp:FormView 
    id="frmEditBook" 
    DataKeyNames="Cat_Id" 
    DataSourceId="srcBooks_Description" 
    DefaultMode="Edit" 
    Runat="server" Visible="False" 
    style="z-index: 1; left: 391px; top: 87px; position: absolute; height: 111px; width: 206px"> 
    <EditItemTemplate> 
    <asp:Label 
     id="lblTitle" 
     Text="Title:" 
     AssociatedControlID="txtTitle" 
     Runat="server" /> 
    <asp:TextBox 
     id="txtTitle" 
     Text='<%#Bind("Title")%>' 
     Runat="server" /> 
    <br /> 
    <asp:Label 
     id="lblDescription" 
     Text="Description:" 
     AssociatedControlID="txtDescription" 
     Runat="server" /> 
    <asp:TextBox 
     id="txtDescription" 
     Text='<%#Bind("Description")%>' 
     Runat="server" /> 
    <br /> 
    <asp:Button 
     id="btnUpdate" 
     Text="Update" 
     CommandName="Update" 
     Runat="server" /> 
    </EditItemTemplate> 
</asp:FormView> 
<asp:SqlDataSource ID="srcBooks" runat="server" 
    ConnectionString="Data Source=&quot;ebiz.isy.vcu.edu, 14330&quot;;Initial Catalog=spring12u08;Persist Security Info=True;User ID=Spring12u08;Password=INFO451u08" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT Title,Id FROM PARTIN_ID"> 

</asp:SqlDataSource> 

<asp:SqlDataSource ID="srcBooks_Description" runat="server" 
    ConnectionString="Data Source=&quot;ebiz.isy.vcu.edu, 14330&quot;;Initial Catalog=spring12u08;Persist Security Info=True;User ID=Spring12u08;Password=INFO451u08" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM PARTIN_INFO WHERE [email protected]" UpdateCommand="UPDATE PARTIN_INFO SET [email protected], 
     [email protected] WHERE [email protected]"> 
<SelectParameters> 
     <asp:ControlParameter 
      Name="Id" 
      Type="int32"     
      ControlID="DropDownList_Books" /> 
    </SelectParameters> 

</asp:SqlDataSource> 

</form> 
</body> 
</html> 

带有新错误的更新后的代码:无法在ControlParameter'标题'中找到控件'txtTitle'。

<form id="form1" runat="server"> 
<div> 

<div class="floater"> 
<h3>Books</h3> 
<asp:DropDownList 
    id="DropDownList_Books" 
    DataSourceId="srcBooks" 
    DataTextField="Title" 
    DataValueField="Id" 
    Runat="server" /> 
    <asp:Button ID="btnSelect" runat="server" Text="View Detials" Width="106px" 
     onclick="btnSelect_Click" /> 
</div> 

<asp:GridView 
    id="grdBooks" 
    DataSourceID="srcBooks_Description" 
    Runat="server" Visible="False" /> 

    <asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" /> 

</div> 

<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click" 
    style="z-index: 1; left: 464px; top: 153px; position: absolute; height: 26px" 
    Text="Cancel" Visible="False" /> 

<asp:FormView 
    id="frmEditBook" 
    DataKeyNames="Cat_Id" 
    DataSourceId="srcBooks_Description" 
    DefaultMode="Edit" 
    Runat="server" Visible="False" 

    style="z-index: 1; left: 391px; top: 87px; position: absolute; height: 111px; width: 206px" > 
    <EditItemTemplate> 
    <asp:Label 
     id="lblTitle" 
     Text="Title:" 
     AssociatedControlID="txtTitle" 
     Runat="server" /> 
    <asp:TextBox 
     id="txtTitle" 
     Text='<%#Bind("Title")%>' 
     Runat="server" /> 
    <br /> 
    <asp:Label 
     id="lblDescription" 
     Text="Description:" 
     AssociatedControlID="txtDescription" 
     Runat="server" /> 
    <asp:TextBox 
     id="txtDescription" 
     Text='<%#Bind("Description")%>' 
     Runat="server" /> 
    <br /> 
    <asp:Button 
     id="btnUpdate" 
     Text="Update" 
     CommandName="Update" 
     Runat="server" /> 
    </EditItemTemplate> 
</asp:FormView> 
<asp:SqlDataSource ID="srcBooks" runat="server" 
    ConnectionString="Data Source=&quot;ebiz.isy.vcu.edu, 14330&quot;;Initial Catalog=spring12u08;Persist Security Info=True;User ID=Spring12u08;Password=INFO451u08" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT Title,Id FROM PARTIN_ID"> 

</asp:SqlDataSource> 

<asp:SqlDataSource ID="srcBooks_Description" runat="server" 
    ConnectionString="Data Source=&quot;ebiz.isy.vcu.edu, 14330&quot;;Initial Catalog=spring12u08;Persist Security Info=True;User ID=Spring12u08;Password=INFO451u08" 
    onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM PARTIN_INFO WHERE [email protected]" UpdateCommand="UPDATE PARTIN_INFO SET [email protected], 
     [email protected] WHERE [email protected]"> 
<SelectParameters> 
     <asp:ControlParameter 
      Name="Id" 
      Type="int32"     
      ControlID="DropDownList_Books" 
      PropertyName="SelectedValue" /> 
    </SelectParameters> 
    <UpdateParameters> 
      <asp:ControlParameter Name="Title" ControlId="txtTitle" PropertyName="Text"/> 
      <asp:ControlParameter Name="Description" ControlId="txtDescription" PropertyName="Text"/> 
      <asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/> 
     </UpdateParameters> 

</asp:SqlDataSource> 

</form> 

而且后面的代码:

namespace WebApplication1 
{ 
public partial class WebForm1 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void srcBooks_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
    { 

    } 

    protected void btnSelect_Click(object sender, EventArgs e) 
    { 
     grdBooks.Visible = true; 
    } 

    protected void btnCancel_Click(object sender, EventArgs e) 
    { 
     frmEditBook.Visible = false; 
    } 

    protected void btnEdit_Click(object sender, EventArgs e) 
    { 
     frmEditBook.Visible = true; 
     btnCancel.Visible = true; 
    } 

    protected void btnUpdate_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      srcBooks_Description.Update(); 
     } 
     catch (Exception except) 
     { 
      // Handle the Exception. 
     } 
    } 






    } 
} 
+0

请提供您的代码。 – 2012-02-11 21:45:53

+0

asp:ControlParameter也需要指定'PropertyName =“Value”'。 – 2012-02-11 22:39:42

+0

'srcBooks_Description'也需要'UpdateParameters'。 – 2012-02-11 22:41:43

回答

0

我有这个问题。我将原始表中的所有字段名称更改为一个字或多个由下划线分隔的单词,如card_number,更改了select和update语句,并且它工作正常。