A
Adrian
Q: In the code below: how do I give the key a value?
new_data["key"] = whatever; gives the error column not found.
Many thanks,
Adrian.
private void Form1_Load(object sender, System.EventArgs e)
{
string connect = "server = fred\\sqlexpress; database = TrialDB; Integrated
Security = TRUE;";
using (SqlConnection my_connection = new SqlConnection(connect))
{
string query = "SELECT Column_one, Column_two, Column_three FROM
dbo.Table_1";
using(SqlDataAdapter my_adapter = new SqlDataAdapter(query,
my_connection))
{
using(DataSet data_set = new DataSet())
{
my_adapter.Fill(data_set);
DataTable my_table = data_set.Tables[0];
my_table.PrimaryKey = new DataColumn[]{my_table.Columns["key"]};
DataRow new_data = my_table.NewRow();
new_data[0]=3;
new_data[1]=4;
new_data[2]="new entry";
my_table.Rows.Add(new_data);
string display_string = string.Empty;
foreach(DataRow row in my_table.Rows)
{
for(int x = 0; x < 3; x++)
{
display_string += '\t' + (row.ItemArray[x].ToString());
}
listBox1.Items.Add(display_string);
display_string = string.Empty;
}
}
}
}
}
new_data["key"] = whatever; gives the error column not found.
Many thanks,
Adrian.
private void Form1_Load(object sender, System.EventArgs e)
{
string connect = "server = fred\\sqlexpress; database = TrialDB; Integrated
Security = TRUE;";
using (SqlConnection my_connection = new SqlConnection(connect))
{
string query = "SELECT Column_one, Column_two, Column_three FROM
dbo.Table_1";
using(SqlDataAdapter my_adapter = new SqlDataAdapter(query,
my_connection))
{
using(DataSet data_set = new DataSet())
{
my_adapter.Fill(data_set);
DataTable my_table = data_set.Tables[0];
my_table.PrimaryKey = new DataColumn[]{my_table.Columns["key"]};
DataRow new_data = my_table.NewRow();
new_data[0]=3;
new_data[1]=4;
new_data[2]="new entry";
my_table.Rows.Add(new_data);
string display_string = string.Empty;
foreach(DataRow row in my_table.Rows)
{
for(int x = 0; x < 3; x++)
{
display_string += '\t' + (row.ItemArray[x].ToString());
}
listBox1.Items.Add(display_string);
display_string = string.Empty;
}
}
}
}
}