Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

When i choose From Date and Todate from datetimepicker to show data in datagridview at that time datagridview show me null data , code is written button click evet

What I have tried:

C#
 DateTime startT = new DateTime();
                DateTime endT = new DateTime();
                startT = dateTimePicker1.Value.Date;
                endT = dateTimePicker2.Value.Date;
                if (startT.Date > endT.Date)
                {
                    MessageBox.Show("To Date Cannot be greater than Start Date");
                }
                else
                {
                    string connetionString = null;
                    connetionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
                    con.ConnectionString = connetionString;

                    DataSet ds = new DataSet();
                    string sql = "SELECT Medicine_name,sum(Medicine_count) as Medicine_count,To_Date from Medicine_count where [To_Date] Between #" + startT.ToString("dd'/'MM'/'yyyy") + "#And #" + endT.ToString("dd'/'MM'/'yyyy") + "#group by Medicine_name,Medicine_count,To_Date";
                    OleDbConnection connection = new OleDbConnection(connetionString);
                    OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
                    ds = new DataSet();
                    connection.Open();
                    dataadapter.Fill(ds);
                    connection.Close();
                    dataGridView1.DataSource = ds;
                    temp = true;
}
Posted
Updated 8-Oct-16 22:34pm

1 solution

Stop doing that... Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
The chances are your problem will disappear at the same time.
C#
string sql = "SELECT Medicine_name, SUM(Medicine_count) AS Medicine_count, To_Date FROM Medicine_count WHERE [To_Date] BETWEEN ? AND ? GROUP BY Medicine_name,Medicine_count,To_Date";
OleDbConnection connection = new OleDbConnection(connetionString);
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
dataadapter.SelectCommand.Parameters.AddWithValue("?", startT);
dataadapter.SelectCommand.Parameters.AddWithValue("?", endT);


And the chances are that SELECT won't do what you want: GROUP BY will treat each column in the list as a reason to generate a new row: SQL GROUP By and the "Column 'name' is invalid in the select list because..." error[^]
 
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