|
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[^]
|
|
|
|
|
Thank you, this is why I asked. My coworker does not agree, but it sounds like he is in the minority.
The lack of surety in programming is part of the reason software is fragile.
|
|
|
|
|
Once they exist as a member, any parameter would be redundant. It's a nice way of sharing variables inside the black box of code. It is not used for things that come from the outside and are only used inside that method - those remain parameters.
We group variables and code that belong together in an object because it is superiour to the procedural model where you pass everything as a parameter.
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.
|
|
|
|
|
If I recall correctly the method will use the parameter value of the variable.
If you would like to use the class variable you need to use "this" in front.
See here [^] to see the result.
using System;
public class Program
{
public static void Main()
{
Test t = new Test();
t.WriteValue("abc");
}
}
public class Test{
private string teststring = "xyz";
public void WriteValue(string teststring){
Console.WriteLine(teststring);
Console.WriteLine(this.teststring);
}
}
Hope this helps
|
|
|
|