user5286952
user5286952

Reputation:

how to check the value present in a datatable

I have populated the value for a datatable by using the value in a database. The data willl get populated into database on a button click. Can anybody tell how to check the value in the datatable which is populated. I am using visual studio and coding in c#

The code for populating the datatable is as shown below :

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            conn.Open();
            var sql = @"select scriptname,accnum,Quantity,price from transac where transactio = 'Sell'";
            var dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sql, conn);
            var dataTablesell = new DataTable();
            dataAdapter.Fill(dataTablesell);


        }
        catch (System.Data.SqlClient.SqlException sqlEx)
        {
            Response.Write("error" + sqlEx.ToString());
        }
        catch (Exception ex)
        {
            Response.Write("error" + ex.ToString());
        }

    }

Upvotes: 2

Views: 158

Answers (3)

ASh
ASh

Reputation: 35646

DataTable has a very nice DebuggerVisualizer. Set a breakpoint after dataAdapter.Fill(dataTablesell); line; when the breakpoint is hit, hover cursor over dataTablesell to open a debug view and click magnifier button (O-) to open visualizer

Upvotes: 1

user5286952
user5286952

Reputation:

got the answer... Instead of using Console.WriteLine as Adil has said, I used Response.Write, The code is

foreach (DataRow row in dataTablesell.Rows)
            {
                Response.Write(row["scriptname"].ToString()); 
                Response.Write(row["accnum"].ToString()); 
            }

Upvotes: 1

Adil
Adil

Reputation: 148110

You can iterate through rows of DataTable to get the value in each row for given columns.

foreach(DataRow row in dataTablesell.Rows)
{
    Debug.WriteLine(row["scriptname"].ToString()); //use cell value where you want
    Debug.WriteLine(row["accnum"].ToString()); //use cell value where you want
}

You can also bind the DataTable to DataGridView to see the rows in the DataTable. This MSDN article How to: Bind Data to the Windows Forms DataGridView Control explains how you would do that.

Upvotes: 1

Related Questions