|
To add to Pete's excellent advice, the best way to do this is to put the construction of all database related items (Connections, Commands, Adapters, ...) inside a using block. That way, when the item goes out of scope for whatever reason (even if there is a failure) the object is automatically closed and disposed.
Do that, and you will never have the problem again, as well as not hogging scarce resources.
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable", con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int id = (int) reader["Id"];
string desc = (string) reader["description"];
Console.WriteLine("ID: {0}\n {1}", id, desc);
}
}
}
}
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I almost didn't up-vote this due to your horrific brace indentation!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Philistine!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm with Richard on that; we Dicks need to stick together.
|
|
|
|
|
At this point in the conversation, I shall make my excuses, and leave ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Why? Are you braceophobic?
This space for rent
|
|
|
|
|
I think they may need some "private time".
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi
We have an old winforms project in c# .net framework 4.
The controls in forms of the project are not inside table layout panel.so when maximize the screens the all the controls are anchored at the left.now we are editing forms one by one using table layout panel.i just want to know is there any way before loading the form from menu can we dynamically store all controls to memory and create a table layout panel to form then is it possible to add controls back from memory to table layout panel of form? If you have any better ideas please share.the project has more than 200 forms in seperate module dlls😂.if there is no other way we continue one by one.
|
|
|
|
|
There is no built-in way to do that, nor do I even know of a tool that will do it.
AFAIK, if you want to do that with a tool, you're going to have to write it yourself.
|
|
|
|
|
Thanks for the info dave 👍🏻
|
|
|
|
|
Even if you write a tool to do so; it will never be able to guess the correct alignment.
Before changing that amount of forms, investigate "docking" too; layout is not limited to anchoring. The Ux Guide will have some hints on how and why
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.
|
|
|
|
|
There are some ways you could attempt this, but I think ... depending on the variation in the structure/placement/anchor/dock properties of Controls in the different Forms ... that there will be no easy solution.
1. at the point you create an instance of the Form;
a. you could traverse/iterate the top-level Controls of the Form, and add them to the TableLayoutPanel ... adding them will effectively remove them from being in the ControlCollection of the Form.
b. the hard-part is deciding where in the TBLP to put the Controls. I think I'd try creating a sorted Array of Controls in left-right/top-bottom order.
Complicating factors could include Control nesting, existing use of 'Dock, etc.
Before I could estimate the extent to which this re-org is able to be somewhat automated, I'd pick two or three different Forms, and experiment.
«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
|
|
|
|
|
hello guys,
How do I write something like this?
string json = null;
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
var carTypes = (from type in types
where type.IsClass && type == typeof(Car)
select type);
foreach(var carType in carTypes )
{
string typeName = carType .Name;
string jsonFile = typeName + ".json";
json = File.ReadAllText(jsonFile);
IList<Porche> cars = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Porche>>(json);
}
Here, Porche is a sub class of Car - Both "Porche" and "Car" are Class. In additional to Porche, we have Toyata, BMW, Tesla... I want to dynamically retrieve them via Reflection (avoid hardcoding)
"cars" is a list of instances of cars.
"carType" is Type.
How can I use "carType" in JsonConvert.DerserializeObject which take List<$CLASS$> as argument?
IList<someReflectionAPI(carType)> cars = Newtonsoft.Json.JsonConvert.DeserializeObject<list<<b>someReflectionAPI(carType)>>(json);
Thanks
dev
modified 4-Dec-18 19:58pm.
|
|
|
|
|
Why?
Why not just return a List<Car> - the base type for the derived classes? All you have to do is tell Newtonsoft what you are doing:
public abstract class Car
{
public int InCar { get; set; }
}
public class Porche : Car
{
public string InPorche { get; set; }
}
public class Toyota: Car
{
public int InToyota { get; set; }
}
Porche p = new Porche();
p.InCar = 111;
p.InPorche = "ppp";
Toyota t = new Toyota();
t.InCar = 222;
t.InToyota = 666;
List<Car> cars = new List<Car>() { p, t };
JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
string json = JsonConvert.SerializeObject(cars, settings);
List<Car> result =JsonConvert.DeserializeObject<List<Car>>(json, settings);
And it will sort it all out.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
devvvy wrote:
var carTypes = (from type in types
where type.IsClass && type == typeof(Car)
select type);
That's not going to do what you want. You're trying to find all sub-classes of the Car type, but your query is looking for types which are exactly the Car type.
I suspect you want to use Type.IsAssignableFrom(Type)[^] :
var carTypes = (from type in types
where type.IsClass && !type.IsAbstract && type.IsAssignableFrom(typeof(Car))
select type);
JsonConvert provides a non-generic method[^] you can use to deserialize your data. To access the result, you need to use a covariant interface[^] combined with the base type. For example:
var listType = typeof(List<>).MakeGenericType(carType);
var cars = (IReadOnlyList<Car>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, listType);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
hello
in the middle of trying to refactor out a nightmare that I have inherited and there is no documentation. The source looked something similar to this
var outcome = MethodCall(param1, param2, param3) ? AnotherMethod(MethodCall(param1, param2, param3), Enum.Value) :
MethodCall(param2, param3, param4) ? AnotherMethod(MethodCall(param2, param3, param4), Enum.Value) :
MethodCall(param3, param4, param5) ? AnotherMethod(MethodCall(param3, param4, param5), Enum.Value)
I have refactored this down to a series of If else but now I am at the following stage which I want to refactor because of the following. Apart from refactoring this into a series of nested if statements so that I dont have to call the same method twice in an if statement I am stuck.
if(methodcall(param1, param2, param3))
{
outcome = AnotherMethod(MethodCall(param1, param2, param3), someintValue);
}
else if (methocall(param2, param3, param4))
{
outcome = AnotherMethod(MethodCall(param2, param3, param4), someintValue);
}
else if (Methodcall(param3, param4, param5))
{
outcome = AnotherMethod(MethodCall(param3, param4, param5), someintValue);
}
Any advice would be greatfully recieved.
Thanks
Simon
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
How about this:
if(MethodCall(p1,p2,p3)||(MethodCall(p2,p3,p4) || MethodCall(p3,p4,p5)){
outcome = AnotherMethodCall(true, someIntValue);
}
From your code, I understand that MethodCall is of bool return type and if any one of three calls is true , we call AnotherMethodCall with true and a number. So, here is how it can be broken down. If I could, I would refactor method one to take in more arguments so only one call will tell me if it returns true for various input sets.
"It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[ ^]
|
|
|
|
|
I make no apologies:
if(methodcall(param1, param2, param3)) {
outcome = AnotherMethod(MethodCall(param1, param2, param3), someintValue);
return;
}
if (methocall(param2, param3, param4)) {
outcome = AnotherMethod(MethodCall(param2, param3, param4), someintValue);
return;
}
if (Methodcall(param3, param4, param5)) {
outcome = AnotherMethod(MethodCall(param3, param4, param5), someintValue);
return;
}
throw new exception( "Logic error" );
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hi,
as others already noted, every second call to MethodCall(param1, param2, param3), I mean the ones after the question marks, is likely to return true, as you just have tested for true (that is what the question mark does). I say likely, it might be ugly code that creates or uses nasty side effects. If so, what follows needs some adjusting.
it seems all your params have the same type, and all your someIntValues have different values, so I would suggest you:
1. stuff all the params in an array "paramsArray"
2. stuff all the someIntValues in an array "someIntValuesArray"
3. modify your MethodCall() to accept paramsArray and return an integer, being the first index in the paramsArray for which the original MethodCall() did return true; this basically means surrounding existing code with a for loop, adjusting the params (now array elements); also return a special value if no match found, say -1.
4. then simply do
int index=MethodCall(paramsArray);
if (index>=0) outcome=AnotherMethod(true, someIntValuesArray[index]);
|
|
|
|
|
Hi,
i have a report with the below structure
<group 1="">
<group 1="" header="">
<group2 footer="">
i'm trying to export this report to excel with repeated headers and footers with N number of rows to print the report..
After setting the page break calculations and repeat header and footers, the report does not break with the exact N number of rows..
i have few merged rows inside the report and conditions to hide rows..
Appreciate if some one could help me to achieve this... its really urgent
thanks in advance
|
|
|
|
|
|
I've just checked and nobody here appears to have this on their ToDo list at all, much less near teh top.
Are you sure it's urgent? Even the cat isn't too fussed about it at the moment.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I've go no experience at all in scanning barcodes, so excuse my ignorance.
What's involved in scanning a barcode and getting back the product's image?
I can provide more detail on the projects idea if needed.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Most barcode scanners are of the "keyboard wedge" variety. Scan a code and it "types" the code into your application.
The thing is, since the scanner types the code, you have to have a way of recognizing that the code is being typed by the scanner. So, most scanners will also let you configure a prefix and postfix you can add to the code. If you setup your code to look at every keystroke at the form level (Key Preview) you can look for the prefix and if you see it, set the focus to a textbox that takes the code, and the look for the postfix to know that the code is done.
As for the image, you have to look up that barcode in your own database if you've got images for each item. If not, there's public databases that have this stuff, but don't count on a free one. How you access them is dependent on the API for the vendor you use.
|
|
|
|
|