Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys am select a data from database and second time when i select data from with where clause dt1 so i get return null values am not getting any error but it's return me null values

What I have tried:

con_string.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\Restaurant.accdb;Persist Security Info=False";
            con_string.Open();
            DataSet dsa2 = new DataSet();
            DataTable dt2 = new DataTable();
            dsa2.Tables.Add(dt2);
            OleDbDataAdapter da2 = new OleDbDataAdapter();
            da2 = new OleDbDataAdapter(string.Format("SELECT column2 FROM Total  Where [Date] between #{0}# AND #{1}#  Group By column2", dateTimePicker1.Text, dateTimePicker2.Text), con_string);
            da2.Fill(dt2);
            con_string.Close();
            //--------------------------------------------------------------------------------
            con_string.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\Restaurant.accdb;Persist Security Info=False";
            con_string.Open();
            DataSet dsa = new DataSet();
            DataTable dt1 = new DataTable();
            dsa.Tables.Add(dt1);
            OleDbDataAdapter da = new OleDbDataAdapter();
            da = new OleDbDataAdapter(string.Format(string.Format("SELECT column2,Sum(MediumVal) As [Dine In], Sum(LargeVal) As [Deliery], Sum(RoyalVal) As [Take Away] From ( SELECT column2 As [column2], Switch(column3 like 'DineIn%', 1,True,0) As [MediumVal], Switch(column3 like 'Delivery%',1,True,0) As [LargeVal], Switch(column3 like 'TakeAway%', 1,True,0) As [RoyalVal] FROM Total  Where [Date] between #{{0}}# AND #{{1}}# AND [column2] IN('{0}') ) Group By column2", dt2.Columns["column2"]), dateTimePicker1.Text, dateTimePicker2.Text), con_string);
            da.Fill(dt1);
            dataGridView1.DataSource = dt1;
            con_string.Close();
            dataGridView1.Columns[0].Width = 286;
            dataGridView1.Columns[1].Width = 180;
            dataGridView1.Columns[2].Width = 180;
            dataGridView1.Columns[3].Width = 180;
Posted
Updated 3-Jul-17 9:03am
Comments
[no name] 3-Jul-17 13:55pm    
If you are getting null from the database, did you validate if the database has the values?
Member 9983063 3-Jul-17 14:25pm    
db has values

And the reason is either the date formats don't match for whatever reason, so SQL is getting them wrong, or there is no data which falls in the range.

Start by not concatenating strings to form SQL commands: use a parameterised query and past the DateTimePicker DateTime values directly instead of converting them to strings first. That way, the actual DateTime is used by SQL without any risk of misinterpretation when the string is processed. You should 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. Always use Parameterized queries instead.

Then check your DB: make sure you are storing the [Date] column as a DATE, DATETIME, or DATETIME2 value, not as a string. Yes, yes - I know. But you'd be surprised how many people do store dates as NVARCHAR and then act all surprised when comparisons like BETWEEN use NVARCHAR comparison rules not DATE and they don't get the results they wanted.

Once that's all checked, use the debugger to look at the date ranges you are passing: SQL / Access will not "reorder" the start and end to make them sensible, so if the later date is first, you won't get any results. If that's all fine, look directly at the database table and manually verify that dates in that range do really exist.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Part of this query could be the issue. The code have double curly brackets instead of one.

SQL
FROM Total  Where [Date] between #{{0}}# AND #{{1}}# AND [column2]


It should be

SQL
FROM Total  Where [Date] between #{0}# AND #{1}# AND [column2]
 
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