Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In database records as follows;

Faculty      Date         Sess1          Sess2           Sess3          Sess4

 RAM        1/1/2013  checkboxchecked checkboxchecked checkboxchecked checkboxchecked
 RAM        2/1/2013  checkboxchecked checkboxchecked checkboxchecked checkboxchecked



Design as follows;

Faculty Code combo box

Datagridiview as follows;

 Date         Sess1          Sess2           Sess3          Sess4
1/1/2013  checkboxchecked checkboxchecked checkboxchecked checkboxchecked
2/1/2013  checkboxchecked checkboxchecked checkboxchecked checkboxchecked

in run mode when i click the Faculty Code, the particular faculty code record is to be retrieved from the database and display into the datagridview.

For that code as follows;

C#
private void cb_Faculty_Code_SelectedIndexChanged(object sender, EventArgs e)
     {

   int irows = 0;
         try
         {
             sql = "Select [Available_date],[Session1],[Session2],[Session3],[Session4]  from Tb_Faculty_Availability where Faculty_code = '" + cb_Faculty_Code.Text.ToString() + "'";
             dr = GFun.ReadAcessSql1(sql);
             while (dr.Read())
             {
         for (irows = 0; irows < DGVCalendar.RowCount; irows++)
                 {

                     DGVCalendar.Rows[irows].Cells[2].Value = dr[0].ToString();
                     DGVCalendar.Rows[irows].Cells[4].Value = dr[1].ToString();
                     DGVCalendar.Rows[irows].Cells[5].Value = dr[2].ToString();
                     DGVCalendar.Rows[irows].Cells[6].Value = dr[3].ToString();
                     DGVCalendar.Rows[irows].Cells[7].Value = dr[4].ToString();

                 }
                     irows++;
             }
             dr.Close();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.ToString(), "Error");
             return;
         }

In run mode when i click the combobox(Faculty code) in the datagridview the selected faculty code not displaying in the datagridview.

what is the problem in my above code?

Please help me.

Note it is windows application.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 16-Mar-13 0:54am
v2
Comments
OriginalGriff 16-Mar-13 6:56am    
So what is happening?
Any display change, message box, error message?
Have you tried the debugger?
[no name] 16-Mar-13 7:58am    
yes i tried the debugger. the error not shows, when i run in the datagridview the data's are not retrieved from the database to the datagridview.


how can i do? please help me.

Regards,

Narasiman P.
[no name] 16-Mar-13 9:54am    
Debug GFun.ReadAcessSql1(sql); and see what it is doing.
[no name] 16-Mar-13 9:57am    
Coupled with there is no "Faculty_code" in your database. It's "Faculty", just as there is no "Session1", "Session2" etc. Did you think that you might get faster answers if you just asked your teacher?
Prasad Khandekar 16-Mar-13 10:05am    
Hello Narsiman,

What not use DataBind instead of manually appending into the GridView. Not related to your error but try to avoid SQL construction by concatenating values. Use SQLCommand instead. It will protect your application (to certain extent) from SQL Injection.

Regards,

1 solution

Oh boy Narasiman,

You are getting stress about your homework aren't you, that you cannot see what you are doing.

You didn't add the faculty code to the datagrid, so how will it display?

you should have something like this in you for loop;

C#
DGVCalendar.Rows[irows].Cells[2].Value = cb_Faculty_Code.Text;


By the way Text is a string, you do not need to convert to string in your query.

Also what is the purpose of the irows variable, why are you incrementing is outside the for loop?

Here is the optimized code for you.
C#
private void cb_Faculty_Code_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        sql = "Select [Available_date],[Session1],[Session2],[Session3],[Session4]  from Tb_Faculty_Availability where Faculty_code = '" + cb_Faculty_Code.Text + "'";
        dr = GFun.ReadAcessSql1(sql);
        while (dr.Read())
        {
            for (int irows = 0; irows < DGVCalendar.RowCount; irows++)
            {
                DGVCalendar.Rows[irows].Cells[2].Value = cb_Faculty_Code.Text;
                DGVCalendar.Rows[irows].Cells[2].Value = dr[0].ToString();
                DGVCalendar.Rows[irows].Cells[4].Value = dr[1].ToString();
                DGVCalendar.Rows[irows].Cells[5].Value = dr[2].ToString();
                DGVCalendar.Rows[irows].Cells[6].Value = dr[3].ToString();
                DGVCalendar.Rows[irows].Cells[7].Value = dr[4].ToString();
               
            }
        }
        dr.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), "Error");
        return;
    }
}


I hope you get good grade on your homework.

Regards
Jegan
 
Share this answer
 
v2

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