2

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

    }

3 Answers3

1

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.

Adil
  • 146,340
  • 25
  • 209
  • 204
  • 1
    where the line will be printed.. I have not redirected the page after button click... can I see the value in the same page after button click?? –  Sep 15 '15 at 06:30
  • You can use Visual Studio output window, http://stackoverflow.com/questions/1159755/where-does-system-diagnostics-debug-write-output-appear – Adil Sep 15 '15 at 06:38
1

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

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

ASh
  • 34,632
  • 9
  • 60
  • 82