【发布时间】:2011-10-06 13:14:25
【问题描述】:
我正在使用 ASP.NET 和 StoredProcedure。我更改了存储过程,所以现在它返回 10 个字段而不是 9 个。我在 asp.net 中添加了相应的文本框,它获得了第 10 个字段。
<asp:TextBox ID="ID" runat="server" Text='<%# Eval("ID") %>' Width="80px" ReadOnly="true"></asp:TextBox>
但它失败了,说
DataBinding:“System.Data.DataRowView”不包含属性 名称为“ID”。
我在 SQL Management Studio 中运行存储过程,它确实没有返回新修改的字段。我停止并启动 SQL 引擎,现在它确实吐出了新值,但在我的 ASP.net 应用程序中,它仍然无法识别新值。解决此问题的最佳方法是什么?我以前也遇到过。
添加调用SP的代码:
SqlConnection aConn = new
SqlConnection(ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString);
SqlCommand aCommand = new SqlCommand("GetCustomer", aConn);
aCommand.CommandType = CommandType.StoredProcedure;
aCommand.Parameters.Add("@ID", SqlDbType.Int).Value = aID;
SqlDataAdapter adapter = new SqlDataAdapter(aCommand);
DataSet aCustomerDS = new DataSet();
adapter.Fill(aCustomerDS);
我的存储过程是(这里没有什么奇怪的):
CREATE PROCEDURE [dbo].[GetCustomer]
@ID int
AS
BEGIN
SET NOCOUNT ON;
IF @ID = 0
BEGIN
SELECT ID,
[this field] as thisfield,
[that field] as thatfield,
[new field] as newfield
FROM MyDB.dbo.customers
ORDER BY ID DESC
END
ELSE
BEGIN
SELECT ID,
[this field] as thisfield,
[that field] as thatfield,
[new field] as newfield
FROM MyDB.dbo.Customers
WHERE ID = @ID
ORDER BY ID DESC
END
END
FormView用于绑定数据到sdsCustomer
<asp:FormView ID="fvCustomer" runat="server" AllowPaging="True" DataSourceID="sdsCustomer"
DataKeyNames="ID" OnPreRender="OnfvCustomer_PreRender">
【问题讨论】:
-
你能发布存储过程(至少选择部分)和数据源定义吗?
-
很好的补充,但仅仅 conn 是不够的。需要用于返回数据的 SQL 以及您可能绑定到 aCustomerDS 的表单数据源。
-
用 SP 更新,但没有什么奇怪的。
-
仍然看不到您绑定到控件的数据源定义。
-
添加了那部分(到最后)。第一段代码就在这个表单视图中。
标签: asp.net debugging stored-procedures sqldatasource