[英]Gridview in asp.net C#
我有兩個下拉列表,對應於值,應該顯示gridview,下面是它的代碼。.但是我沒有得到問題所在!
protected void ddlstudents_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlstudents.SelectedIndex > 0)
{
BindData();
}
}
private void BindData()
{
try
{
SQLiteConnection con = new SQLiteConnection("data source=C:\\ITS Database\\its.development.sqlite3");
string strquery = "select topics.name,course_coverages.progress from topics JOIN course_coverages on topics.id=course_coverages.topic_id where course_coverages.student_id=@studentid AND course_coverages.course_id=@courseid";
con.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.connection=con;
cmd = con.CreateCommand();
cmd.CommandText = strquery;
cmd.Parameters.AddWithValue("@studentid", ddlstudents.SelectedIndex);
cmd.Parameters.AddWithValue("@courseid", ddlcourse.SelectedValue);
SQLiteDataAdapter ada = new SQLiteDataAdapter(cmd.CommandText, con);
SQLiteCommandBuilder cbl = new SQLiteCommandBuilder(ada);
DataTable dt = new DataTable();
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
catch (SQLiteException)
{
}
}
任何幫助,將不勝感激!!
提前致謝!!
了解如何找到自己的問題。 如果gridview沒有顯示正確的數據,則可以調試應用程序並查找失敗的地方。
您尚未給出如何綁定ddlstudents
和ddlcourse
,請檢查是否為ddlstudents.SelectedIndex
和ddlcourse.SelectedValue
獲得的值是否ddlcourse.SelectedValue
您的預期。
如果值正確,則可以使用上述值在數據庫上運行SQL語句並查看結果。
如果您確實需要查找錯誤,請從代碼中刪除try catch
語句,
如果發現異常,請對其進行處理。 否則不要。
嘗試這個
protected void ddlstudents_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlstudents.SelectedIndex > 0)
{
BindData();
}
}
private void BindData()
{
try
{
SQLiteConnection con = new SQLiteConnection("data source=C:\\ITS Database\\its.development.sqlite3");
string strquery = "select topics.name,course_coverages.progress from topics JOIN course_coverages on topics.id=course_coverages.topic_id where course_coverages.student_id=@studentid AND course_coverages.course_id=@courseid";
con.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.connection=con;
cmd = con.CreateCommand();
cmd.CommandText = strquery;
cmd.Parameters.AddWithValue("@studentid", ddlstudents.SelectedValue);
cmd.Parameters.AddWithValue("@courseid", ddlcourse.SelectedValue);
SQLiteDataAdapter ada = new SQLiteDataAdapter(cmd.CommandText, con);
SQLiteCommandBuilder cbl = new SQLiteCommandBuilder(ada);
DataTable dt = new DataTable();
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
catch (SQLiteException)
{
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.