我有一个带有自动生成列的infragistics网格如何从后面的代码中为它分配一个strored proc结果
<ig:WebScriptManager ID="WebScriptManager1" runat="server"></ig:WebScriptManager>
<ig:WebDataGrid ID="EntityGrid" runat="server" Width="100%">
<Behaviors>
<ig:Sorting>
</ig:Sorting>
</Behaviors>
</ig:WebDataGrid>
背后的代码是
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
EntityName.Text = DropDownList1.SelectedItem.Text;
string @RegardingObjectName = DropDownList1.SelectedItem.Text.Trim();
String strConnString = ConfigurationManager.ConnectionStrings["LiveLeaseConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "p_DataList_ByRegardingObject";
cmd.Parameters.AddWithValue("@RegardingObjectName", @RegardingObjectName);
cmd.Connection = con;
try
{
con.Open();
EntityGrid.DataSource = cmd.ExecuteReader();
EntityGrid.DataBind();
我需要将实体作为参数传递给存储过程。我怎么能在这里做到这一点?
我收到读者错误
答案 0 :(得分:1)
您需要向SelectParameters
<asp:ControlParameter Name="entityId"
ControlID="DropDownList1"
PropertyName="SelectedItem" Type="String" />
另请参阅此问题How to specify parameter value for stored procedure in SqlDataSource
编辑您可以使用此处显示的选择事件
Writing a SQLdatasource in code behind in C# to use a code behind value
您还可以将选择参数添加为
EntityGrid.SelectParameters.Add("entityId", DropDownList1.SelectedItem.Text);
只需将“entityId”重命名为要与SP一起使用的参数名称
Edit2 :
而不是
EntityGrid.DataSource = cmd.ExecuteReader();
EntityGrid.DataBind();
试试这个
SqlDataReader reader = cmd.ExecuteReader();
using (reader)
{
DataTable table = new DataTable();
table.Load(reader);
EntityGrid.DataSource = table;
}
请参阅此链接了解更多信息:
http://mentaljetsam.wordpress.com/2008/11/20/loading-an-sqldatareader-into-a-datagridview/