Search between two dates not working - c#

I have the following code:
var q = context.Fuels.AsQueryable();
if (dateEdit1.EditValue != null && dateEdit2.EditValue != null)
{
q.Where(d => d.FuelDate >= dateEdit1.DateTime && d.FuelDate <= dateEdit2.DateTime);
}
and it's working, it will get all rows in table.
but when I'm using this code:
if (dateEdit1.EditValue != null && dateEdit2.EditValue != null)
{
var r = context.ExecuteStoreQuery<Fuel>(String.Format("SELECT * FROM Fuel WHERE FuelDate BETWEEN '{0}' AND '{1}'",
dateEdit1.DateTime.Year + "-" + dateEdit1.DateTime.Month + "-" + dateEdit1.DateTime.Day,
dateEdit2.DateTime.Year + "-" + dateEdit2.DateTime.Month + "-" + dateEdit2.DateTime.Day));
}
it's working but I need to use the first way to get relations working.
Any idea what is the problem in the first one?

Inside of your if clause, you need to assign it to q, if you don't do that q is just a query for all Fuels
This should work:
q = q.Where(d => d.FuelDate >= dateEdit1.DateTime && d.FuelDate <= dateEdit2.DateTime);

Related

C# windows form project Divide variable error

Greeting everyone, I'm trying to do a windows form for collage were I can get the average of student scores. Seems like I have an issue with the divide function. here is my code , to see if you guys spot something wrong that could help me. I Appreciate in advance
private void getaveragebutton_Click(object sender, EventArgs e)
{
if (customTextboxes4.Texts == "" || customTextboxes5.Texts == "" || customTextboxes6.Texts == "" || customTextboxes7.Texts == "" || customTextboxes8.Texts == "" ||
customTextboxes9.Texts == "" || customTextboxes10.Texts == "" || customTextboxes11.Texts == "" || customTextboxes12.Texts == "" || customTextboxes13.Texts == ""
|| customTextboxes15.Texts == "" || customTextboxes16.Texts == "" || customTextboxes17.Texts == "" || customTextboxes18.Texts == "" || customTextboxes19.Texts == "" ||
customTextboxes20.Texts == "" || customTextboxes21.Texts == "" || customTextboxes22.Texts == "" || customTextboxes23.Texts == "" || customTextboxes24.Texts == "")
{
MessageBox.Show("Fill all the Boxes with Digits");
}
else
{
label4.Visible = true;
label5.Visible = true;
label7.Visible = true;
label61.Visible = true;
label28.Visible = true;
label29.Visible = true;
label30.Visible = true;
label31.Visible = true;
label45.Visible = true;
label46.Visible = true;
//short--test calculations
//students short test score variables
decimal a, b, c, d, e1, f, g, h, i, j;
//students short test value variables
decimal k, l, m, n, o, p, q, r, s, t;
//st results variables
decimal u, v;
decimal r1st;
decimal r2st;
decimal results;
//customboxes st score
a = Decimal.Parse(customTextboxes4.Texts);
b = Decimal.Parse(customTextboxes5.Texts);
c = Decimal.Parse(customTextboxes6.Texts);
d = Decimal.Parse(customTextboxes7.Texts);
e1 = Decimal.Parse(customTextboxes8.Texts);
f = Decimal.Parse(customTextboxes9.Texts);
g = Decimal.Parse(customTextboxes10.Texts);
h = Decimal.Parse(customTextboxes11.Texts);
i = Decimal.Parse(customTextboxes12.Texts);
j = Decimal.Parse(customTextboxes13.Texts);
//custom boxes st values
k = Decimal.Parse(customTextboxes15.Texts);
l = Decimal.Parse(customTextboxes16.Texts);
m = Decimal.Parse(customTextboxes17.Texts);
n = Decimal.Parse(customTextboxes18.Texts);
o = Decimal.Parse(customTextboxes19.Texts);
p = Decimal.Parse(customTextboxes20.Texts);
q = Decimal.Parse(customTextboxes21.Texts);
r = Decimal.Parse(customTextboxes22.Texts);
s = Decimal.Parse(customTextboxes23.Texts);
t = Decimal.Parse(customTextboxes24.Texts);
r1st = a + b + c + d + e1 + f + g + h + i + j;
r2st = k + l + m + n + o + p + q + r + s + t;
//st results variables definitions
u = Decimal.Parse(label28.Text);
v = Decimal.Parse(label29.Text);
//st result
label28.Text = r1st.ToString();
label29.Text = r2st.ToString();
results = u / v;
label4.Text = results.ToString();
return;
}
[enter image description here](https://i.stack.imgur.com/xu8cp.png)
Im trying to get all the score inserted in the text boxes to sum them. then get the results to Student Score and Total (That part is working for me) . After that divide the Student Score / Total to get the results in a hidden label that will be visible after getting the results. (that is the part that Im having issues with)
Well sorry to everyone for not following the proper way to post to get the proper help and thanks for those who let me know what to do for a next time to get the proper help. Also , thank you for explaining me the variable thing that was empty I really appreciate. Here is how I did fix my issue:
private void getaveragebutton_Click(object sender, EventArgs e)
{
if (customTextboxes4.Texts == "" || customTextboxes5.Texts == "" || customTextboxes6.Texts == "" || customTextboxes7.Texts == "" || customTextboxes8.Texts == "" ||
customTextboxes9.Texts == "" || customTextboxes10.Texts == "" || customTextboxes11.Texts == "" || customTextboxes12.Texts == "" || customTextboxes13.Texts == ""
|| customTextboxes15.Texts == "" || customTextboxes16.Texts == "" || customTextboxes17.Texts == "" || customTextboxes18.Texts == "" || customTextboxes19.Texts == "" ||
customTextboxes20.Texts == "" || customTextboxes21.Texts == "" || customTextboxes22.Texts == "" || customTextboxes23.Texts == "" || customTextboxes24.Texts == "")
{
MessageBox.Show("Fill all the Boxes with Digits");
}
else
{
label4.Visible = true;
label5.Visible = true;
label7.Visible = true;
label61.Visible = true;
label28.Visible = true;
label29.Visible = true;
label30.Visible = true;
label31.Visible = true;
label45.Visible = true;
label46.Visible = true;
//short--test calculations
//students short test score variables
decimal studentscorebox1, studentscorebox2, studentscorebox3, studentscorebox4, studentscorebox5, studentscorebox6, studentscorebox7, studentscorebox8, studentscorebox9, studentscorebox10;
//students short test value variables
decimal studentshorttestvalue1, studentshorttestvalue2, studentshorttestvalue3, studentshorttestvalue4, studentshorttestvalue5, studentshorttestvalue6, studentshorttestvalue7, studentshorttestvalue8,
studentshorttestvalue9, studentshorttestvalue10;
//st results variables
decimal studentshortestoverallscore;
decimal shortestoverallvalue;
decimal shortestaverage;
//customboxes student short test scores
studentscorebox1 = Decimal.Parse(customTextboxes4.Texts);
studentscorebox2 = Decimal.Parse(customTextboxes5.Texts);
studentscorebox3 = Decimal.Parse(customTextboxes6.Texts);
studentscorebox4 = Decimal.Parse(customTextboxes7.Texts);
studentscorebox5 = Decimal.Parse(customTextboxes8.Texts);
studentscorebox6 = Decimal.Parse(customTextboxes9.Texts);
studentscorebox7 = Decimal.Parse(customTextboxes10.Texts);
studentscorebox8 = Decimal.Parse(customTextboxes11.Texts);
studentscorebox9 = Decimal.Parse(customTextboxes12.Texts);
studentscorebox10 = Decimal.Parse(customTextboxes13.Texts);
//student short test value
studentshorttestvalue1 = Decimal.Parse(customTextboxes15.Texts);
studentshorttestvalue2 = Decimal.Parse(customTextboxes16.Texts);
studentshorttestvalue3 = Decimal.Parse(customTextboxes17.Texts);
studentshorttestvalue4 = Decimal.Parse(customTextboxes18.Texts);
studentshorttestvalue5 = Decimal.Parse(customTextboxes19.Texts);
studentshorttestvalue6 = Decimal.Parse(customTextboxes20.Texts);
studentshorttestvalue7 = Decimal.Parse(customTextboxes21.Texts);
studentshorttestvalue8 = Decimal.Parse(customTextboxes22.Texts);
studentshorttestvalue9 = Decimal.Parse(customTextboxes23.Texts);
studentshorttestvalue10 = Decimal.Parse(customTextboxes24.Texts);
//Sumatory of Students scores and Sumatory of Test Value
studentshortestoverallscore = studentscorebox1 + studentscorebox2 + studentscorebox3 + studentscorebox4 + studentscorebox5 + studentscorebox6 + studentscorebox7 + studentscorebox8 + studentscorebox9 + studentscorebox10;
shortestoverallvalue = studentshorttestvalue1 + studentshorttestvalue2 + studentshorttestvalue3 + studentshorttestvalue4 + studentshorttestvalue5 + studentshorttestvalue6 + studentshorttestvalue7 +
studentshorttestvalue8 + studentshorttestvalue9 + studentshorttestvalue10;
//String Results to Labels
label28.Text = studentshortestoverallscore.ToString();
label29.Text = shortestoverallvalue.ToString();
//Get Average of Short Test
shortestaverage = studentshortestoverallscore / shortestoverallvalue * 100;
label4.Text = shortestaverage.ToString();
return;
}
}
I did a correction of the variable names to not get confused like I was advised in the first post , then I deleted some variables that were empty like someone here explained me (thanks). Finally I did a re order of how it should be calculated to get the average and got it working. thanks to all and if you see something that I can improve let me know. I really appreciate everyone and every single advise. Have a good day everyone.
You cannot convert label to decimal as long as label contains no value. You have to rearrange the lines as follows
//st result
label28.Text = r1st.ToString();
label29.Text = r2st.ToString();
u = Decimal.Parse(label28.Text);
v = Decimal.Parse(label29.Text);
results = u / v;
label4.Text = results.ToString();
return;

datagdridview display all days of each month between two dates

can someone help me i tried to display all days of months in datagridview c# but it show me in one row instead 15 days for each rows. here is what I need exactly..
I already did this in listbox and it works fine see image.
can someone please tell me how to draw and do the same in datagridview, because dgv display one rows for each month, while i want to break it in 15th of month to have 4 rows for each month..
here is what I tried so far in listbox and it works.
List<DateTime> dates = new List<DateTime>();
var d1 = dateTimePicker1.Value;
var d2 = dateTimePicker2.Value;
for (var dt = d1; dt <= d2; dt = dt.AddMonths(1))
{
dates.Add(dt);
int m1days = DateTime.DaysInMonth(dates[0].Year, dates[0].Month);
int m2days = DateTime.DaysInMonth(dates[1].Year, dates[1].Month);
int m3days = DateTime.DaysInMonth(dates[2].Year, dates[2].Month);
var culture = new System.Globalization.CultureInfo("fr-FR");
for (int dayCounter = 1; dayCounter <= m1days; dayCounter++)
{
var checked = "";
if (IsWeekend(Convert.ToDateTime("" + dates[0].Year + "-" + dates[0].Month + "-" + dayCounter + "")))
{
checked = "";
}
else
{
checked = " - X";
}
var weekend = ((IsWeekend(Convert.ToDateTime("" + dates[0].Year + "-" + dates[0].Month + "-" + dayCounter + ""))) ? " (Weekend)" : "");
listBox1.Items.Add(dayCounter + weekend + "" + checked);
}
for (int dayCounter = 1; dayCounter <= m2days; dayCounter++)
{
var checked = "";
if (IsWeekend(Convert.ToDateTime("" + dates[1].Year + "-" + dates[1].Month + "-" + dayCounter + "")))
{
checked = "";
}
else
{
checked = " - X";
}
var weekend = ((IsWeekend(Convert.ToDateTime("" + dates[1].Year + "-" + dates[1].Month + "-" + dayCounter + ""))) ? " (Weekend)" : "");
listBox2.Items.Add(dayCounter + weekend + "" + checked);
}
}
please if someone could help me to achieve the same results of the first above image using datagridview, I will greatly appreciate it!
Thanks in advance!
Edit: here is Iweekend function, i forgot to i
private static bool IsWeekend(DateTime date)
{
return date.DayOfWeek == DayOfWeek.Saturday
|| date.DayOfWeek == DayOfWeek.Sunday;
}
This is the same code I have, but the one I use it for listbox, can you create or do one for datagidview please?
I already did it in listview and it works..
But I want the same as this in report viewer to be generated when click button between two dates.

Populating data from database into multiple colums equally?

I'm trying to populate two different container equally with data coming from the database. Putting 5 locations in the first container and 6 at the second. Every time that run this code, it populates all into the left container.
Any Ideas what am I missing?
shopDataContext dc = new shopDataContext();
var areas = (from s in dc.Areas select s);
String content = "";
foreach(var a in areas) {
if (a.id >= 1 && a.id <= 6) {
content += "<div><h3>" + a.Tittle + "</h3>";
var aStores = (from s in dc.Stores where s.areaid == a.id select s);
foreach(var s in aStores) {
content += "<p>" + s.Tittle + "</p>";
}
leftContent.InnerHtml = leftContent.InnerHtml + content;
} else {
content += "<div><h3>" + a.Tittle + "</h3>";
var aStores = (from s in dc.Stores where s.areaid == a.id select s);
foreach(var s in aStores) {
content += "<p>" + s.Tittle + "</p>";
}
rightContent.InnerHtml = rightContent.InnerHtml + content;
}
}
I guess you want to reset the added content in each iteration of the loop:
...
foreach(var a in areas) {
string content = "";
if (a.id >= 1 && a.id <= 6) {
...
}
}
....
Otherwise the content is added several times.

Error Using Decimal And Bool

In my syntax below I get an error of
An exception of type 'System.FormatException' occured in mscorlib.dll but was not handled in ther user code
Additional Information: Input string was not in a correct format
And the value being passed that throws the error is 9.7000
And this is my syntax - I have a comment above the line that throws an error
private void calculate()
{
var qtys = val(dropdownlist.SelectedItem.Text) + "|" + val(dropdownlist1.SelectedItem.Text) + "|" + val(dropdownlist2.SelectedItem.Text) ;
var totalitems = dropdownitem.SelectedItem.Text + "|" + dropdownitem1.SelectedItem.Text + "|" + dropdownitem2.SelectedItem.Text + "|" + dropdownitem3.SelectedItem.Text;
var amounts = dropdownamt.SelectedItem.Value + "|" + dropdownamt1.SelectedItem.Value + "|" + dropdownamt2.SelectedItem.Value + "|" + dropdownamt3.SelectedItem.Value;
var totalitems = itemInfo.Split('|');
var qtys = qty.Split('|');
var amounts = amount.Split('|');
for (int i = 0; i < totalitems.Count(); i++)
{
if (totalitems[i] != "" && qtys[i] != "" && qtys[i] != "0")
{
TotalPrice += Math.Round(val(amounts[i]), 2);
TotalTax += Math.Round(val(amounts[i]) * Convert.ToDecimal(0.07), 2);
}
}
}
private decimal val(string p)
{
if (p == null)
return 0;
else if (p == "")
return 0;
//The value of p = 9.7000
return Convert.ToInt16(p);
}
You're trying to return a decimal but calling Convert.ToInt16 try Convert.ToDecimal
Also I would recommend using decimal.TryParse rather than calling Convert directly with some code like this:
public decimal convert(string p)
{
decimal result;
if(decimal.TryParse(p, out result))
return result;
else
return 0;
}

could not translate expression into sql

With the below code I get this error
InvalidOperationException:
Could not translate expression
stok_getmiktar_byyapilanislem(urunler.ID,
"SATIS") into SQL and could not treat
it as a local expression.
var urundb = (from urunler in db.TBLP1URUNs
orderby urunler.SERIAL
where (urunler.ID.Contains(FilitreText) || urunler.URUNADI.Contains(FilitreText))
&& ((kategori1.SERIAL == null) || urunler.SERIAL.StartsWith(kategori1.SERIAL))
select new
{
UrunNo = urunler.ID,
UrunAdi = urunler.URUNADI,
Marka = urunler.MARKA,
SatisFiyati = urunler.SATFIYAT1.GetValueOrDefault(0),//.ToString() + " " + urunler.SATFIYAT1BIRIM,
TedarikFiyati = urunler.TEDFIYAT1.GetValueOrDefault(0),//.ToString() + " " + urunler.TEDFIYAT1BIRIM,
PiyasaFiyati = urunler.SATFIYAT2.GetValueOrDefault(0),//.ToString() + " " + urunler.SATFIYAT2BIRIM,
Hizmet = (urunler.HIZMETYENSURYIL > 0 && urunler.HIZMETYENSURAY > 0 ?
urunler.HIZMETYENSURYIL.ToString() + " Yıl " + urunler.HIZMETYENSURAY.ToString() + " Ay"
: (urunler.HIZMETYENSURYIL > 0 && urunler.HIZMETYENSURAY <= 0 ?
urunler.HIZMETYENSURYIL.ToString() + " Yıl"
: (urunler.HIZMETYENSURYIL <= 0 && urunler.HIZMETYENSURAY > 0 ?
urunler.HIZMETYENSURAY.ToString() + " Ay"
: ""))),
TedarikEdilenMiktar = DAOUrun.stok_getmiktar_byyapilanislem(urunler.ID, "TEDARIK"),//this one works fine
SatilanMiktar = DAOUrun.stok_getmiktar_byyapilanislem(urunler.ID, "SATIS"),//this one works fine
Stok = urunler.TEDARIKCISTOKMIKTAR.GetValueOrDefault(0)
- DAOUrun.stok_getmiktar_byyapilanislem(urunler.ID, "SATIS")//error occurs with this one
});
below is stok_getmiktar_byyapilanislem
public static double stok_getmiktar_byyapilanislem(string urun_id, string yapilanislem)
{
VeriyazDBDataContext db = new VeriyazDBDataContext(); db.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
double miktar = (from rows in db.TBLP1ISLEMDETAYs
where rows.URUN_ID == urun_id && rows.TBLP1ISLEM.YAPILANISLEM == yapilanislem
select rows.MIKTAR).Sum().GetValueOrDefault(0);
return miktar;
}
How can I solve it?
For those who are or who may be confronted with and suffered from this error
check Custom Method in LINQ to SQL query and Calling functions in LINQ queries
they say we cannot call our own functions/methods in linq, actually we can but when trying to get something from that query it throws the referred exception in my question.What I really wonder is why this one
SatilanMiktar = DAOUrun.stok_getmiktar_byyapilanislem(urunler.ID, "SATIS"),
works but this one doesn't
Stok = urunler.TEDARIKCISTOKMIKTAR.GetValueOrDefault(0) - DAOUrun.stok_getmiktar_byyapilanislem(urunler.ID, "SATIS")
I just wrote some other method like this:
public static double GetMevcutMiktar(string urunId,double acilisMiktari)
{
double mevcutMiktar = 0;
mevcutMiktar = acilisMiktari - stok_getmiktar_byyapilanislem(urunId, "SATIS")
return mevcutMiktar;
}
And called it instead of
urunler.TEDARIKCISTOKMIKTAR.GetValueOrDefault(0) - DAOUrun.stok_getmiktar_byyapilanislem(urunler.ID, "SATIS")

Categories

Resources