|
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
|
|
|
|
|
|
Hello & Salutations to Everyone, using: Win Forms .NET 4.7.2
I have a set of Form buttons named button1, button2, button3,………
I have an array of strings BUTTON_LIST_str[] =” button1”, “button2”, “button3”,…
I have a function:
private void APPLY_BUTTON_CFG(Button b)
{ …………… }
What I need to do is loop through BUTTON_LIST_str[] and pass each button object to APPLY_BUTTON_CFG(Button b).
Basically for every string I need to use for pointing to a different type form object or its property of object, I get the error "String cannot be converted to the object required type/format (System.Form......)
I have searched every convoluted order of words for hours and cannot find a solution that matched my scenario of converting strings to all form object types (or specific ones either). With so many unique object specific variables how does one research the solution to convert a string to each type for each case?
Is there a ConvertToType universal function that solves this issue.
I also ran into same problem with:
FlatStyle P_STYLE = “Flat”;
Button1.FlatStyle = P_STYLE or FlatStyle.P_STYLE ; // error cannot convert string to…………..FlatStyle object type
I understand that a string name cannot point to a form object OOP when passing to a function. I’m not sure I can ask my question competently………….
With so many unique object specific variables how does one research the string conversion solution for each case?
Simply put, how would I research and find the solution for these types of problems:
Button1.FlatStyle = How to convert string to FlatStyle object reference, or Font, TextAlign when an error occurs?
I did find this translator for colors but not FlatStyle though or for any other applicable parameters.
P_COLOR = System.Drawing.ColorTranslator.FromHtml("White"); //"White" will be replaced with a string array element CFG_PARAM_PARSED[2]);
Button1.ForeColor = P_COLOR;
This is the real kicker: Button name as string (String_Array[x]) needs conversion (typecast?) to object name reference
var ButtonObject = String_Array[x]; or Button ButtonObject = String_Array[x];
APPLY_BUTTON_CFG(ButtonObject);
private void APPLY_BUTTON_CFG(Button b)
{
……………
}
Thank You So Much for your time and assistance…………………….
|
|
|
|
|
You would be much better off adding the controls themselves to the array, not the names of them. Those are bad control names by the way.
Why? Because if you go back and change the names of the controls to something sensible and debuggable, you need have to go through the code and find the control names you stuck in arrays and change those too.
Did you Google for "C# Windows Forms find control by name[^]"? There's plenty of results.
|
|
|
|
|
Hello Dave and Thank You very Much,
I understand and agree with you completely but I am taking an inverse approach due to my apps functionality. I am creating a SendKeys keyboard emulator which will have a pre compiled set of universal buttons all referenced by the simple button1-n name. I will have an excel config file with the button’s parameters (visibility, location, size, text, colors.... and key code) which will be loaded for each application I want to keyboard emulate. Instead of having to have multiple form layouts and keycodes for each interface application I will have a simple excel file to copy cut and paste parameters.
So from my excel config I can set number and string parameters but I can't store and retrieve object oriented parameters (def?), I have to store & retrieve them as strings.
The problem that hangs me up is when I have a parameter such as FlatStyle with has arguments of Standard, Flat, PopUp... that are not strings but some type of object property. So when I need to retrieve PopUp as a string I need to convert it to a type of object property.
var TypeOfStyleObject = convert-this-string-to-what-FlatStyle-wants("PopUp"); // PopUp will actually be a StringArray[x]
button1.FlatStyle = TypeOfStyleObject;
I studied the link you pointed out and it solved my problem, with an exception. I fetched the name of the CONTROL (not a specific control like Button) and changed my function input from BUTTON to CONTROL. It passed without error but I did find a catch....
In the function that I apply the configuration parameters to the generic buttons I successfully applied Text, BackColor, ForeColor as a quick test. When I was passing the Button control I was setting FlatStyle but getting a string to object error (above). Now that I am passing the Control control it is saying my "Control" does not have a parameter FlatStyle. I am interpreting this way..... the Control lets me work without specifying the type of object(button, label, RTBox) but I may lose access to some parameters??? Accurate??
I may need to still find a way of passing specific controls instead of generic control object so I don’t lose configurability. I could follow your lead of preloading a Button[] object array and trying see if I can use its index as an object.
Am going anywhere but backwards or circles…… I assuming there has to be some methods that allows you to convert-this-string-to-what-AnyObject-wants() so objects can be acted on in loops and sequences. You have to be able to store & retrieve non numeric config info (strings) that represent object.string.parameterss!!!!! read them & convert to string, save, retrieve, convert string back to object.
Thank You Again………….
|
|
|
|
|
If you look at the documentation for all these features you will see that they invariably have a numeric value which you can pass to the constructor, or set dynamically.
|
|
|
|
|
Your control is (still) also a button, and you can test for that using "is" and "as", and switch between them.
https://stackoverflow.com/questions/3786361/difference-between-is-and-as-keyword
"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
|
|
|
|
|
Thank You to Everyone ,
Your time is valuable and I appreciate it…………
I don’t comprehend ‘yet’ why when I pass a Button reference, I have a parameter to modify but when I pass it as a Control I’m told this parameter is no longer available, at the top level. I do understand there are other hooks into the control I haven’t learned yet.
The reality of the matter is I stepped on a puddle and found out it was a pot hole, I’m sailing in unfamiliar waters. I am a self-taught C#’er with a background from Verlog FPGA and C firmware with no one local to tutor me. I started writing GUI’s in order to aid and test FPGA and firmware code development plus the automation of production testing.
I’ve gotten away so far by walking around the ‘object mountain’ and I’ve reached a point I need to climb it. I have a strong background in state machine development and implementation, I just need to up the game on ‘objects’. I’m pretty good at making a big snow ball if I can figure out which direction to roll down the hill………..
As can been seen from my post I lack the vocabulary………… I was searching for Button when I should have been researching Controls. I fully comprehend the feedback I am getting but I’m lost in the implementation. Not having a full vocabulary in object language can you point me to a youtube series that would be a good launching point for me at my current level of comprehension. I find a free-flowing mind on video conveys more implementation guidance than a rigid text of facts.
A finger pointing in the right direction is all I really need…………..
Thank You Again for your time and help…….
|
|
|
|
|
Everything in c# (.NET) is an "object".
You then add dressing (a class) and turn it into something else. Add another class and make a composite of what came before. But it is still an "object" and also whatever comes after; you're just putting on and taking off diffferent coats. That's "inheritance" ... one of the "3 pillars" of OOP.
That, and understanding "value types" versus "reference types" ("pointers" to "objects" or functions) allow you to model just about anything in software.
"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
|
|
|
|
|
Is that the essence of “wrapping”?? I have seen that term with a few libraries I have fiddled with from GitHub.
“understanding "value types" versus "reference types" ("pointers" to "objects" or functions)”
That sounds like the perfect search phrase to get me started………….
Thanks Again…………..
|
|
|
|
|
"Wrapping" (which is a "design pattern") is intentionally "hiding" one object by wrapping it with another in order to protect or facilitate certain features; not the same as "inheriting" which "adds" functionality.
"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
|
|
|
|
|
Sounds a lot like a decorator pattern. Do you despise it? I mean, I do like adding functionality by using a decorator, and the .NET framework apparently too, as there are a lot of places where we use them.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I said a wrapper can hide or "facilitate"; which is a good thing. I wrapped a C++ "animation" engine in a FoxPro dll so I could generate 3rd graphics using FoxPro. I did it to earn a license that normally cost $8,000. (A "long" time ago). I could create and race a buggy over 3D terrain ... The ultimate plan was to use the graphics engine in my "rail car repair system", but that's another track.
"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
|
|
|
|
|
A "decorator" is usually there to add functionality?
Gerry Schmitz wrote: I could create and race a buggy over 3D terrain Now there is some code I'd like to read and learn from
I'm really bad at making games, I do forms and databases.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I'm sure, with a bit of lateral thinking, you could make a game out of form filling.
|
|
|
|
|