code query

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;
}
}
}
}
}
 
G

Guest

I would think that the PrimaryKey would have to be set to something that is
part of the table. The key column has not been added to the DataTable. Add it
and then assign the primary key.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top