2014年7月13日 星期日

[RESOLVED] Error when I select a value from GridView to DetailsView.


Hi all, I want to use the value of a Gridview to show the details of the particular line in the database in a DetailsView. It is based on two IDs (TutID and SubTutID).


This are the following codes that I am using. However, I am getting this error. 


Ambiguous column name 'TutID'.


These are my following codes :


protected void gvChapters_SelectedIndexChanged(object sender, EventArgs e)

{

dvChapters.Visible = true;


SqlConnection conn = new SqlConnection(connStr);


SqlCommand chapters = new SqlCommand("Select t.TutID, s.SubTutID from [Tutorial] t, [SubTutorial] s where TutID = '" + gvChapters.SelectedDataKey.Value.ToString() + "' and s.SubTutID = '" + gvChapters.SelectedDataKey.Value.ToString() + "'", conn);

conn.Open();

SqlDataReader readerDetails;

readerDetails = chapters.ExecuteReader();


if (readerDetails.Read())

{

//BookID = gvConsultations.SelectedValue.ToString();

//BindDetailsView(BookID);

dvChapters.DataSource = readerDetails;

BindDetailsView(TutID);



//dvConsultations.BindDetailsView;

}

readerDetails.Close();

conn.Close();


}



How do i solve it?



Please help! Thanks!







Do like this : gvChapters.DataKeys (gvChapters.SelectedIndex).Values (0).ToString() and gvChapters.DataKeys (gvChapters.SelectedIndex).Values (1).ToString()

Erm pardon me but where do I put these 2 lines of codes? 



To replace this code : gvChapters.SelectedDataKey. Value.ToString(). Apologize, seem the problem because you have same TutID fieldname in both tables. Try specify the table name before TutID, like : WHERE s.TutID=...

Hi,


Your Sql Query needs some modification at the where clause, (where t.TutID)


Select t.TutID, s.SubTutID from [Tutorial] t, [SubTutorial] s where t.TutID = '" + gvChapters.SelectedDataKey.Value.ToString() + "' and s.SubTutID = '" + gvChapters.SelectedDataKey.Value.ToString() + "'"



Hope this helps!



Hey, there is another error. I am unable to show the detailsView.



Set GridView DataKeyNames="SubTutID"


Then use SubTutID only as criteria


SqlCommand chapters = new SqlCommand("Select t.TutID, s.SubTutID from [Tutorial] t, [SubTutorial] s
where s.SubTutID = '" + gvChapters.SelectedValue.ToString() + "'", conn);







hayden, can you please share the complete detail of the error you are having and it will be good if you can post the compete stack trace of the error.


May be there is no data in the db for the combination of these IDs,just verify the data to be on safe side.


thanks 


沒有留言:

張貼留言