如何从存储过程中获取返回值以在GridView上显示?

时间:2017-09-22 16:01:31

标签: c# asp.net sql-server stored-procedures

我有一个存储过程Bundles的返回值,我希望它显示在Gridview中。这就是我所拥有的,但它没有显示在gridview中:

GetTeam

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

在此尝试以下解决方案,请在此解决方案中发送一个参数,以帮助我们将结果缩小到特定团队。

using (SqlConnection conn = new SqlConnection())
 {
            //Connection string
            conn.ConnectionString = ConnectionString.DataSourceString;

            //Create adapter and assign store procedure name
            SqlCommand cmmd = new SqlCommand()
            {
                CommandType = CommandType.StoredProcedure
            };

            // Assign to Command type
            //exception is caught because it cannot find Stored Procedure

            //Insert parameters into row from input text
            cmmd.CommandText = "GetTeam";
    // Send parameter            
cmmd.Parameters.AddWithValue("@PGetDayTeam",
 ShiftParameterTextBox.Text.Trim());

            cmmd.Connection = conn;
            try
            {
                conn.Open();
                //If we do not receive any records 
                GridView2.EmptyDataText = "No Records Found";
                GridView2.DataSource = cmmd.ExecuteReader();
                GridView2.DataBind();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                // Close and dispose connection
                conn.Close();
                conn.Dispose();
            }
 // select from grid
protected void GridView2_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{

GridViewRow row = GridView2.Rows[e.NewSelectedIndex];


        string GetTeam = row.Cells[2].Text;
       // You can use it in viewstate or what you choose.
        ViewState["GetTeam"] = GetTeam;

}

答案 1 :(得分:0)

  

然后,“填充”操作会将行添加到目标DataTable对象   在DataSet中,创建DataTable对象(如果尚未创建)   存在。创建DataTable对象时,通常会执行“填充”操作   仅创建列名元数据。但是,如果是MissingSchemaAction   property设置为AddWithKey,相应的主键和   也创造了约束。

你正在使用sql prosedure。所以你已经有了Colums的名字。您不需要使用DataTable。 Sql适配器也可以填充数据集。您需要先使用DataSet。

public static DataSet ExecuteDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
        {
            //create a command and prepare it for execution
            SqlCommand cmd = new SqlCommand();
            PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters);

            //create the DataAdapter & DataSet
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();

            //fill the DataSet using default values for DataTable names, etc.
            da.Fill(ds);

            //return the dataset
            return ds;
        }

然后你可以将你的Gridview绑定到那个。

grdView.DataSource = ds;
grdView.DataBind();