using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(
"Data Source=PTZ1\\SQLEXPRESS;Initial Catalog = test; Integrated Security=SSPI;User ID=sa; Password=sa@; Trusted_Connection=True;");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from testing", conn);
adapter.SelectCommand = cmd;
adapter.Fill(ds, "First Table");
foreach (DataTable tables in ds.Tables)
{
ListBox1.Items.Add(tables.TableName);
}
conn.Close();
}
catch (Exception ex)
{
Label1.Text = "Error in execution " + ex.ToString();
}
}
}
I have the following program where I’m reading the values from the table and want to display the table values in a text field upon button click. Now when I click on the button, it just keeps on displaying first table in the listbox.
Can someone guide me through my error?
I suppose you need to display the Values existing in the
DataRowof aDataTable. In the code snippet below, columnName is referring to theColumnof thetesting Table, that you want to show in theListBox.OR