|
Apologies for the shouting but this is important.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid..
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
For those new to message boards please try to follow a few simple rules when posting your question.- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode "<" (and other HTML) characters when pasting" checkbox before pasting anything inside the PRE block, and make sure "Use HTML in this post" check box is checked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question into an unrelated forum such as the lounge. It will be deleted. Likewise, do not post the same question in more than one forum.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
namespace ATAS.Indicators.Technical
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using ATAS.Indicators.Technical.Properties;
using OFT.Attributes;
[DisplayName("5.Heiken Ashi Z")]
[Category("# 1234")]
public class HeikenAshiz : Indicator
{
private readonly CandleDataSeries _candles = new CandleDataSeries("Heiken Ashi Z"){ Visible = true };
private readonly PaintbarsDataSeries _bars= new PaintbarsDataSeries("Bars Z"){ Visible = false };
public HeikenAshiz()
{
Panel = IndicatorDataProvider.NewPanel;
DataSeries[0]= _bars;
DataSeries.Add(_candles);
}
protected override void OnCalculate(int bar, decimal value)
{
var candle = GetCandle(bar);
_bars[bar] = Colors.Transparent;
if (bar == 0)
{
_candles[bar] = new Candle()
{
Close = candle.Close,
High = candle.High,
Low = candle.Low,
Open = candle.Open
};
}
else
{
var prevCandle = _candles[bar - 1];
_candles[bar] = new Candle()
{
Close = (candle.Open+candle.Close+candle.High+candle.Low)*0.25m,
High = candle.High,
Low = candle.Low,
Open = (prevCandle.Open+ prevCandle.Close)*0.5m,
};
}
}
protected override void OnApplyDefaultColors()
{
if (ChartInfo is null)
return;
_candles.UpCandleColor = Color.FromRgb(14, 203, 129);
_candles.DownCandleColor = Color.FromRgb(246, 70, 93);
_candles.BorderColor = Color.FromRgb(127, 127, 127);
}
}
}
modified 11hrs 5mins ago.
|
|
|
|
|
I have the below function which is called with a button click. It is supposed to chart from a database. The problem is that when selecting from database I am filtering the database using "WHERE". When I don't use this "WHERE" and plot the whole database table, the code works fine, but when I use this filter, it often leads to blank charts even though the data is there in the table. What is wrong with this code?
void PlotChart()
{
try
{
chart1.Series.Clear();
Series First = new Series("Series11");
Series Second = new Series("Series12");
First.ChartType = SeriesChartType.Spline;
Second.ChartType = SeriesChartType.Spline;
First.Color = Color.Blue;
Second.Color = Color.Orange;
using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"))
{
if(connection.State == ConnectionState.Closed)
{
connection.Open();
}
string query = $"SELECT * FROM Data WHERE RawDataOrder = {Convert.ToInt32(label86.Text.Trim())}";
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int id = reader.GetInt32(0);
int value11 = reader.GetInt32(1);
int value12 = reader.GetInt32(8);
First.Points.AddXY(id, value11);
Second.Points.AddXY(id, value12);
}
}
}
}
chart1.Series.Add(First);
chart1.Series.Add(Second);
chart1.Titles.Clear();
chart1.Titles.Add(new Title("Channel 1"));
chart1.Update();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error PlotChart", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
|
|
|
|
|
without having your data it's difficult to say something because you've written "sometimes ... and sometimes not"
Have you ever checked with the Debugger wat kind of Data you get with yor Request which fails ? This would be my 1st approach to find out what happens ...
|
|
|
|
|
Ralf Meier wrote: Have you ever checked with the Debugger wat kind of Data you get with yor Request which fails ? This would be my 1st approach to find out what happens ...
How do I do this? If I run the debugger, how would it show the problem point? Where do I have to look? I am sort of new to this thing.
|
|
|
|
|
you could set Breakpoints inside your Code which stops the Code at this point. Now you can see which values your different Variables have (by hovering with the Mouse over them).
|
|
|
|
|
 I tried setting breakpoints, but then got an error which said something to the effect "break point won't break because some symbol isn't there". I couldn't do away with that error so I put some checks in the code itself, to make sure that the label86 was a proper integer, that there was actually data to be plotted etc, and I ran the new code. It never went to the coded error messages and still the charts were plotted blank. Is it possible the fault is somewhere in the chart settings? maybe the series become transparent or something? But why would this happen only when we use "Where" and try to plot a part of the table data?
