Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys, I am facing an issue that is I get value null from DB I have values in my DB but in my form when I select so I get null please help me how can I do it

What I have tried:

C#
con_string.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\Restaurant.accdb;Persist Security Info=False";
            con_string.Open();
            DataSet dsa22 = new DataSet();
            DataTable dt22 = new DataTable();
            dsa22.Tables.Add(dt22);
            OleDbDataAdapter da22 = new OleDbDataAdapter();
            string query = "SELECT count(column3) As [QTY]  from [Total] Where [column3] like 'DineIn*'  AND  [Column1] <>0   AND  Cancel  IS NULL And [Date] Between #" + System.DateTime.Now.ToShortDateString() + "# AND #" + System.DateTime.Now.AddDays(1).ToShortDateString() + "# Group By [column3] ";
            da22 = new OleDbDataAdapter(query,con_string);
                      da22.Fill(dt22);
            con_string.Close();
            int sum22 = 0;
            for (int i = 0; i < dsa22.Tables[0].Rows.Count; ++i)
            {
                sum22 += Convert.ToInt32(dsa22.Tables[0].Rows[0][0].ToString());
            }
           Dinein_Orders.Text = sum22.ToString();
Posted
Updated 20-Oct-17 14:05pm
v2
Comments
codejet 20-Oct-17 15:20pm    
Are you getting nulls for all the columns or a particular column?
Member 9983063 20-Oct-17 15:30pm    
i am select just the particular column and I null value from it
Karthik_Mahalingam 20-Oct-17 22:55pm    
Use reply button to post comments. So that the user gets notified and respond to your text
PIEBALDconsult 20-Oct-17 19:42pm    
You're doing a whole lot of things poorly, so it will take time to figure it out.
I'm guessing you have a VB background.
First off, do NOT use string concatenation to form SQL statements -- ALWAYS use parameterized statements.
Second, do NOT use ToString and then try to Convert -- simply cast the value.
Avoid DataAdapters.
It looks like you actually want to use ExecuteScalar.

1 solution

Just off the top of my head; something like this might serve you better.

C#
con_string.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\Restaurant.accdb;Persist Security Info=False";

con_string.Open();

System.Data.IDbCommand cmd = con_string.CreateCommand() ;

cmd.CommandText = "SELECT count(column3) As [QTY]  from [Total] Where [column3] like 'DineIn*'  AND  [Column1] <>0   AND  Cancel  IS NULL And [Date] Between ? AND ? Group By [column3] ";

System.Data.IDbDataParameter prm = cmd.CreateParameter() ;
prm.ParameterName = "?" ;
prm.Value = System.DateTime.Now;
cmd.Parameters.Add ( prm ) ;

prm = cmd.CreateParameter() ;
prm.ParameterName = "?" ;
prm.Value = System.DateTime.Now.AddDays(1) ;
cmd.Parameters.Add ( prm ) ;

Dinein_Orders.Text = cmd.ExecuteScalar().ToString();

con_string.Close();



I do hope you're not storing dates as strings.
 
Share this answer
 
Comments
Member 9983063 21-Oct-17 2:51am    
bro still am getting an error now here is my code stuck
Dinein_Orders.Text = cmd.ExecuteScalar().ToString();
PIEBALDconsult 21-Oct-17 12:08pm    
You can use the "Improve question" to edit your question and add the updated code and the full text of the error you get.

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