|
Create Identifying name
string c = System.Environment.MachineName;
string Domain = System.Net.Dns.GetHostName();
ManagementObjectSearcher SerialNo = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM_
Win32_SystemEnclosure");
foreach (ManagementObject querySN in SerialNo.Get())
{
string SN = (querySN["SerialNumber"].ToString());
string SysIdent = c + SN + "@" + Domain;
Check if ID exists by looking for Identifying name - string SysIdent
using(SqlCommand selectname = new SqlCommand("SELECT COUNT(1) FROM dbo.SysIdent WHERE_
FullName = @SysIdent", IdentSys));
If name isn't found, insert it into the table
using(SqlCommand newname = new SqlCommand("INSERT INTO dbo.SysIdent(FullName)_
VALUES(@FullName)", InsertSys);
Reconnect to table to get the ID number associated with the name
SqlCommand GetID = new SqlCommand("SELECT * FROM dbo.SysIdent WHERE FullName = @SysIdent", selectID);
set the system's id
int mySysId = myID;
|
|
|
|
|
I got that already - but how does this connect to your timed_event... items? I'm trying, but I can't see what they have to do with this. Take a step back and explain in simple terms what you are trying to achieve with the application - don't dive into implementation details, tell me what problem your application is trying to solve and we'll go from there.
A question - is the system name just meant to be retrieved once per machine? Are you attempting to put the service onto individual machines and that's what the above code is to identify, has the system previously been registered or not (and register it if it hasn't)?
|
|
|
|
|
The purpose of the application is to collect various metrics for individual machines and write those metrics to a SQL table.
In order:-
Service is installed via a msi per individual machine
Service auto-starts and logs the start in the Application & System logs
If this is the first time the code runs, the system queries the 'identity' table for its ID by looking for the id number associated with it name, adds its name to the table if it isn't there already then retrieves the ID number associated with its name and uses that ID 'forever'
* is there a way to set this ID in the code or does it need to get its ID every time?( I would prefer not to use a "cookie" approach nor to write a text file to a file).
on_timed_event_1 fires up
uses the ID allocated above to write the various metrics to the various tables
on_timed_event_2 fires up
uses the ID allocated above to write the various metrics to the various tables
"is the system name just meant to be retrieved once per machine?" - no/yes. The machine uses its system name to identify itself. Once that name is in the table and the system's id has been allocated it uses that id for every transaction afterwards(the id is used as the foreign key in all the other tables)
"Are you attempting to put the service onto individual machines and that's what the above code is to identify" - yes
"has the system previously been registered or not (and register it if it hasn't)?" - exactly
Is this explanation a bit better?
|
|
|
|
|
Right - that's fairly straighforward then. OK, how I would structure this (very loosely):
I would have a manager class which was responsible for triggering the different timed events. This class would also be responsible for triggering the insert/lookup into the database of the system identifier (if I was doing this, I would put this in a separate data layer which could easily be mocked for testing purposes, and I would have the data access happen on a background thread in order not to slow down the startup of the service too much).
I would keep the identifier as a property in the manager class. Now, the manager class would be a member of the service class, and I would instaniate it in the OnStartup event (triggering the system identification).
I hope that this gives you some ideas.
|
|
|
|
|
Pete, I'm sorry but you have lost me, I am insufficiently advanced to fully understand what you are suggesting.
Are you suggesting that I created a second .cs or that I create classes in the same namespace? e.g.
using this;
using that;
namespace a
{
class MyService
{
public MyService()
{
InitializeComponent();
//do stuff
}
}
class Manager
{
public class Manager()
{
// do the get/set id thing
}
}
class ServiceStart
{
protected override void OnStart(string[]args)
{
// create event logs and EnableRaisingEvents stuff
}
}
}
If you would like me to I'll happily post the code I have adapted.
|
|
|
|
|
Not quite. I would create a second class - this does nothing with regards to service management, and it would look something like this:
public class TimedEventManager
{
private BackgroundWorker bw = new BackgroundWorker();
public void Initialise()
{
bw.DoWork += CheckForId();
bw.RunWorkerAsync(null);
}
private void CheckForId(object sender, DoWorkEventArgs e)
{
bw.DoWork -= CheckForId();
bw.Dispose();
}
public int Id { get; set; }
} Then, in your service class, I would create an instance of this:
private TimedEventManager timedEvent;
protected override OnStart(string[] args)
{
timedEvent = new TimedEventManager();
timedEvent.Initialise();
} And that's it. That's how you hook it together.
|
|
|
|
|
Hiya Pete & thank you for helping me out, it is appreciated.
Sorry if I am being thick but I still can't get the SysID to carry across so that I can use it repetitively.
I would like to post my code so that you could look at it and tell me what I am doing wrong but there is a LOT of it and popping it in a forum message makes it hard to read.
Suggestions?
|
|
|
|
|
Put it somewhere that I can download it from and I may be able to take a look at it - no promises at the moment.
|
|
|
|
|
I'll try and do it tonight or, failing that, tomorrow morning-ish. Thanks Pete.
|
|
|
|
|
No problem, but just be aware that I tend not to spend much time on CP at weekends as I like to spend time with the family, and I don't have email notifications turned on because my inbox used to get overwhelmed by emails.
|
|
|
|
|
I agree, wives and kids are a lot more fun (well, after the Saturday morning grocery shopping is done). I'll put the timed_events stuff in as well so you will be able to get a better picture of what it is I am trying to do.
Have a great Friday.
M.
|
|
|
|
|
|
My apologies, I've been insanely busy for the last couple of days. If you'd care to reupload your code, I have temporarily enabled the option to email me, so you can send me the location of it and I will download it later on.
|
|
|
|
|
Hi Pete,
Please don't apologise, it is you who is doing me a favour...
I've put the code here http://pastebin.com/ye3N3NNY[^]
I have more-or-less got it working. The problem I am having is the line "private static void OnTimedEvent(object source, ElapsedEventArgs e)". If I leave it as is the various methods run and populate the database but I can't use the SID (System ID value) value previously created with a get/set.
To access the SID I have to change it to a "public" method but then the db doesn't get populated...
Thanks once again for looking at my (hopefully not to shabby) code.
M.
|
|
|
|
|
You can't use SID because it's an instance variable, but the static method indicates you need to use static variables, so you either need to convert your property and field to static
private static int SID;
public static int mID or you remove the static from the OnTimedEvent.
|
|
|
|
|
aaaarghhh....of course!!!
|
|
|
|
|
Fresh pair of eyes and all that.
|
|
|
|
|
Could you please stop using the phrase "Greetings Gurus"? You don't need to use it to get help - just keep asking decent questions and provide us with enough information to help. By using this phrase, you're coming across as a crawler - and that's a bit offputting.
|
|
|
|
|
Hi Pete,
My apologies if you found the greeting distasteful, it was meant to be cheerful and recogniscent of the superior abilities held by some of those in the forum .
I shall refrain in the future.
Thanks for pointing it out.
|
|
|
|
|
CCodeNewbie wrote: recogniscent of the superior abilities
That implies an elitism that doesn't exist. We may have more experience in some areas, but this does not infer any form of superior ability. Each of us has deficiencies in our knowledge that others cover for - for instance, I tend not to answer much on the p/invoke side because it's not an area I deal much with.
[Edit]Don't get me wrong - you ask good questions. You provide detail and clarification, along with actual code. That means we are more than happy to help, so just keep this part up and we'll all be happy.
|
|
|
|
|
Will do
|
|
|
|
|
Hello Experts,
I would like to ask even though I manage to add the Merge Module for visual studio 2010 so that I can deploy my Application that uses crystal report I still got the error below.
Could not load file or assembly CrystalDecisions.Windows.Forms.Version=13.0.2000.0
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.IO.FileNotFoundException: Could not load file or assembly 'CrystalDecisions.Windows.Forms, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The system cannot find the file specified.
File name: 'CrystalDecisions.Windows.Forms, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304'
at SimplyOutbound.Form1.InitializeComponent()
at SimplyOutbound.Form1..ctor()
at SimplyOutbound.ReportGenerator.btnView_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
Any advice, suggestion are so much appreciated.
Thanks,
DAN
|
|
|
|
|
Hello,
I was a bit lucky I manage to fix it also. I am not sure but might be helpful to others also, it seems the error can be done by the previous version of crystal report that is being installed in the client machine. I tried to properly uninstall the previous version of the crystal report then repeat the same I process until its successful.
|
|
|
|
|
|
I'm a relatively new C# programmer and am using datagridview to maintain my data. However, some of the tab pages in my application need to show data and data subsets in a different format. Some of it will be row-based instead of column-based. Here's an example; if I have 4 objects, each taking up one row of a datagrid in the first tab I will need to break that up into 4 columns per object in another tab for a total of 16 columns and another tab will break that into 4 groups of 6 rows for a total of 24 rows. I also might want to get rid of the grid "look" and show it in more of a row-based/column based reporting format that will still be bound to a set of underlying methods. Before I start trying to do this with datagridview (perhaps it's not even possible) does anyone out there have any better ideas as to how to go about this? The key is that the data has to be bound to underlying calculations, kind of like a spreadsheet, but I need a lot of flexibility as to how I go about it. Thanks in advance.
|
|
|
|