简体   繁体   中英

Gridview in asp.net C#

I have two dropdownlist, corresponding to the values,gridview should be displayed,,and below is code for it..But i am not getting What's the problem in it!!

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)
    {

    }
}

Any Help Would Be Appreciated!!

Thanks in Advance!!

Learn how to find the problem your self. if the gridview not showing correct data you can debug the application and find where it failed.

you have not given how you bind ddlstudents and ddlcourse , check the values you get for ddlstudents.SelectedIndex and ddlcourse.SelectedValue as you expected or not.

if values are correct, you can run the SQL statement on your database with above values and see the results.

If you really need to find the error, remove the try catch statement from your code,

If you catch the exception, do something with it. otherwise don't.

try this

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)
    {

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM