|
It's not about effort now - it's about effort later.
Using Visual Studio default names for everything is a very poor idea - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
And having all names be very, very similar is a recipe for unreliable software as well - it's far, far to easy to get it wrong and not notice. Until of course you have mucked up the DB so badly it's going to take days or weeks of human effort to sort it all out.
And as regards the number of controls, that's good advice as well - it reduces the "clutter" and makes your code more "generic" - it's not fixed to a particular set of textboxes so additions and upgrades become easier - and code changes all the time! Plan for changes and life becomes easier. "Throw it together" without planning and maintenance becomes more difficult and fraught with potential bugs.
Take time to get it right: you won't get time to come back and do it properly later, so just like SQL Injection prone code it will bite you in the ass at a future date when you have forgotten most of how the software works.
It really does save you time and effort in the long run, as well as producing better code.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi Jim, Member 13070736 wrote: that seems like way too much effort just to slot an array into textboxes. I've added a paragraph to my original response as a comment on this statement.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Hi, Luc, it's a pleasure to see you (someone I regard as a true mentor) posting, again !
On a general (pedagogic) level, I agree with your advocacy of strong, descriptive, names; however, in the case of a question like this one, how do you know the OP is not just prototyping a solution, and, when implementing it, will use strong naming ?
Luc Pattyn wrote: When your user interface requires a large number of Controls you probably should rethink your design There are circumstances that require a large number of data-entry Controls to have their values set by the user: the issue is not "wrong design," but how to control the UI so the user is not overwhelmed ... by organizing the input tasks into groups, presenting each group in a separate UI, etc. ... this can be as simple as using a TabControl.
Re: use of DataGridView: we don't know if a DataBase is involved, and, imho, the DataGridView is one ugly monster of an antique Control. FlowLayoutTable, and TableLayout panel are other optional uber-containers.
The need for a collection of TextBox Controls functioning as a kind of logical unit for inputting/validating a set of data is very common. I prefer to implement this in a UserControl (or Panel) that implements validation, provides options for either sequential entry, random entry, etc. ... or, in a Form shown as a Dialog.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
modified 26-Feb-18 5:15am.
|
|
|
|
|
Hi Bill,
I never stopped visiting CodeProject, I'm here a few times a week reading an article and/or looking at the C# forum. My contributions are few, I only post when a thread remains unanswered, or something I consider important is missing in the replies.
We all take the given information, which unfortunately often is sparse and without much context, and we have been told not to look at the poster's screen, access his HDD, or read his mind. So we need to work with what is made available while assuming nothing much. However I tend to assume code shown is representative for actual code.
Of course having several TextBoxes isn't automatically wrong, or bad design; it may even be the right choice. Showing code for five, plus ellipsis, made me produce a warning. And then the poster's question left me wondering whether the Form pertained to one composite object, or to a collection of simple data items, hence a number of shoulds, probablies, and maybes. Due to the array I tended to the collection-of-items situation, and hence suggested a DGV, which is a bulky Control I first used in CP Vanity[^] if my memory serves me well. For the single-object situation, maybe I should have suggested a PropertyGrid.
Cheers
|
|
|
|
|
Luc Pattyn wrote: Of course having several TextBoxes isn't automatically wrong, or bad design; it may even be the right choice. Showing code for five, plus ellipsis, made me produce a warning.
Particularly when they start with "Textbox10" and go up from there...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
How about the following untested code:
TextBox tbs[] = { textBox10, textBox11, ..., textBox99 };
Object arr[tbs.Length];
for (int i = 0; i < tbs.Length; i++)
tbs[i].Text = arr[i].ToString();
or
TextBox tbs[] = { textBox10, textBox11, ..., textBox99 };
Object arr[tbs.Length];
int i = 0;
foreach (TextBox tb in tbs)
tb.Text = arr[i++].ToString();
|
|
|
|
|
hello..
I'm developing a software? for my thesis using C# winform application.. it is an offline system.. and we are not allowed to make a web application..
my goal is to show pdf files from MySQL database without using Adobe Acrobat reader or any pdf viewer that has a printing tool because I want to disable the printing process.. please help me.. Is it possible to show pdf file in a panel? if so, how?.. or even in a picturebox?..
I want to disable the printing process because I want to control my printing process with a coin acceptor and an ARDUINO..
thank you..I hope you could help me..
Shiela Amante
|
|
|
|
|
Is it possible? Sure. You just have to write the massive pile of code that interprets and renders the PDF.
USE AN EXISTING LIBRARY! Turning in an application for your thesis that isn't complete because you're wasting a couple of YEARS writing your own PDF interpreter and rendering engine is just f'ing STUPID!
|
|
|
|
|
To "show" a pdf (under control of an app) one typically uses the "print preview" option (of a given pdf printer driver) in the app via the "print / printer dialog".
(PdfSharp can generate pdf's / previews from raw content if you want to get fancy).
The "app" controls the printing in this case; not the user.
If your pdf source is "hidden", you have effectively taken control of the "printing process".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
|
There is no library that forces you to implement a printing-button. You're not ready for a thesis.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Message Removed
modified 24-Feb-18 2:56am.
|
|
|
|
|
|
I'm trying to read eventlogs from a server which has about 100 000 records using class EventLogReader(eventLogQuery).
I'm using pagination and each page will show only 25 records in my screen. So, I will be reading 25 records out of total records for the first page and next 25 records for the second page and so on.
My question is how to get the total records count for the below snippet like total rows affected because of that eventLogQuery applied on full set of events?
EventLogReader reader = new
EventLogReader(eventLogQuery);
reader.Seek(SeekOrigin.Begin, filter.PageStart);
eventLogs.TotalLogs = **totalRowsAffected**;
EventRecord eventInstance = reader.ReadEvent();
int i = filter.PageSize;
while (eventInstance != null && i-- > 0)
{
try
{
eventLogs.Entries.Add(new EventLogData
{
Type = eventInstance.LevelDisplayName,
Source = eventInstance.ProviderName,
Time = eventInstance.TimeCreated,
Category = eventInstance.TaskDisplayName,
EventId = eventInstance.Id,
User = eventInstance.UserId != null ? eventInstance.UserId.Value : "",
Computer = eventInstance.MachineName,
Message = eventInstance.FormatDescription(),
FullXml = eventInstance.ToXml()
});
}catch{}
eventInstance = reader.ReadEvent();
}
}
return eventLogs;
|
|
|
|
|
"affected" is not even a word in this context ... and even if one were to try and guess which "count" you were referring to, there is still at least a 50% chance of guessing wrong.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
My focus is to get the total count of records when we use EventLogReader(eventLogQuery).If you see the code I believe you will get what I want from you guys. Hope I made it clear.
|
|
|
|
|
In the following snippet of a class, I'm passing a reference to connection_string (a private field in the same class) into ExecuteScalar method by way of the connection_string parameter.
But in SomeMethod , I'm using connection_string , even though I haven't passed it into the method as a parameter.
Which way is the most common and why?
class public DBCall
{
private string connection_string = "...";
private void SomeMethod()
{
var zipcode = ExecuteScalar<string>("select zipcode from ZIpcodes where Zip='92108'", connection_string);
}
public T ExecuteScalar<T>(string queryString, string connection_string)
{
using (OleDbConnection conn = new OleDbConnection (connection_string))
{
using (OleDbCommand cmd = new OleDbCommand (queryString , conn))
{
var result = cmd.ExecuteScalar ( );
if (Convert.IsDBNull (result) && typeof (T).IsValueType)
return default (T);
else
return (T) (result);
}
}
}
private void treeView1_NodeMouseClick(object sender , TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
string loadrowset = e.Node.Text;
Rowset rowset = (Rowset) Enum.Parse (typeof (Rowset) , loadrowset);
using (OleDbConnection conn = new OleDbConnection (ConnectionString))
{
OleDbSchemaTable s = new OleDbSchemaTable (rowset , conn);
DataTable dt = new DataTable ( );
dt = s.datatable;
dataGridView1.Enabled = true;
dataGridView1.DataSource = dt;
}
}
else
{
return;
}
}
The lack of surety in programming is part of the reason software is fragile.
|
|
|
|
|
Yes it is fine, since the private string is part of the class definition, and therefore is present in every instance of the DBCall class. However, I would suggest you use a different name in the parameter field to make it clear which object you are referring to in the following code. I am assuming that ExecuteScalar could also be called from elsewhere with a different value for that parameter.
|
|
|
|
|
Than you, Richard. Good point about using a different name. I meant to go back and edit that. Someone told me that they would be "pissed" if a variable was used in a method, that wasn't passed into it. He said it makes the code hard to read and not portable. He has a point, but I did it both ways in the program in question and the one where I didn't pass fields into methods looked cleaner. It might be because I use long variable names, which people laugh at me for, but people rarely have to ask what I meant. I drigress...
The lack of surety in programming is part of the reason software is fragile.
|
|
|
|
|
Rick_Bishop wrote: they would be "pissed" if a variable was used in a method, that wasn't passed into it. The fact that both connection_string and SomeMethod are both private, and also close together in the code means that it is not unreasonable to do it that way. Just as long as it does not create a maintenance problem in the future.
Rick_Bishop wrote: because I use long variable names Something that I think far too few people do. Reading your code it is pretty obvious what the name connection_string refers to. As opposed to something like textBox8.Text which means absolutely nothing.
|
|
|
|
|
Rick_Bishop wrote: public T ExecuteScalar<T>(string queryString, string connection_string)
That method is going to force you to write code which is vulnerable to SQL Injection[^]. You have to be able to pass parameters to the query as parameters, not using string concatenation.
private OleDbCommand CreateCommand(OleDbConnection connection, string queryString, object[] parameters)
{
var command = new OleDbCommand("", connection);
if (parameters != null && parameters.Length != 0)
{
queryString = Regex.Replace(queryString, @"\{(\d+)\}", match =>
{
int index = int.Parse(match.Groups[1].Value);
object value = parameters[index];
string name = "@p" + command.Parameters.Count;
command.Parameters.AddWithValue(name, value);
return "?";
});
}
command.CommandText = queryString;
return command;
}
public T ExecuteScalar<T>(string queryString, params object[] parameters)
{
using (OleDbConnection connection = new OleDbConnection(connection_string))
using (OleDbCommand command = CreateCommand(connection, queryString, parameters))
{
var result = cmd.ExecuteScalar();
if (Convert.IsDBNull(result)) return default(T);
return (T)result;
}
}
private void SomeMethod(string zipCode)
{
var zipcode = ExecuteScalar<string>("select zipcode from Zipcodes where Zip = {0}", zipCode);
...
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Just getting back to this. Thank you for your comments. The querystring is being built by another method and isn't SQL Injection isn't going to be a problem. I can't use parameters as defined by the db provider because this same method is used for oledb and odbc and sqlclient connections.
The lack of surety in programming is part of the reason software is fragile.
|
|
|
|
|
There are many things "not so clean" in your code.
Why does that class have a field for the connection string, but otherwise require the connection string to be passed into a public function? Why should a consumer of this class know anything about connection strings?
Also, fields and parameters should "look" differently to avoid confusion of the human reader (I think it is a BUG in Microsoft's specification of scope ).
Then there's some UI code also: treeView1_NodeMouseClick . And there is another kind of connection string - now ConnectionString , previously connection_string . Totally confusing.
Also, the else {return;} is not useful.
Obviously, that class is responsible for more than one thing. Hence it needs refactoring to single responsibilities. I am sure that those confusing connection string will be resolved during that refactoring.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Thanks, Bernard. I'm just getting back to this. ConnectionString was typo and thank you for the tip about else {return;}.. not sure why that's there actually. This program actually connects to many different types of databases and with many different providers, so the connection string had a different scope than is typical, I think. I like the way you worded that.
The lack of surety in programming is part of the reason software is fragile.
|
|
|
|
|
If you have the info in a field, then don't pass it as a parameter
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|