首页 > 解决方案 > 任务不包含“行”的定义

问题描述

我有这段代码可以从数据库中获取数据

public async Task<DataTable> SelectData(string stored_procedure, SqlParameter[] param)
    {
        SqlCommand sqlcmd = new SqlCommand();
        sqlcmd.CommandType = CommandType.StoredProcedure;
        sqlcmd.CommandText = stored_procedure;
        sqlcmd.Connection = sqlconnection;
        if (param != null)
        {
            sqlcmd.Parameters.AddRange(param);
        }

        SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
        DataTable dt = new DataTable();
        await Task.Run(()=> da.Fill(dt));
        return dt;
    }

我使用这段代码来运行存储过程

public async Task<DataTable> GetOrderManagementManagerEmail()
    {
        DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
        DataTable dt = new DataTable();
        dt =await DAL.SelectData("GetOrderManagementManagerEmail", null);
        DAL.Close();
        return dt;
    }

然后我单击按钮使用此代码

private async void btnValidate_Click(object sender, EventArgs e)
    {
        int[] selectedRows = gridView2.GetSelectedRows();
        for (int i = 0; i < selectedRows.Length; i++)
        {
            DataRow rowGridView2 = (gridView2.GetRow(selectedRows[i]) as DataRowView).Row;
          await  stock.ValidateProjectNeed(Convert.ToInt32(rowGridView2["id"]), DateTime.Now);
        }
        if (XtraMessageBox.Show(Resources.addedSuccessfullyBonBesoinAndSendEmail, Resources.Validate, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
        {


            Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            Recipients oRecips = oMsg.Recipients;

            oMsg.To =await Task.Run(()=> stock.GetOrderManagementManagerEmail().Rows[0][0].ToString());
            oMsg.Subject = "Bon Besoin " ;
            oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
            oMsg.Display(false); //In order to display it in modal inspector change the argument to true
            oMsg.HTMLBody = "Un nouveau bon besoin a été ajouté " +
                "<br />" +  oMsg.HTMLBody; //Here comes your body;

        }
        gridControl2.DataSource = stock.GetProjectNeedsForValidate();
    }

但我在这行代码中遇到错误

oMsg.To =await Task.Run(()=> stock.GetOrderManagementManagerEmail().Rows[0][0].ToString());

任务不包含“行”的定义,并且找不到接受“任务”类型的第一个参数的可访问扩展方法“行”(您是否缺少 using 指令或程序集引用?)。在我使用异步之前,代码工作正常。提前致谢。

标签: c#winforms

解决方案


你要:

var table = await stock.GetOrderManagementManagerEmail();
oMsg.To = table.Rows[0][0].ToString();

不知道发生了什么await Task.Run()。你从 to 的调用中得到一个Task返回,GetOrderManagementManagerEmail()然后尝试从一个任务中检索行,包裹在一个....

简化它。另外,看看您是否真的需要使用带有索引器的表/行。您可以使用:

var oMsg.To = table.AsEnumerable().Select(d => d.Field<string>("To")).FirstOrDefault();

推荐阅读