Search code examples
c#asp.netheaderdatasetcell

how can I get a cell value from a header name in a dataset with one row


I have a table and I want to get specific column from my dataset my table only has one row.

Here is a image of what my table

Here is my code

private DataSet Ds = new DataSet();

public DataSet GetDataSet(string Query)
{
    try
    {
        using (MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connString))
        {
            MySqlDataAdapter Da = new MySqlDataAdapter(Query, conn);
            Da.Fill(Ds);
            conn.Close();
        }
    }
    catch (Exception) { }
    return Ds; //See image
}

public string getDataCellString(string headerName)
{
    return "";//I want to get cell from heder name
}

Here is my question: How can I get a cell value from a header name?


Solution

  • If you only have one row you can:

    public string getDataCellString(string headerName)
    {
        return Ds.Tables[0].Rows[0][headerName].ToString();
    }