Click here to Skip to main content
15,900,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
These methods give result, only if both Image_ID1 and Image_ID2 are not null.
If one of them or both of them are null, then it shows error.
Besides it returns only even value counts. Odd value counts are skipped.
Example, Column Image_ID1=1,3,null and Image_ID2=2,4,6.
It has to show 5 values but it shows 4 values cause of that null value counts.
How can I solve these issues?

C#
private void BindGrid()
    {
        MySqlConnection con = new MySqlConnection(constr);
        MySqlCommand cmd = new MySqlCommand("SELECT * FROM images where Image_ID in (" + String.Join(",", getImage_ID()) + ")", con);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        gvImages.DataSource = dt;
        gvImages.DataBind();
    }
    private List<int> getImage_ID()
    {
        List<int> i = new List<int>();
        MySqlConnection con = new MySqlConnection(constr);
        con.Open();
        string query = "Select Image_ID1, Image_ID2 from register where students_ID='" + getStudents_ID() + "'AND Image_ID1 IS NOT NULL AND Image_ID2 IS NOT NULL"; 
        MySqlCommand cmd = new MySqlCommand(query);
        cmd.Connection = con;
        MySqlDataReader reader = cmd.ExecuteReader();
        foreach (DbDataRecord s in reader)
            {
                i.Add(s.GetInt32(0));
                i.Add(s.GetInt32(1));
            }
        return i;
    }
Posted
Updated 17-Sep-15 23:12pm
v2
Comments
CPallini 18-Sep-15 5:12am    
So? What is wrong behaviour? What is the expected result?
Krunal Rohit 18-Sep-15 5:20am    
You have used IS NOT NULL in query, which indeed going to skip the NULL records.

-KR

1 solution

C#
string query = "Select Image_ID1, Image_ID2 from register where students_ID='" + getStudents_ID() + "'"; 

and check null in
C#
foreach (DbDataRecord s in reader)
            {
                if (s["Image_ID1"] != DBNull.Value){
                   i.Add(s.GetInt32(0));
                }
                if (s["Image_ID2"] != DBNull.Value){
                  i.Add(s.GetInt32(1));
                }
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900