|
The SQL is wrong. When I execute the SQL to the database, no result will show. I will try to attach the picture for you
|
|
|
|
|
Syafiqah Zahirah wrote: TempInTextBox.Text = dr.GetValue(0).ToString();
TempOutTextBox.Text = dr.GetValue(1).ToString();
HumidTextBox.Text = dr.GetValue(2).ToString();
Chart1.Series["TempIn"].Points.AddXY(DateTime.Now.ToShortTimeString(), Double.Parse(TempInTextBox.Text));
Chart1.Series["TempOut"].Points.AddXY(DateTime.Now.ToShortTimeString(), Double.Parse(TempOutTextBox.Text));
Chart1.Series["Humidity"].Points.AddXY(DateTime.Now.ToShortTimeString(), Double.Parse(HumidTextBox.Text));
Why do you send the values from the database to textboxes, and then parse them from the textboxes to numeric values again?
What about
double tempIn = dr.GetDouble(0);
Chart1.Series["TempIn"].Points.AddXY(DateTime.Now.ToShortTimeString(), tempIn);
And since you always use DateTime.Now on the x axis, I think you are missing something else. What about querying the DateTime value also from the database, and using it here instead of DateTime.Now?
|
|
|
|
|
Hye Bernhard! Tq for replying my message. I will try to change my coding referring in what u say. And one more thing, I actuallt didn't understand on what u try to explain to me with this sentence '. What about querying the DateTime value also from the database, and using it here instead of DateTime.Now?'. can you give some example? tq
Student that not familiar with C# code and try very hard to finish her final year project with flying colours. Hope that everyone out there can help me
|
|
|
|
|
I tried to retrieve the DateTime value from database,and plot it to the graph but it doesn't seems to work. This is my code:
while (dr.Read())
{
double tempIn = dr.GetDouble(0);
double tempOut = dr.GetDouble(1);
double humidity = dr.GetDouble(2);
double date = dr.GetDouble(3);
DateTime? value = (DateTime?)dr["DateT"];
Chart1.Series["TempIn"].Points.AddXY(value, tempIn);
Chart1.Series["TempOut"].Points.AddXY(value tempOut);
Chart1.Series["Humidity"].Points.AddXY(value, humidity);
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
|
|
|
|
|
using System;
using System.IO;
using System.Collections;
namespace Applica
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo da = new DirectoryInfo("C:\\Folder");
FileInfo[] Arr = da.GetFiles();
if (Arr.Length == 0)
{
throw new InvalidOperationException("No files found.");
}
FileInfo ap = Arr[Arr.Length - 1];
long Totbyte = ap.Length;
string filePath = ap.FullName;
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
}
}
}
|
|
|
|
|
The ReadAllBytes method does exactly that: it reads the entire file content into the memory and returns it as an array of bytes. If you are getting "Out of memory" errors, then there are two main possibilities:
1) The size of any one file exceeds 2GB. There is an absolute limit on any .NET object of 2GB, no item (and an array of bytes is one item) can exceed this.
2) These are large files, and as such they will go on the Large Object Heap - anything larger than 85KB is a "large object" and as such goes on the LOH - which is not compacted, it's objects are "connected" when they are garbage collected, and it's filling up...but that should trigger a GC which should empty it so it shouldn't cause a problem.
The bad news is...System.Array does not implement IDisposable so you can't free it yourself. You might be able to force it, by setting data to null and manually calling the GC, but that's a nasty solution.
I would suggest that your best approach might be to "block read" your files: 64K lumps for example so that any allocation problem is reduced.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
<pre>
using System;
using System.IO;
using System.Collections;
namespace Applica
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo da = new DirectoryInfo("C:\\Folder");
FileInfo[] Arr = da.GetFiles();
if (Arr.Length == 0)
{
throw new InvalidOperationException("No files found.");
}
FileInfo ap = Arr[Arr.Length - 1];
long Totbyte = ap.Length;
string filePath = ap.FullName;
string temPath = Path.GetTempFileName();
byte[] data = File.ReadAllBytes(filePath);
File.WriteAllBytes(temPath, data);
decimal[] arry = new decimal[Totbyte];
for (int count = 0; count < data.Length; count++)
{
arry[count] = data[count];
}
byte[] data2 = new byte[Totbyte];
for (int count = 0; count < arry.Length; count++)
{
data2[count] = (byte)arry[count];
}
string filePath2 = Path.Combine("C:\\check", Path.GetFileName(filePath));
File.WriteAllBytes(filePath2, data2);
data = File.ReadAllBytes(temPath);
data2 = File.ReadAllBytes(filePath);
}
}
}
|
|
|
|
|
¡Ay, caramba!
You do realize what that code does, don't you?
Not only do you allocate two arrays of bytes the same size as each file in the folder, you also allocate a third array 16 times larger for each file as well!
The decimal datatype is 128 bits wide - or 16 bytes!
So is any file in your folder is bigger than 134MB, you will exceed the 2GB maximum-object limit...and get "out of memory".
And frankly, that code doesn't make a whole lot of sense.
You read the file as bytes, copy each byte value to a decimal (which does nothing in practice), and then convert each decimal back to a byte (which will give you exactly what you started with!)
And then you write the data to a new place...
You could do the same much more easily with File.Copy...
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
See my comment below; I thought I recognised this code.
|
|
|
|
|
|
hello guys,
below is my sql select statement :
SELECT product.P_ID, Product.P_Name,Product.Leadtime, Product.SafetyStockamount,
Monthlysales.Month, Monthlysales.totalsalesamount, (totalsalesamount/30) as Averagedailysales, ((totalsalesamount/30) * Leadtime + SafetyStockamount) as reorderpoint
FROM Product, Monthlysales
where Product.P_ID = Monthlysales.P_ID
in product table there are product id, name, lead time, safety stock column, meanwhile in monthly sales table, there are productid, month (in int), and totalsalespermonth.
the problem is :
i want to retrieve the reorder point result from the select statement above in visual studio c#
the interface will include product id , month options, button and label to show the result.
the reorder point result will be based on the month, for example
if the user choose
pid 1 : 1
month options: 2 (february)
then label result will show the reorder point amount for pid 1 in february.
i also wondering whether i can directly calculate the reorder point result based on product id and its month in sql server and directly call it in c#.
how could i do it?? please help me
thank you
|
|
|
|
|
And?
What help do you need?
You know how to use SQL from C# I assume, or you wouldn't be writing complicated SQL...so what do you need help with?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Hi All,
In a tutorial I found a a new way (new for me)of updating a control from a thread.
In the example code the new way(AsyncParallel) is in button1_Click and what I am familiar with is in button2_Click.
They both do the job, but what is better, regarding performance and especially when more tasks are running that might update UI-thread?
Here is the example:
namespace Task_Compare
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string data = "";
Task Tf1 = Task.Factory.StartNew(() =>
{
data = GetData();
});
Task T2 = Tf1.ContinueWith((previousTask) =>
{
listBox1.Items.Insert(0, data);
}, TaskScheduler.FromCurrentSynchronizationContext());
}
private void button2_Click(object sender, EventArgs e)
{
string data = "";
Task Tf2 = Task.Factory.StartNew(() =>
{
data = GetData();
});
Tf2.Wait();
listBox1.Invoke(new EventHandler(delegate
{
listBox1.Items.Insert(0, data);
}));
}
private string GetData()
{
string temp = "";
{
temp = " Do something long running ......";
}
return temp;
}
}
}
Groover,
0200 A9 23
0202 8D 01 80
0205 00
|
|
|
|
|
GrooverFromHolland wrote: They both do the job, but what is better They do the same; which one is 'better' is nitpicking. If you want to pick the nits (which can be fun and educational), download ILspy and decompile the class.
The Invoke-pattern is older than the Task-class, which was added in 4 (?). To some, the newer way is "more readable" as it does the same with less code (=fewer bugs). Others will be used to the old pattern.
I prefer not to have a wait or an extra task, but to invoke the UI from the thread using the classical invoke-pattern from MSDN;
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using System.Threading.Tasks;
7
8 namespace ConsoleApplication2
9 {
10 class Program
11 {
12 static void Main()
13 {
14 System.Threading.Thread.CurrentThread.Name = "Main thread";
15 using (var f = new SomeForm())
16 f.ShowDialog();
17 }
18 }
19 class SomeDataCollection : List<Guid> { }
20 class SomeForm: Form
21 {
22 ListBox _listBox1;
23 Button _button1;
24 public SomeForm()
25 {
26 _listBox1 = new ListBox() { Dock = DockStyle.Fill };
27 _button1 = new Button() { Dock = DockStyle.Top };
28 Controls.AddRange(new Control[] { _listBox1, _button1 });
29
30 _button1.Click += delegate
31 {
32 Task Tf2 = Task.Factory.StartNew(() =>
33 {
34 SomeDataCollection _data = getData();
35 showData(_data);
36 });
37 };
38 }
39 void showData(IList<Guid> whatData)
40 {
41 if (InvokeRequired)
42 {
43 Invoke(new Action<SomeDataCollection>(showData), new[] { whatData });
44 return;
45 }
46
47 _listBox1.DataSource = whatData;
48 }
49 SomeDataCollection getData()
50 {
51 var result = new SomeDataCollection();
52 for (int i = 0; i < 10; i++)
53 {
54 result.Add(Guid.NewGuid());
55 System.Threading.Thread.Sleep(150);
56 }
57 return result;
58 }
59 }
60 }
I keep preferring the old style as it makes for very predictable code-constructs. I would however add that it's beneficial during debugging to name each thread during coding! Put a breakpoint on line 35, run, and when the debugger hits it, choose "view, threads" from the menu in the VS-IDE. Each thread/task that you launch can have a unique name - which may even include spaces
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
Thank You for the good explanation.
Regards,
Groover
0200 A9 23
0202 8D 01 80
0205 00
|
|
|
|
|
You're welcome
|
|
|
|
|
Hy @ everyone,
i have a Little Problem:
class Track
{
string Name;
string Artist;
string Album;
int SampleRate;
int BitRate;
public Track(string name, string artist, string album, int sampleRate, int bitRate)
{
Name = name;
Artist = artist;
Album = album;
SampleRate = sampleRate;
BitRate = bitRate;
}
}
Now i want to editing some values foreach Track:
List<Track> TracksOniTunes = new List<Track>();
foreach (Track item in TracksOniTunes)
{
}
Later, i have a Switch and I need depending on different values to compare from the List
switch (criteria)
{
case "":
break;
case "Qualität":
break;
case "Album":
break;
case "Compilation":
break;
}
My Problem is that i can't editing some items?
I can not access the objects.
Is there an easy way?
Thanks for help.
|
|
|
|
|
Simple. In the line:
List<Track> TracksOniTunes = new List<Track>();
You are creating a brand new empty List. So when you get to
foreach (Track item in TracksOniTunes)
there is nothing in your list. You have to put something in your list before you can change its properties.
|
|
|
|
|
Just Add some items to the list TracksOniTunes. Then u can edit the inner values.
|
|
|
|
|
Sorry, here is the line that add a Track to the list
for (int countForTrack = countTracks; countForTrack > 0; countForTrack--)
{
currentTrack = tracks[countTracks] as IITFileOrCDTrack;
countTracks--;
TracksOniTunes.Add(new Track(currentTrack.Name, currentTrack.Artist, currentTrack.Album, currentTrack.SampleRate, currentTrack.BitRate));
}
|
|
|
|
|
Okay... since you say that there are actually items in your List, the problem becomes more clear. In your foreach you need to say
item.SampleRate = 0; But to get to the variables, you either need to make your class level variables public or create public properties for your class exposing those variables to outside that class.
modified 10-May-14 20:30pm.
|
|
|
|
|
Member 10441939 wrote: My Problem is that i can't editing some items?
I can not access the objects. What specific error message do you get, and where in the code does the error occur ?
I note in your second reply you do not use the for-loop index count variable to access the current Track, but instead use and decrement another variable: this is not an error, but, why not use the for-loop index variable ?
“I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges
|
|
|
|
|
Change your fields to auto-properties like that;
public string Name { get; set; }
public string Artist{ get; set; }
public string Album { get; set; }
Tim Toady Bicarbonate
|
|
|
|
|
Hep me Please. Where are wrong.Please fix it
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Hastane_Randevu_Sistemi
{
public partial class Giris : Form
{
public Giris()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
RandevuAl frm = new RandevuAl();
frm.Show();
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
comboBox4.Text = "";
cmbbolum.Text = "";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
{
GetCountry();
}
}
private DataTable Connection(string commandType)
{
DataTable dataTable = new DataTable();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\vt.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand command = new SqlCommand(commandType);
command.Connection = con;
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dataTable);
con.Close();
return dataTable;
}
private void GetCountry()
{
DataTable dataTable = new DataTable();
dataTable = Connection("select * from IL");
comboBox1.DisplayMember = "COUNTRYNAME";
comboBox1.ValueMember = "GUID";
comboBox1.DataSource = dataTable;
}
private void GetCity(string CountryId)
{
DataTable dataTable = new DataTable();
dataTable = Connection("Select * from vt where IL ='" + CountryId + "'");
comboBox2.DisplayMember = "İlçe Adı";
comboBox2.ValueMember = "GUID";
comboBox2.DataSource = dataTable;
}
private void cboCountry_SelectionChangeCommitted(object sender, EventArgs e)
{
GetCity(comboBox1.SelectedValue.ToString());
}
}
private void Giris_Load(object sender, EventArgs e)
{
}
}
|
|
|
|
|
Please edit your first message, and remove this one. Re-posting is never a good idea.
You need to set break-points in your code, and then single-step (using F11 in Visual Studio) through the code. Identify the exact place the error occurs, and then edit your post to show (us) where that is.
Make an effort.
“I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges
|
|
|
|
|