M.Bouabdallah
M.Bouabdallah

Reputation: 772

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?). before I use async the code work fine.Thanks in advance.

Upvotes: 0

Views: 1242

Answers (1)

reckface
reckface

Reputation: 5858

You want:

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

Not sure what's happening with the 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....

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

Upvotes: 7

Related Questions