|
Hi
I use this code to add the values to the parameters.
cmd.Parameters.AddWithValue("@n", this.Name_textBox.Text);
cmd.Parameters.AddWithValue("@a", this.Address_textBox.Text);
cmd.Parameters.AddWithValue("@p", this.Phone_textBox.Text);
cmd.Parameters.AddWithValue("@em", this.Email_textBox.Text);
cmd.Parameters.AddWithValue("@d", this.Date_textBox.Text);
cmd.Parameters.Add(new OleDbParameter("@don", OleDbType.Boolean));
cmd.Parameters.Add(new OleDbParameter("@r", OleDbType.Boolean));
cmd.Parameters.Add(new OleDbParameter("@war", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@IT", this.Type_comboBox.Text);
cmd.Parameters.AddWithValue("@m", this.Make_textBox.Text);
cmd.Parameters.AddWithValue("@MO", this.Model_textBox.Text);
cmd.Parameters.AddWithValue("@s", this.Serial_textBox.Text);
cmd.Parameters.AddWithValue("@RF", this.ReportedFault_textBox.Text);
cmd.Parameters.AddWithValue("@i", this.RepairedBy_textBox.Text);
cmd.Parameters.AddWithValue("@FT", this.FaultType_comboBox.Text);
cmd.Parameters.Add(new OleDbParameter("@Rep", OleDbType.Boolean));
cmd.Parameters.Add(new OleDbParameter("@BER", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@FF", this.FaultFound_textBox.Text);
if (DateTested_textBox.Text != "")
{
cmd.Parameters.AddWithValue("@DT", this.DateTested_textBox.Text);
cmd.Parameters.AddWithValue("@DueD", this.DueDate_textBox.Text);
cmd.Parameters.AddWithValue("@TestedBy", this.TestedBy_textBox.Text);
cmd.Parameters.Add(new OleDbParameter("@CPass", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@CR", this.Conductor_textBox.Text);
cmd.Parameters.Add(new OleDbParameter("@InRes", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@IR", this.Insulator_textBox.Text);
cmd.Parameters.Add(new OleDbParameter("@EarthL", OleDbType.Boolean));
cmd.Parameters.AddWithValue("@ER", this.Earth_textBox.Text);
}
else
{
cmd.Parameters.AddWithValue("@DT", "");
cmd.Parameters.AddWithValue("@DueD", "");
cmd.Parameters.AddWithValue("@TestedBy", "");
cmd.Parameters.AddWithValue("@CPass", false);
cmd.Parameters.AddWithValue("@CR", "");
cmd.Parameters.AddWithValue("@InRes", false);
cmd.Parameters.AddWithValue("@IR", "");
cmd.Parameters.AddWithValue("@EarthL", false);
cmd.Parameters.AddWithValue("@ER", "");
}
I have changed the checkbox lines from
cmd.Parameter.AddWithValue("@r, this.Repair_checkbox.checked); to
cmd.Parameters.Add(new OleDbParameter("@r", OleDbType.Boolean));
The Bool variable r gets it's value with the line
bool r = Repair_checkBox.Checked;
However changing the line gives me an error saying "Failed Due to Parameter r has no default value"
The only checkbox line before the r variable is don and the corresponding checkbox is ticked, which is why I think it doesn't error.
I am only learning C# so the database will never be used. Therefore I am concentrating on getting it working rather than security, Which to me would make it even harder to understand.
|
|
|
|
|
Member 13261084 wrote: cmd.Parameters.Add(new OleDbParameter("@r", OleDbType.Boolean)); That adds and creates a parameter, but doesn't assign a value for it yet. That's why it is complaining about the default value; it has no value assigned.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
How do I go about assigning a value to it? I tried doing "r= Repair_checkBox.Checked;" before and after the line and still get the same error.
|
|
|
|
|
Member 13261084 wrote: How do I go about assigning a value to it? Something like below should work;
OleDbParameter op = new OleDbParameter("@r", OleDbType.Boolean);
op.Value = Repair_checkBox.Checked;
cmd.Parameters.Add(op);
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Thanks for that but I get the same error.
However if I change the line
cmd.Parameters.Add(op); to
cmd.Parameters.Add(op.Value); I get the error message "Failed due to The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not Boolean objects"
stepping through the code op.Value equals false yet it seems to be treating it as if it is blank.
|
|
|
|
|
Member 13261084 wrote: Thanks for that but I get the same error. Post the full exception. Is it pointing to the same line?
Member 13261084 wrote: I get the error message "Failed due to The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not Boolean objects" Exactly as it says; you add a OleDbParameter object to cmd.Parameters. The .Value should be pointing to the boolean.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hi I changed the code back to what you suggested.
I'm not sure how to post the full exception but the error happens when it tries to execute the query
cmd.ExecuteNonQuery();
as soon as it tries to execute this line it jumps to the catch statement which runs a messagebox that gives the exception message. I can do the exception.ToString and screen print it, but I cant find a way to upload the image on this site and it is quite large. However the exception code that I get is 0x80040E10 the rest of the text is, the line after the exception message talks about error handling, next line is talking about the Parameters and not being able to get a result, this is followed by a line talking about behavior followed by a line that talks about ExecuteReaderInternal at ExecuteNonQuery
The exception message that I get is "Parameter r has no default value"
however in the locals window it shows "r false bool"
|
|
|
|
|
Hi I have sorted it. The mistake was that deleted a field from the query yesterday when building it because I was having trouble with the where part of the statement. I didn't realise that I had to have that field in the query since I was calling the fields by name. Any way with that field put back in and your code, it is all working
|
|
|
|
|
Good to hear
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hi,
0. Welcome to (C#) programming.
1. Your piecemeal reporting makes it very difficult to get an overall picture of what is going on, I had to copy each of your messages into a single file to understand and check it somewhat...
2. MessageBoxes aren't very good for debugging, I much prefer logging (to a file).
You can read about it here: Article: A simple logging scheme[^] .
When you write an Exception.ToString() result to a file, you can look at it at your leasure; and you can copy and paste all or parts of it here.
3. From Google "0x80040e10":
OleDbException (0x80040e10): No value given for one or more required parameters.
4. Some databases know about booleans, and allow them to have either two (true/false) or three (true/false/unknown) values. This is a probable cause for problems.
My guess: it would probably be very wise to tell your database what default value you want (either true or false, not unknown/null).
This may be sufficient for your code to start working (I mean without exception, not necessarily correctly).
5.
You are probably right that a boolean is the culprit, and your current attempt uses five boolean fields; fixing one won't help a bit. If something is wrong with one of them, the same probably applies to all of them. Hence my urgent advice:
- create a small table with only one number and one bool;
- use it to get successful INSERT and UPDATE operations, so you find out how to do that;
- only then attempt your complex table with real fields and data.
In general: try walking before running.
6. your subject line refers to the update query, however your messages seem to discuss inserts all the time??
[ADDED LATER]
7. IMO you need an explicit three-line approach, something like:
OleDbParameter parameter = new OleDbParameter("@r", OleDbType.Boolean);
parameter.Value = myCheckBox.Checked;
cmd.Parameters.Add(parameter);
Good luck,
modified 23-Jul-19 14:13pm.
|
|
|
|
|
I know partial classes were primarily introduced to facilitate code generation but I use them on those occasions when I have to include a lot of tightly coupled, but loosely feature related items to a class.
Like, for my Context Free Grammar class (always referred to as a CFG in compsci circles) i have
Cfg.cs
Cfg.Validation.cs
Cfg.Computation.cs
Cfg.Symbols.cs
etc.
each one being partial class Cfg
Am I crazy for doing this?
Normally I like to spread features across several different classes but sometimes it just doesn't make sense to do so. This is one of those cases.
I could use regions to accomplish some of the source segregation but I find partial classes make moving from section to section easier in devstudio
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
codewitch honey crisis wrote: Am I crazy for doing this? Not for this
I think whatever organizational scheme that works for you is great; using ReSharper makes moving a selection of whatever into a new partial Class a single menu action + selecting (via CheckBoxes) which items in the selection you want to move + optionally renaming the new partial Class.
Of course, you do end up with multiple files ... the presence of which are known to tempt demons.
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Oh I'm quite mad.
But yeah, this way of organizing has worked well for me since I started doing it.
The thing about the multiple files is I have a merge utility that integrates with VS that will package source code for me, so I can get it in one file if I want it.
I have a core source library that covers everything from MIDI to webserving to threading. It's just things I've built over the years that are useful and I use this tool to "export" bits of it in manageable chunks.
Maybe I should release that here sometime.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
codewitch honey crisis wrote: ... merge utility that integrates with VS that will package source code for me ... a core source library that covers everything from MIDI to webserving to threading. ... I use this tool to "export" bits of it in manageable chunks.
Maybe I should release that here sometime. Good idea !
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
I've held off on it mostly because it goes against MS coding "best practices" but they seem to think everything should be its own DLL/assembly.
I don't operate that way. I like my smaller apps to be as self-contained and as simply packaged as possible so I jump through hoops to make it the way i like it.
I tested the waters here with the idea and it seemed like the tide was against me on this.
So if i publish i expect wars over it to ensue.
hah. maybe more reason to do it.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
I think Microsoft's built in object templates (especially the new class template) should default to public partial , because that's what is used most.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
That question is not as easy as it looks like.
Normally, a class should care about one (and exactly only one) thing.
But still, there may be some aspects differing from the actual purpose of the class.
I use partial classes typically for the purposes of configuration (checking that the configuration is consistent, and that all mandatory dependencies were injected) and another partial class for test accessors (i.e. special functions for setting up that class with a specific state for test purposes, accessing non-public members as reporters for test purposes etc.).
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
I generally like to keep classes tightly factored like that as well, but sometimes, particularly with non-trivial engine style things, you're going to probably have something that's got a lot of tightly coupled functionality that is nevertheless loosely related and your choices are to break encapsulation, greatly expand the surface area of your interconnected class APIs or encase the functionality all in one class. The latter leads to a much smaller test matrix in cases where it's appropriate.
There's a time and a place for breaking just about every rule of thumb. =)
But that being said, I take your point. In most circumstances we wouldn't disagree, and I'm not sure we would in these cases either.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
codewitch honey crisis wrote: greatly expand the surface area of your interconnected class APIs That's another aspect: the Facade Pattern. Seems appropriate to distribute the "facade" class into some partial classes.
True, I use partial classes also with the Decorator Pattern, distinguishing between the "decorated" methods and those which are called without any changes.
There are some practical uses for partial classes.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Yeah I could see doing that. I don't do a lot of injection and decoration these days because I'm out of the field and mainly researching parsing and compiling which doesn't really require a lot of that.
Still it makes a lot of sense to me to factor orthogonal functionality that way. It's an interesting approach.
Primarily I use them for augmenting classes created with code generation, which as I understand it is what they were primarily designed for, but I really like them in general as another tool in my factoring toolbelt.
I use one on my ParseContext class as well to make a "tear-off" with a bunch of helper functions that aren't necessary for the core functionality but are provided as a convenience.
Partial classes are neat. =)
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
How to write a method in C# that returns String as data type, no matter what datatype we have on excel spread sheet ?
modified 19-Jul-19 16:22pm.
|
|
|
|
|
I seem to have missed the question in your post. Would you care to clarify that?
|
|
|
|
|
Currently, readCell method in ExcelReader only reads String data. I have to Make it more dynamic so it can accept different datatypes like int, double, String, etc..
|
|
|
|
|
You already said that. Repeating it doesn't help.
I'm going to make some assumptions here. A method can only have one return type, so you're either going to have to make a method for each type you want to support, or write a "generic" version that accepts a type parameter and returns the data as the type specified, IF it can be cast to that type.
The problem with this is that your code has to know, or assume, the correct type for the field you're reading ahead of time.
|
|
|
|
|
Actually, the OP did not repeat the first post which had zero content: it's now clear he wants more than just Type 'String.
I upvote your first response for its salutary emetic effect on the OP
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|