|
Thanks but that was what I initially did, and that's when I got the code I've quoted in my first message.
Maybe there's some problem with the "Intelli"-part of Intellisense? Or it might be just me, but I never get an Event. It compiles ok, but still "no action" when subscribed, and this COM object does send a lot of updates...
|
|
|
|
|
Are you sure the COM component is calling the event? The two code snippets you provide are different in their approach to subscribing. Perhaps you need to call SetServiceAccessSubscription from C#, instead of just hooking onto the event?
COM interop can be a bit strange to use because many COM components do not quite work how a .Net person expects.
|
|
|
|
|
BobJanova wrote: Perhaps you need to call SetServiceAccessSubscription from C#, instead of just hooking onto the event?
I'm starting to think that myself. Just have to figure out Variants in C#.
Thanks!
|
|
|
|
|
I think you can use object , and then use is or GetType to work out what further to do with it.
|
|
|
|
|
So, I am currently writing some software for my job. I am using OLE DB to connect to a Microsoft Office Access 2007 database, where all the data is stored. I have no issues for the most part. However, I have a question concerning null field values when using OleDbDataReader.GetValue/String/Int/DateTime/etc.
Some fields in the database are required. This is no issue because I validate the necessary data in the software before attempting to insert/update records. But what about the fields that are not required by the database? Do I have to use "IsDBNull(ordinal)" for each field before I attempt a GetValue/String/Int/DateTime/etc. Or is there an easier way to go about "looking over" null values?
I have a class, RepairRecord, which stores the information about each record. And as an example, I have a field, Comments, which is not a required field. If I insert a record with no value for Comments all is well. That is, until I attempt to SELECT the record. If I simply use "reader.GetString(11)" and the value is empty I get an InvalidCastException, which I understand. I just need to know if there is a simpler method which I am looking past for dealing with these. The Comments field is not the only field that is optional.
If I am leaving out any important information just let me know.
Tools I am using:
Visual Studio 2010
C# (.NET 4.0)
OLE DB
|
|
|
|
|
For one thing, I never use the Getxxx methods -- they're as useless as the Convert class .
For another, don't use the ordinal values; use the names -- the order of things could change unexpectedly.
And, what do you do when it is null? Apply a default?
You may want something along the lines of:
System.DateTime somedate = System.DateTime.MinDate ;
if ( reader [ "somedate" ] != System.DBNull.Value )
{
somedate = (System.DateTime) reader [ "somedate" ] ;
}
(I don't feel like testing that right now, so I hope it works. Come on, beer, do your stuff!)
P.S. Oh, and I suppose you could write a generic Extension Method to simplify that for you.
|
|
|
|
|
Oh, thanks for the timely response. :-P I will give that a try as soon as I get back on the project. I knew I was missing something, I just couldn't figure out what. But I haven't had much experience AT ALL with database-driven application development. So please stick with me, as I am a noob. xD
|
|
|
|
|
That made me wonder about a few things, so here are two questions for you:
1.
I understand if your SQL says "SELECT * FROM ..." you will get the fields either in their table structure order, or in an arbitrary order (which is the same for all practical purposes as someone could change the structure).
However if your SQL says "SELECT A,B,C FROM ...", would reader["A"] and reader[0] not be absolutely equivalent?
2.
If so, it gives another good reason to name the fields explicitly in the SQL statements. And yes, I would still prefer to use the field names as an index, as that improves readability. Would it also negatively affect performance?
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Luc Pattyn wrote: would ... not be absolutely equivalent?
I haven't bothered to compare, but I suspect that the overload with the name performs a lookup and then calls the overload with the index -- it would therefore be slower, but easier to read and maintain etc.
P.S. Oh, and don't forget that if you are using stored procedures for your queries , some one may mess with one.
modified on Wednesday, May 18, 2011 11:08 PM
|
|
|
|
|
Yeah, that's what I was thinking. Slower and better looking.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
That's how I like my women too.
|
|
|
|
|
Well, obviously, the best of both worlds would be to set some constants above it all, i.e. A=0, B=1,C=2, so Reader["A"] would be the equivalent of Reader[A], and so on.
Almost time to go home. Silliness abounds.
|
|
|
|
|
1. Using index is faster than using names, since using names does an additional index lookup, but it increases readability considerably and is also safer. Imagine a database developer changing your query from something like this
SELECT A,B,C FROM ...
to this
SELECT B,A,C FROM ...
|
|
|
|
|
Luc Pattyn wrote: Would it also negatively affect performance?
This tends to be treated as a micro-optimisation that is completely useless. There is a simple way to get around this performance hit, which is to use the ordinal based value, but use GetOrdinal to retrieve the position of the ordinal based on the field name.
The thing is, you only need to use GetOrdinal once for each field you are interested in - which means that you can use the ordinal in the loop over the reader.
Effectively, your code becomes something like this (with apologies because I'm just typing this off the top of my head in the CP editor):
int customerId = reader.GetOrdinal("CustomerID");
int customerName = reader.GetOrdinal("Name");
while (reader.Read())
{
Customer cust = new Customer(GetValue(reader, customerId), GetValue(reader, customerName));
}
private T GetValue(SqlDataReader dr, int position)
{
if (dr.IsDbNull(position))
return default(T);
return (T)dr[position];
}
|
|
|
|
|
I like it. The rule now is: don't use numeric field indexes ever; use field names directly, unless you can't afford the speed penalty, then use GetOrdinal as per your example (which is safe but somewhat more verbose).
And you could still make that an extension method on DataReader, as PIEBALD suggested.
Thanks Pete.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Luc Pattyn wrote: The rule now is: don't use numeric field indexes ever; use field names directly,
unless you can't afford the speed penalty, then use GetOrdinal as per your
example (which is safe but somewhat more verbose).
Guidelines to live by
|
|
|
|
|
How to make possible the activation of group-box controls by choosing one of the options in combo-box list?
Best regards
|
|
|
|
|
Look at the documentation first, then come ask questions.
DOCUMENTATION[^]
Looks like OnSelectedItemChanged or OnSelectedValueChanged should work quite well for you.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
|
|
|
|
|
Not sure what you mean by activation. I think you may be referring to either enabled or visible. Lets say it's the enabled property of the GroupBox. First you need to catch the SelectedIndexChanged event of the ComboBox, evalutate the selection and determine if the GroupBox should be enabled
private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
MyGoupbox.Enabled == (MyComboBox.SelectedValue == criteria);
}
Excuse the pseudocode but i think you get the idea
"You get that on the big jobs."
|
|
|
|
|
Hello.
I have a problem. I have two independient forms inside the same project and I want to call a function that is in the form 1 from form 2.
I am using c# for an window application
I have declared in the form 1 the following function:
public void Hola()
{
MessageBox.Show("Hello");
}
But I don´t know how to call from form2
Thank you very much!!
modified on Wednesday, May 18, 2011 11:22 AM
|
|
|
|
|
For a simple excercise, you could create a form level variable in form2 that holds a reference to form1. Then in form2 you can just call the function like this
public Form1()
{
InitializeComponent();
}
public void Hello(string name)
{
MessageBox.Show(string.Format("Hello, {0}", name)
}
public partial class Form2 : Form
{
Form1 form1;
public Form2()
{
InitializeComponent();
form1 = new Form1();
}
private void Form2_Load(object sender, EventArgs e)
{
form1.Hello("Wayne");
}
}
As a simple excercise, this is fine, but in a production app you would want to look into creating a Mediator class to communicate between classes, so that the entities have no need of any knowledge of each other.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
I am hoping this is not for your homework, but, I will answer nonetheless.
Two simple ways to accomplish this would be:
1. Declare your method in Form1 as static and invoke it from Form2 like so:
In Form1...
public static void Hola()
{
MessageBox.Show("Hello");
}
In Form2...
public void InvokeMessage()
{
Form1.Hola();
}
2. Using your declaration like so:
In Form2...
public void InvokeMessage()
{
using(Form1 otherForm = new Form1())
{
otherForm.Hola();
}
}
|
|
|
|
|
I'm assuming that Form1 creates the instance of Form2?
If so you should use an event in Form2 to signal and Form1 should subscribe to the event and call the method.
Basic theory is in this tip[^] which is about passing values but the same theory applies.
|
|
|
|
|
If you need to call a method in another form then the forms are not independent. Form1 is actually dependent on Form2. Sure you can make the method static but you should really consider refactoring this fuctionality into a separate class.
"You get that on the big jobs."
|
|
|
|
|
I have a problem, that is to mask data in certain format that is displayed in the browser
To solve the problem I came up with the following solution
1)Find all the browser window handles (class Internet Explorer_server)
2)Get the IHTMLDOCUMENT2 using the window handle
3)Use IHTMLDOCUMENT2::body::innerHTML to find the data I am looking for and modify it.
The code I have written below throws a COMException. [Could not set the innerHTML property. Invalid target element for this operation.]
IHTMLElementCollection dome = document.all;
foreach (IHTMLElement dom in dome)
{
StringBuilder bf = new StringBuilder(dom.innerHTML);
if (bf.ToString().Contains(pattern.ToString()))
{
bf.Replace(pattern.ToString(), "***-**-****");
dom.innerHTML = bf.ToString();
}
}
Why does this exception occur. How do I fix it so that the webpage contents will be altered.
I wrote another version of the code, it works, but not completely.
I found out that is because the webpage contains multiple frames and so it does not work. I does not work if the SSN number is inside a frame(or something like a frame which holds an embeded object).
StringBuilder bf = new StringBuilder(document.body.innerHTML);
if (bf.ToString().Contains(pattern.ToString()))
{
bf.Replace(pattern.ToString(), "***-**-****");
document.body.innerHTML = bf.ToString();
}
The first piece of code throws exceptions, the second one does not work if there are multiple frames.
What am I doing wrong in both the code above.
How do I go about achieving my solution. Is my approach correct, or is there a better way to do it.
Thanks and regards,
David
|
|
|
|
|