Programmatisk datalæsning
Hej,jeg har et gridview, hvor man kan select en bog, hvornæst info om forfatteren skal blive vist i et detailsview..
Det udkommenterede, '//GridView1.DataKeyNames = "BookID";', er det der giver problemer. Får fejlen "cannot implicitly convert type 'string' to 'string[]'" .. Men det er DataKeyNames jeg mangler at angive ikke? Og hvorn gør jeg så det :) ?
{
GridView1.AutoGenerateSelectButton = true;
string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("Select * from Book", myConnection);
SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
try
{
myConnection.Open();
adapter.Fill(ds, "Books");
}
catch (Exception err)
{
Response.Write(err);
}
finally
{
myConnection.Close();
}
GridView1.DataSource = ds.Tables["Books"];
//GridView1.DataKeyNames = "BookID";
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("Select * from Person where PersonID = @AuthorID", myConnection);
SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myCommand.Parameters.Add("@AuthorID", SqlDbType.Int);
myCommand.Parameters["@AuthorID"].Value = GridView1.SelectedValue;
try
{
myConnection.Open();
adapter.Fill(ds, "Authors");
}
catch (Exception err)
{
Response.Write(err);
}
finally
{
myConnection.Close();
}
DetailsView1.DataSource = ds.Tables["Authors"];
DetailsView1.DataBind();
}