0

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!!

13
  • Gridview is not displaying with data corresponding to the selected value!! Commented Feb 5, 2014 at 6:26
  • are you tried debug it? Commented Feb 5, 2014 at 6:27
  • yes!! several times!! its showing its bound with gridview!! but not shown in webpage Commented Feb 5, 2014 at 6:27
  • 1
    your select return value? Commented Feb 5, 2014 at 6:27
  • 1
    I think problem is with ddlstudents.SelectedIndex Commented Feb 5, 2014 at 6:28

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

mesa agrees with you on this
0

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

    }
}

8 Comments

sir!! nothing happened,,gridview is not displayed!!
can u explain clearly what the problem is when u change the index of ddlstudents control, is the control fires post back?
@Guru sir in postback i used seesion to save the username and in !page.postback i wrote code for binding data in ddlcourse dats it!!is that any thing m missing???
how, in page load the drop downs do not have values, so it might be parameters exception, did u checked with break point on page load for ddlstudents.SelectedValue and ddlcourse.SelectedValue, if no check it once, if the values are nulls you can modify the code under page load.
if (!Page.IsPostBack) { ddlcourse.AppendDataBoundItems = true; string strquery = "select id,name from courses order by name,id asc"; cmd = new SQLiteCommand(strquery); cmd.Connection = con; ddlcourse.DataSource = cmd.ExecuteReader(); ddlcourse.DataTextField = "name"; ddlcourse.DataValueField = "id"; ddlcourse.DataBind(); } this is the code i used in page load
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.