简体   繁体   English

任务 <DataTable> 不包含“行”的定义

[英]Task<DataTable> does not contain definition for 'Rows'

I have this code that get data from database 我有这段代码从数据库中获取数据

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;
    }

and I use this code to run stored procedure 我用这段代码运行存储过程

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;
    }

then I use this code on a click of button 然后我在单击按钮时使用此代码

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();
    }

but I get error in this line of code 但是我在这行代码中遇到错误

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

Task does not contain definition for 'Rows' and no accessible extension method 'Rows' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly refrence?). Task不包含“行”的定义,并且找不到包含可接受的“任务”类型的第一个参数的可访问扩展方法“行”(您是否缺少using指令或程序集引用?)。 before I use async the code work fine.Thanks in advance. 在使用异步之前,代码可以正常工作。谢谢。

You want: 你要:

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

Not sure what's happening with the await Task.Run() . 不知道await Task.Run()发生了什么。 You are getting a Task back from the call to to GetOrderManagementManagerEmail() and then attempting to retrieve rows from a task, wrapped in a.... 您正在从对GetOrderManagementManagerEmail()的调用中获取Task ,然后尝试从任务中检索行,并包装在一个行中。

Simplify it. 简化它。 Also, take a look at whether you really need to use tables/rows with indexers. 另外,看看是否真的需要将表/行与索引器一起使用。 You can use: 您可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM