Click here to Skip to main content
15,881,872 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
try
                {

                    con.Open();
                    OleDbCommand cmnd = new OleDbCommand();
                    cmnd.Connection = con;
                    string query = "select FullName,datein,remarks from QAttendance where SubjDesc ='" + cBsubject.Text + "' AND datein= #" + lblcurdate.Text+"#";
                    cmnd.CommandText = query;
                    OleDbDataReader reader = cmnd.ExecuteReader();
                    while (reader.Read())
                    {
                        DataTable dt = new DataTable();
                        dt.Load(reader);
                        dtgviewatt.DataSource = dt;
                    }
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
Posted
Updated 6-Dec-14 4:15am
v2
Comments
[no name] 6-Dec-14 10:17am    
What means "cannot display select statement"?
Any exception while execute SQL?
markqui 6-Dec-14 10:20am    
my datagridview can't display anything even if it doesn't return any error..

Um.
If you want to load a DataTable, then you don't want to do it in a loop...
And you shouldn't be concatenating strings to form SQL statements, it's very, very dangerous - it means a user can damage or destroy your database just by typing in the text boxes. You should always use a parameterised query instead.

Try using a DataAdapter instead of a DataReader.

And the other reason it doesn't work is probably that the date doesn't work either - because SQL doesn't know what date format your user is entering data in, so assuming your user doesn't make any mistakes, it can still interpret it wrongly.

So, start by checking the date he entered - if it's valid, convert it to a DateTime value, if it isn't complain and do no more. (Or by preference, use a DateTimePicker control instead of a textbox, and use the Value property directly - it will always be a valid DateTime)
Then use a DataAdapter with a parameterised query:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter("SELECT FullName,datein,remarks FROM QAttendance WHERE SubjDesc = @SUBJ AND datein= @DATE", con);
    da.SelectCommand.Parameters.AddWithValue("@SUBJ", cBsubject.Text);
    da.SelectCommand.Parameters.AddWithValue("@DATE", userDateTimeValue);
    DataTable dt = new DataTable();
    da.Fill(dt);
    dtgviewatt.DataSource = dt;
    }
You should find it works.
 
Share this answer
 
Comments
markqui 6-Dec-14 12:10pm    
is this the same with c# and ms access syntax.?thanks anyway
OriginalGriff 6-Dec-14 12:25pm    
Should, be pretty much, with OleDb... Instead of SQL...

I copy 'n' pasted it from existing code, and forgot the Access part. :blush:
markqui 6-Dec-14 13:13pm    
what about parameters.addwithvalue used for?
OriginalGriff 6-Dec-14 13:40pm    
It allows you to send a value through to SQL (or Access, or Oracle, or ...) Without conveying out to a string. Which means that it can't be used for SQL Injection, and can't be misinterpreted by the Database. The first parameter ties the value to the position in the SQL command string.
markqui 6-Dec-14 14:14pm    
ok thanks... but my point is i just wanted to display in datagridview which has the same date from my database and the current date that i put in lblcurdate.text...:) please ty
A bit description of your code:
C#
OleDbDataReader reader = cmnd.ExecuteReader();
//read data from OleDbReader
//loop through the collection of rows
while (reader.Read())
{
    //create new datatable
    DataTable dt = new DataTable();
    dt.Load(reader);
    //set datasource for dgv
    dtgviewatt.DataSource = dt;
}

So, instead of above, use this (skip While loop and use named parameters):
C#
OleDbCommand cmnd = new OleDbCommand();
                    cmnd.Connection = con;
                    string query = "PARAMETERS [subjectdesc] CHAR, [myDate] DATE; select FullName,datein,remarks from QAttendance where SubjDesc =[subjectdesc] AND datein=[myDate]";
                    cmnd.CommandText = query;
                    cmnd.CommandType = CommandType.StoredProcedure;
                    cmnd.Parameters.AddWithValue("subjectdesc", cBsubject.Text);
                    cmnd.Parameters.AddWithValue("myDate", lblcurdate.Text);
                    OleDbDataReader reader = cmnd.ExecuteReader();
                    DataTable dt = new DataTable();
                    dt.Load(reader);
                    dtgviewatt.DataSource = dt;
                    con.Close();


For furher information, please see:
OleDbCommand.CommandType Property[^]
OleDbParameterCollection.AddWithValue Method [^]
Commands and Parameters[^]
PARAMETERS Declaration (Microsoft Access SQL)[^]
 
Share this answer
 
v2
Comments
markqui 7-Dec-14 2:00am    
is this the same with c# and ms access syntax?thanks
Maciej Los 7-Dec-14 4:40am    
Sorry, i can't get you... What you mean: "is this the same with c# and ms access syntax"?
markqui 7-Dec-14 4:46am    
i am using ms access as my database..is that the same syntax from your code?
Maciej Los 7-Dec-14 4:48am    
It's your code partially changed by me. Yes, it is suitable for MS Access.

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