void PlotChart()
{
try
{
if (!int.TryParse(label86.Text.Trim(), out int rawDataOrder))
{
MessageBox.Show("Invalid RawDataOrder value. Please enter a valid integer.", "Error PlotChart", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"))
{
using (SqlCommand command = new SqlCommand("SELECT * FROM SerialDataNIBP WHERE RawDataOrder = @RawDataOrder", connection))
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
command.Parameters.AddWithValue("@RawDataOrder", rawDataOrder);
using (SqlDataReader reader = command.ExecuteReader())
{
if (!reader.HasRows)
{
MessageBox.Show("No data found for the given RawDataOrder.", "Error PlotChart", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
chart1.Series.Clear();
Series PulseCh1 = new Series("Series11");
Series PressureCh1 = new Series("Series12");
PulseCh1.ChartType = SeriesChartType.Spline;
PressureCh1.ChartType = SeriesChartType.Spline;
PulseCh1.Color = Color.Blue;
PressureCh1.Color = Color.Orange;
while (reader.Read())
{
var id = reader.GetInt32(0);
var value11 = reader.GetInt32(1);
var value12 = reader.GetInt32(8);
PulseCh1.Points.AddXY(id, value11);
PressureCh1.Points.AddXY(id, value12);
}
chart1.Series.Add(PulseCh1);
chart1.Series.Add(PressureCh1);
chart1.Titles.Clear();
chart1.Titles.Add(new Title("Channel 1"));
chart1.Update();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error PlotChart", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
|
|
|
|
|
You can only set a Breakpoint to a code-line.
I would in every case look with the Debugger - it's much better than a guesswork - but it's your decission.
I don't believe that the fault is inside the Chart-Settings - but perhaps inside the data you read from the table. Perhaps you don't get any data from it because your request isn't correct ...
|
|
|
|
|
Iskander12345 wrote:
string query = $"SELECT * FROM Data WHERE RawDataOrder = {Convert.ToInt32(label86.Text.Trim())}"; Whilst in this specific instance you're probably safe, this sample suggests you're writing code which would be vulnerable to SQL Injection[^].
And even in this case, your code will result in query plan cache pollution - every value for the parameter will result in a different plan being compiled and stored.
Rather than trying to work out whether your values are "safe" to inject into the query, adopt a simple strategy: always use parameters.
const string query = "SELECT * FROM Data WHERE RawDataOrder = @RawDataOrder";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@RawDataOrder", Convert.ToInt32(label86.Text.Trim()));
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
string query = $"SELECT * FROM Data WHERE RawDataOrder = {Convert.ToInt32(label86.Text.Trim())}";
You are trying to convert a string to an integer, just so it can be immediately converted back into a string. So remove the Convert.ToInt32 part. It is also not a good ide to use Convert as it will fail (maybe silently) if the text is not a pure number. Better to use Int32.TryParse .
|
|
|
|
|
tried both the suggestions above, it didn't work.
|
|
|
|
|
I did not say they would necessarily work, but they are things you need to consider when writing code. As to why your chart does not get created, there is only one way to make progress: debugging. You need to use the debugger to step through the code so you can see exactly what values are in all your variables as the code proceeds. Repeatedly trying random changes without understanding what is actually happening is just a waste of your time.
|
|
|
|
|
The way you've written your SQL code, specifically the parameter you put in the query string, makes it impossible to debug. It's not the existence of the WHERE clause that's the problem. It's that your WHERE clause condition doesn't match any records.
To be able to debug this, you have to rewrite the code so you can see what's going on with the debugger:
string query = "SELECT * FROM Data WHERE RawDataOrder = @orderId";
int orderId;
if (int.TryParse(label86.Text.Trim(), out orderId))
{
using (SqlConnection conn = new SqlConnection("... connection string ..."))
{
using (SqlCommand comm = new SqlCommand(query, conn))
{
SqlParameter orderIdParam = new SqlParameter("@orderId", SqlDbType.Int);
orderIdParam.Value = orderId;
comm.Parameters.Add(orderIdParam);
conn.Open();
using (SqlDataReader reader = comm.ExecuteReader())
{
while (reader.Read())
{
...
}
}
}
}
}
else
{
}
|
|
|
|
|
This is the correct answer, OP stated that when he runs the query without the WHERE clause, it displays the data fine, when using the WHERE clause it show blanks which leads me to believe that no record was returned. That then leads to the reason why nothing was returned - error must lie in the -
Quote: int.TryParse(label86.Text.Trim() part.
As per Dave's solution, first define the value - 'orderId' as a proper value which we know will return a record, then us that value in the sql select statement.
|
|
|
|
|
Iskander12345 wrote: but when I use this filter, it often leads to blank chart
That is your specific problem.
Presumably you do not see the MessageBox for the exception ever.
I doubt the suggestion that 'Convert.ToInt32' is relevant. If it was throwing an exception you would see it. It might however return zero which leads to the next problem. It might return zero because that is a valid value though also.
Your code assumes all of the following
- That there are rows of any sort that are returned.
- That the data in those rows is 'valid' (see above.) But also is zero a valid value?
So what happens for the following
1. There are no rows?
2. The data returned is null. Which means id, value1, value2 will be zero?
I am not saying that is what is happening but rather if does that you are not going to get anything. So you need to check for that.
Perhaps as a minimum you should check the following
1. If there are no rows returned post (new MessageBox) and error about that. You should probably also post 'RawDataOrder'
2. Check that at least one (maybe two) rows exist and that both have non-zero data. If not post a different MessageBox error.
|
|
|
|
|
(1) "Hard code" a "where query". When that works, make a "parm" version. At least you'll have a better idea of where the problem is.
(2)
Try {
MyStuff();
} catch (etc) {
... also works.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
I just discovered the problem. It appears that the plotting of SQL ID on the x axi was encountering occasional problem depending on what the IDs were, so I set the plots to just have a sequential number starting from 0 and that works.
|
|
|
|
|
im a newbie to coding,,how can i get highest for the below code
private decimal Lowest(int bar, ValueDataSeries src, int length)
{
if (bar < length)
return (0.0m);
decimal res = decimal.MaxValue;
for (int index = bar; index >= bar - (length - 1); index--)
{
if (src[index] < res)
res = src[index];
}
return (res);
}
modified 30-Nov-23 3:08am.
|
|
|
|
|
Have you tried stepping through this code with a debugger so you know what each line is doing? The debugger will allow you to execute one line at the time and inspect all variable values as you do so.
When you are new to coding, debugging might seem to be an advanced topic you will learn later. This is a huge mistake. debugging is simple:
Tutorial: Debug C# code - Visual Studio (Windows) | Microsoft Learn[^] (if you do not use Visual Studio Google "tutorial debugging c# with xxxx" where xxxx is whatever you use to write code.
No matter if you are a professional or a beginner, the debugger is the tool that will save you the most time by far.
|
|
|
|
|
|
I upvoted the question because I cannot see any reason why anyone would down vote it.
|
|
|
|
|
One way to do this is to modify the 'Lowest' method you have used to find the highest value by initializing res to 'decimal.MinValue' and then checking if 'src[index]' is greater than 'res' -
private decimal Highest(int bar, ValueDataSeries src, int length)
{
if (bar < length)
return 0.0m;
decimal res = decimal.MinValue;
for (int index = bar; index >= bar - (length - 1); index--)
{
if (src[index] > res)
res = src[index];
}
return res;
}
|
|
|
|
|
it worked perfectly , thank you
|
|
|
|
|
|