从存储过程填充datgrid

时间:2017-08-07 13:47:32

标签: c# asp.net stored-procedures

example.com www.example.com

我正在使用上面的代码根据用户从Sql Server数据库中的下拉列表和存储过程中的选择来填充ASP.NET中的数据网格。它似乎运行正常,因为我在函数中放置了一个断点,它没有错误地遍历每一行,但它没有显示数据网格,而存储过程正在返回数据。任何帮助,将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

您是否考虑过将代码包装在返回DataTable的方法中?

例如:

    public DataTable Get_Stored_Procedure_Data()
    {
        DataTable Titles = new DataTable();

        SqlConnection connection = new SqlConnection(AssessmentData.GetConnection());
        try
        {
            connection.Open();
            SqlCommand sqlGetTitles = new SqlCommand("[HRO_AAT].[dbo].[GetPositionLibrary]", connection);
            sqlGetTitles.CommandType = System.Data.CommandType.StoredProcedure;
            SqlDataAdapter sqlTitles = new SqlDataAdapter(sqlGetTitles);
            sqlTitles.Fill(Titles);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            connection.Close();
        }

        return Titles;
    }

然后:

     DataTable results = Get_Stored_Procedure_Data();
     gvPositions.DataSource = Titles.DefaultView;

另外,我添加了一个尝试,捕获,最后作为良好的做法。

相关问题