How to get specific column value from DataTable in C #

How to get specific column value from DataTable in C #

I have a problem with my C # code .

I need to read a specific column value from a DataTable .

I tried to use this solution without success, because the output is the name of the column selected in the query (File), and not the value of the database of fields.

I would really appreciate any help you can give me in solving this problem.

Here is my code:

public DataTable GridViewBind()
{
    using (OdbcConnection cn =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
    {    
        sql1 = " SELECT FILE FROM tbl_A WHERE Id = 1; ";


        using (OdbcDataAdapter command =
            new OdbcDataAdapter(sql1, cn))
        {
            try
            {
                cn.Open();

                dset = new DataSet();
                dset.Clear();
                command.Fill(dset);
                DataTable dt = dset.Tables[0];
                GridView1.DataSource = dt;                    
                GridView1.DataBind();

                Response.Write(dt.Columns[0].ToString());

                return dt;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("operation failed!", ex);
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }

                if (cn != null)
                {
                    cn.Close();
                    cn.Dispose();
                }
            }
        }
    }
}

Edit # 1

Now I have an error:

Correction System.IndexOutOfRangeException: at position 0 there is no line.

public DataTable GridViewBind()
{
    using (OdbcConnection cn =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
    {    
        sql1 = " SELECT FILE FROM tbl_A WHERE Id = 1; ";


        using (OdbcDataAdapter command =
            new OdbcDataAdapter(sql1, cn))
        {
            try
            {
                cn.Open();

                dset = new DataSet();
                dset.Clear();
                command.Fill(dset);
                DataTable dt = dset.Tables[0];
                GridView1.DataSource = dt;                    
                GridView1.DataBind();

                string file = dt.Rows[0].Field<string>(0);
                Response.Write(file.ToString());  

                return dt;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("operation failed!", ex);
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }

                if (cn != null)
                {
                    cn.Close();
                    cn.Dispose();
                }
            }
        }
    }
}
+4
source share
1 answer

. row.Field<string>(0) .

foreach(DataRow row in dt.Rows)
{
    string file = row.Field<string>("File");
}

:

foreach(DataRow row in dt.Rows)
{
    string file = row.Field<string>(0);
}

, DataRowCollection:

string file = dt.Rows[0].Field<string>(0); 

, , dt.Rows.Count, , :

if(dt.Rows.Count > 0)
    file = dt.Rows[0].Field<string>(0);
+8

All Articles