I would like to sum the following two columns from my database table and display it in form of a chart. My code below will run correct, but show empty chart result. Please help !
c.Open();
DateTime startDateTime = Convert.ToDateTime(textBox1.Text);
DateTime endDateTime = Convert.ToDateTime(textBox2.Text);
string query = "SELECT * FROM People_Tracking WHERE Enter_Exit_Time BETWEEN #startDateTime AND #endDateTime ;";
SqlCommand cmd = new SqlCommand(query, c);
cmd.Parameters.Add("#startDateTime", SqlDbType.DateTime).Value = startDateTime;
cmd.Parameters.Add("#endDateTime", SqlDbType.DateTime).Value = endDateTime;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable t = new DataTable();
adapter.Fill(t);
dataGridView1.DataSource = t;
string query1 = "SELECT Sum(Number_of_Enterance) FROM People_Tracking ;";
string query2 = "SELECT Sum(Number_of_Exits) FROM People_Tracking ;";
this.chart1.Series["DateTime"].Points.AddXY("Number_of_Enterance", query1);
this.chart1.Series["DateTime"].Points.AddXY("Number_of_Exits", query2);
Related
Below is my sql query written in APP.Config
<add key="query" value="Select *
from POManage po
Where po.VendorID = #manufacturerid
and po.order_date between #StartDate and #EndDate order by PONumber asc"/>
Here is my Program .cs code
int manufacturerid = 32;
DateTime StartDate = DateTime.Now.AddDays(-1);
DateTime EndDate = DateTime.Now;
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("#manufacturerid", SqlDbType.BigInt,manufacturerid);
cmd.Parameters.AddWithValue("#StartDate", StartDate);
cmd.Parameters.AddWithValue("#EndDate", EndDate);
cmd.CommandType = CommandType.Text;
SqlDataAdapter ad = new SqlDataAdapter(query, con);
ad.Fill(ds);
But it is showing error for #manufacturerid Must declare the scalar variable.
Similar issue is coming for StartDate and EndDate.
firstly, It's a very bad idea to keep the command query in the appconfig
You have already created a command with query. You need pass the command to Adapter. not query.
int manufacturerid = 32;
StartDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
EndDate = DateTime.Now.ToString("yyyy-MM-dd");
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("#manufacturerid", SqlDbType.BigInt,manufacturerid);
cmd.Parameters.AddWithValue("#StartDate", StartDate);
cmd.Parameters.AddWithValue("#EndDate", EndDate);
cmd.CommandType = CommandType.Text;
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(ds);
and change Where po.VendorID = '#manufacturerid' to Where po.VendorID = #manufacturerid in your query
update:
int manufacturerid = 32;
var query = #"Select * from POManage po
Where po.VendorID = #manufacturerid
and CONVERT(DATE, po.order_date) between #StartDate and #EndDate
order by PONumber asc";
var startDate = DateTime.Now.AddDays(-1);
var endDate = DateTime.Now;
var connectionString = "your connection string";
var dataTable = new DataTable();
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(query, con))
{
adapter.SelectCommand.Parameters.AddWithValue("#manufacturerid", manufacturerid);
adapter.SelectCommand.Parameters.AddWithValue("#StartDate", startDate);
adapter.SelectCommand.Parameters.AddWithValue("#EndDate", endDate);
adapter.Fill(dataTable);
}
}
I'm trying to make a query when the user clicks the End Shift button to get the total sales of the day right away without entering the date
I can't figure out how can that be possible, I'm currently using this code for it, which he has to choose 2 dates.
I tried with 1 datetimepicker but I never get a result
can it be done without the user choosing the date?
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
{
conn.Open();
string query = #"select SUM(SQuantity) AS 'Total' From Sales where Sdate = #datetime";
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(query, conn);
cmd.Parameters.Add("#datetime", System.Data.OleDb.OleDbType.Date).Value = dateTimePicker1.Value;
cmd.Parameters.Add("#datetime2", System.Data.OleDb.OleDbType.Date).Value = dateTimePicker2.Value;
cmd.ExecuteNonQuery();
string result = cmd.ExecuteScalar().ToString();
textBox1.Text = #result;
}
Try this
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
{
conn.Open();
string query = #"select SUM(SQuantity) From Sales where Sdate = #datetime";
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(query, conn);
cmd.Parameters.Add("#datetime", System.Data.OleDb.OleDbType.Date).Value = dateTimePicker1.Value.ToShortDateString();
//cmd.Parameters.Add("#datetime2", System.Data.OleDb.OleDbType.Date).Value = dateTimePicker2.Value;
//cmd.ExecuteNonQuery();
//string result = cmd.ExecuteScalar().ToString();
textBox1.Text = cmd.ExecuteScalar().ToString();
}
can it be done without the user choosing the date?
Yes, take a look at the #M.Rezaeyan answer.
Try
SELECT sum(SQuantity) as 'Total' FROM Sales WHERE Sdate = Date()
Final
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
{
conn.Open();
string query = #"select SUM(SQuantity) AS 'Total' From Sales where Sdate = Date()";
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(query, conn);
textBox1.Text = cmd.ExecuteScalar().ToString();
}
I'm trying to select a field from my table where date is less than today.
connect.Open();
command.Connection = connect;
today = DateTime.Today.ToString("dd/MM/yyyy");
string query = "select attendance as [Attend], Emp_UserId as ID, 'Date_ofday' as [Today Date] , Emp_UserName as Name ,Delay_Hours as [Delay Hours] from Attendance where Date_ofday > '"+ DateTime.Parse(today) + "' ";
command.CommandText = query;
OleDbDataAdapter da1 = new OleDbDataAdapter(command);
DataTable dt1 = new System.Data.DataTable();
da1.Fill(dt1);
dataGridView1.DataSource = dt1;
This gives a data type miss or missing operator error, using an Access DB.
You are converting a DateTime to string and then parse it back to DateTime in the next line, this doesn't make sense. I think you should use parameters, see if this works, I don't use MySql:
connect.Open();
command.Connection = connect;
todday = DateTime.Today.ToString("dd/MM/yyyy");
string query = "select attendance as [Attend], Emp_UserId as ID, 'Date_ofday' as [Today Date] , Emp_UserName as Name ,Delay_Hours as [Delay Hours] from Attendance where Date_ofday > #Today";
command.CommandText = query;
OleDbDataAdapter da1 = new OleDbDataAdapter(command);
DataTable dt1 = new System.Data.DataTable();
da1.SelectCommand.Parameters.AddWithValue("#Today", DateTime.Today);
da1.Fill(dt1);
dataGridView1.DataSource = dt1;
In the case you are using MS Access the query becomes:
string query = "select attendance as [Attend], Emp_UserId as ID, 'Date_ofday' as [Today Date] , Emp_UserName as Name ,Delay_Hours as [Delay Hours] from Attendance where Date_ofday > ?";
What I'm trying here is to compare the dates. No Error is displayed when running this code , but at the same time, code does not fetch any data from database too either.
The code is as shown below :
string constr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Auto_Finance.accdb;Persist Security Info=False;";
OleDbConnection con = new OleDbConnection(constr);
con.Open();
string d1 = DateTime.Now.ToString("MM/dd/yyyy");
DateTime df6 = DateTime.Now;
int da1 = df6.Date.Month;
int yr = df6.Date.Year;
//DateTime df7 = new DateTime(df6.Year, df6.Month, df6.Day).AddYears(+1);
//string sql = #"select * from ins where mon<=[#mon] and yr<=[#yr]";
string sql = #"select * from ins where Insdate<=[#ddd]";
OleDbCommand cmd = new OleDbCommand(sql, con);
//cmd.Parameters.AddWithValue("#mon", da1);
//cmd.Parameters.AddWithValue("#yr", yr);
cmd.Parameters.AddWithValue("#ddd", d1);
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
Any clarification and guidance is appreciated on what could be the cause of the issue here.
I have 5 data in my datatbase, these data I want to display in a gridView using Data Table. But my code displays only the last binded data in GridView? My code is. Please point out the mistake?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand cmd = new SqlCommand("select Date from MusterRoll where EmpCode='"+code+"' and Month='1' and Year='2015'", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
var rows = ds.Tables[0].Rows;
foreach (DataRow row in rows)
{
string date1 = Convert.ToString(row["Date"]);
DateTime date2 = Convert.ToDateTime(date1);
SqlCommand cmd1 = new SqlCommand(" select TOP 1 m.EmpCode,m.NOH,m.OT,m.Late,m.Early,convert(varchar(10),m.Date,103)AS DATE,convert(varchar(10),s1.Shiftname,103)AS Shift From ShiftChange s,ShiftType s1,MusterRoll m WHERE s1.ShiftID=s.NShiftID and '" + date2 + "'>=Fromdate and Todate>='" + date2 + "' and m.Month = '1' and m.date='"+date2+"' and m.EmpCode='Neena' order by Todate desc", conn);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
sda1.Fill(dt);
//var rows1 = ds.Tables[0].Rows;
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
DataRow rpw = dt.Rows[rowIndex];
string EmpCode = rpw.Field<string>("EmpCode");
string NOH = rpw.Field<string>("NOH");
string OT = rpw.Field<string>("OT");
string Latae = rpw.Field<string>("Late");
string Early = rpw.Field<string>("Early");
string date3 =rpw.Field<string>("Date");
string Shift = rpw.Field<string>("Shift");
gvSingleemp.Visible = true;
gvSingleemp.DataSource = dt;
gvSingleemp.DataBind();
}
}
In my shiftChange table there is no Field for date instead of that I have fromDate and ToDate.I want display employee shifft according to MusterRoll table date. So that first I selected MusteRoll date nd checkrd this date exist in between ShiftChange FromDate and ToDate if exist show the Shift
You are databinding the GridView in a loop. You don't need the loop, just bind it to the DataTable:
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
sda1.Fill(dt);
gvSingleemp.DataSource = dt;
gvSingleemp.DataBind();
I still think that you don't need those loops at all. I guess that you want to select all records from EmpDetails where the EmpCode = code and MusterRoll.Month='1' and MusterRoll.Year='2015'. Then you only need one sql query to fill one DataTable which can be used as DataSource for gvSingleemp. Is that correct?
If so, this should work (note that i use the using statement and sql-parameters):
DataTable tblData = new DataTable();
string sql = #"SELECT ed.EmpCode,ed.Name,ed.Age,ed.Date
FROM MusterRoll mr
INNER JOIN EmpDetails ed
ON mr.Date = ed.Date
WHERE mr.EmpCode=#EmpCode AND mr.Month=1 AND mr.Year=2015";
using(var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
using(var sda = new SqlDataAdapter(sql, conn))
{
var codeParam = new SqlParameter("#EmpCode", SqlDbType.VarChar).Value = code; // change type accordingly
sda.SelectCommand.Parameters.Add(codeParam);
sda.Fill(tblData); // no need for conn.Open/Close with SqlDataAdapter.Fill
}
gvSingleemp.Visible = true;
gvSingleemp.DataSource = tblData;
If you don't want to join the tables you can also use EXISTS:
string sql = #"SELECT ed.EmpCode, ed.Name, ed.Age, ed.Date
FROM EmpDetails ed
WHERE EXISTS
(
SELECT 1 FROM MusterRoll mr
WHERE mr.EmpCode = #EmpCode
AND mr.Month = 1 AND mr.Year=2015
AND mr.Date = ed.Date
)";
You dont have to use loop to bind the DT to GridView :
SqlCommand cmd1 = new SqlCommand(" select EmpCode,Name,Date,Age from EmpDetails where CompanyID='1'", conn);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
sda1.Fill(dt);
gvSingleemp.DataSource =dt;
gvSingleemp.DataBind();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
DataTable data = new DataTable();
Da.Fill(data);
gvSingleemp.DataSource = data;
gvSingleemp.DataBind();