|
|
Try this
SqlConnection objCon = New SqlConnection(ConfigurationManager.ConnectionStrings["VEESERV4"].ConnectionString)
|
|
|
|
|
I'm writing a kind of terminal program that uses the serial port.
I have it on my "main form", which does most of the communication.
My problem is that I need to add another form that also uses the serie port.
Now to my question / problem.
How can I access the opened serial port on my main form?
//Ola
|
|
|
|
|
Hope you are using VB.NET,
If yes then, make the variable of serial port instance to public instead of friend
e.g.
Instead of
Friend WithEvents SerialPort1 As System.IO.Ports.SerialPort
Use
Public WithEvents SerialPort1 As System.IO.Ports.SerialPort
Then you can access it using My.Forms.main-form-name.SerialPort1
Hope I answered your query
Thanks
Ashu
dnpro
"Very bad programmer"
|
|
|
|
|
Thanks for the advice but unfourtanly I'm not using VB.Net.
I'm writing in c#...
|
|
|
|
|
Same principal, but in C# you make the member public or internal rather than protected or private . For a visual control, you can go to it's properties in the designer and set this via the Modifiers property rather than editing the designer generated code file. Also, the second form will have to have a reference to an instance of the main form; so you'll need to also make that link and then you can access the serial port member as a property of an instance of the main form from the second form. I find the easiest way to establish that link is in the second form's constructor by passing in a reference to the main form:
private MainForm mainForm = null;
public SecondForm(MainForm mainForm) <---- constructor
{
...
this.mainForm = mainForm; <---- save reference to main form
...
}
private void UseSerialPort()
{
SerialPort sp = mainForm.SerialPort; <---- access the member on the main form
}
And on the main form you'd have:
private void OpenSecondForm()
{
SecondForm secondForm = new SecondForm(this); <----- pass the instance of the main form here
secondForm.Show();
}
Remember that even though they are Forms, they are also still just classes like everything else and the controls on them are just members of the class. So, if you wanted one class to be able to access a member from another class you'd have to increase the visibility of that member in the other class and have a reference to an instance of it. So, you're just doing the same thing here but the classes happen to be Forms. Also, it's probably better practice to make an actual property on the main form to control access to the serial port member. This way you could also make it with just a get and have a readonly property.
It's important to consider the ownership and access of objects in the structure of your application. Does it make sense for the serial port object to be 'owned' by the Main Form and shared between it and then second form, or does it make more sense to have a separate class that owns it and from which both forms access it? I can't tell you the answer to that, its just something to keep in mind when you are making these kinds of decisions with your application.
Hope this gets you going!
Keep It Simple Stupid! (KISS)
|
|
|
|
|
Thanks, that helped alot.
It works as I wanted it to.
But I did take youre advice and moved the serial port control to a "ordanary class", witch in terms the two forms can use.
Thanks
Ola
|
|
|
|
|
Hello, I'm trying to determine why this encryption won't work for binary files. (specifically docx) I can't use a filestream which is what I've read on a few boards and examples because I'm pulling the files from word and posting to a cloud service. Any Ideas? Am I just going about it wrong?
TIA,
foster
public static byte[] Cipher(byte[] PlainText, string InitialVector, string Salt, int KeySize, string PassPhrase, int PasswordIterations, string HashAlgorithm)
{
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(PassPhrase, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged();
SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC;
System.Security.Cryptography.ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes);
System.IO.MemoryStream MemStream = new System.IO.MemoryStream();
System.Security.Cryptography.CryptoStream CryptoStream = new System.Security.Cryptography.CryptoStream(MemStream, Encryptor, System.Security.Cryptography.CryptoStreamMode.Write);
CryptoStream.Write(PlainText, 0, PlainText.Length);
CryptoStream.FlushFinalBlock();
byte[] CipherTextBytes = MemStream.ToArray();
MemStream.Close();
CryptoStream.Close();
return CipherTextBytes;
}
public static byte[] Decipher(byte[] CipherText, string InitialVector, string Salt, int KeySize, string PassPhrase, int PasswordIterations, string HashAlgorithm)
{
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(PassPhrase, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged();
SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC;
System.Security.Cryptography.ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes);
System.IO.MemoryStream MemStream = new System.IO.MemoryStream(CipherText);
System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(MemStream, Decryptor, System.Security.Cryptography.CryptoStreamMode.Read);
byte[] PlainTextBytes = new byte[CipherText.Length];
int ByteCount = cryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
MemStream.Close();
cryptoStream.Close();
return PlainTextBytes;
}
|
|
|
|
|
The problem may be these two lines in the Decipher function:
byte[] PlainTextBytes = new byte[CipherText.Length];
int ByteCount = cryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
The Read() will fill PlainTextBytes up through the number of bytes read, the rest will be 0's. I find that very often the encrypted data is longer than the decrypted data. So it's very possible that PlainTextBytes will be created as a byte[] larger than the actual decrypted data. I've also come across code like this on the internet and won't use it myself; it just doesn't look right to me. For what arbitrary reason would you determine that the length of the decrypted data is exactly the same as the length of the encrypted data? Which is essentially what the code is assuming. I prefer to play it safe and not make that assumption.
I find it easier to use the CryptoStream in Write mode for both encrypting and decrypting, I find it helps avoid the aforementioned assumption. If you supply the CryptoStream an empty MemoryStream and the proper ICryptoTransform (encrypt or decrypt), then all you have to do is write the byte[] to the CryptoStream and when you Read from the MemoryStream you will get the transformed data. Doing that in the Decipher function would look like this:
MemoryStream memStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memStream, Decryptor, CryptoStreamMode.Write);
cryptoStream.Write(CipherText, 0, CipherText.Length);
PlainTextBytes = memStream.ToArray();
This is pretty much how you did the encryption in the Cipher function. The cool thing about this is that the actions for encrypting and decrypted are exactly the same, the only thing different is the data and the ICryptoTransform used. So, you can put this functionality in a single function in which you only need to pass the proper ICryptoTransform and the proper byte[] . When you are encrypting pass in the unencrypted data as a byte[] and the Encryptor ICryptoTransform and when decrypting pass in the encrypted data as a byte[] and the Decryptor ICryptoTransform , and you'd have this (which is almost the same as code I've used):
private byte[] PerformTransform(byte[] data, ICryptoTransform tran)
{
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, tran, CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);
cs.Close();
return ms.ToArray();
}
One last note, save yourself a lot of unnecessary text and put in the using statements for the System.IO and System.Security.Cryptography namespaces.
Keep It Simple Stupid! (KISS)
|
|
|
|
|
Timed GTalk Status Updater
Hi,
I want to make a program in C# (rather Win-Service), that would real text from XML file, and update GoogleTalk, Status message with it.
I need API's, interoperability guidence on this.
What can u do for me?
....
Ravie Busie
Coding is my birth-right and bugs are part of feature my code has!
|
|
|
|
|
RaviBusie wrote: What can u do for me?
Direct you to Google to look for a GoogleTalk API. Wow, look what happened[^] when I typed "googletalk api" into Googles search bar.
|
|
|
|
|
I can pray for you
I are troll
|
|
|
|
|
|
...
Ravie Busie
Coding is my birth-right and bugs are part of feature my code has!
|
|
|
|
|
I'm experiencing a deserialisation problem which pares down to the following snippet:
[Serializable]
public class SomeClass
{
private SomeHandler<int> m_receiver;
public SomeClass()
{
m_receiver = Enter<int>;
}
protected virtual void Enter()
{ }
protected virtual void Enter<T>(T data)
{ }
}
public delegate void SomeHandler<T>(T data);
class Program
{
static void Main(string[] args)
{
SomeClass x1 = new SomeClass();
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream()) {
formatter.Serialize(ms, x1);
ms.Seek(0, SeekOrigin.Begin);
SomeClass x2 = (SomeClass)formatter.Deserialize(ms);
}
}
}
When I run this I get an ArgumentNullException way down in the stack, as follows:
at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
at System.Reflection.MemberInfoSerializationHolder.GetRealObject(StreamingContext context)
at System.Runtime.Serialization.ObjectManager.ResolveObjectReference(ObjectHolder holder)
at System.Runtime.Serialization.ObjectManager.DoFixups()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
...
If I remove the non-generic Enter() method from SomeClass then it works as expected. So what's wrong?
Thanks,
Chris.
modified on Wednesday, January 21, 2009 5:04 AM
|
|
|
|
|
Hi All,
I have a BCP utility runs by a job CTS in Windows schedular. It is working properly, but the problem is I want the BCP to throw a proper exception and status message to CTS when exception occurs. So that the CTS can send emails with proper message to a particular team.
Can any body please help me in completing this task. I am writing some BCP code sample below. Any kind of help can be very much thankfull.
Please let me know if you need any more information to help.
Thanks a Lot,
Aleem Mohammad.
isql -Q"IF EXISTS(SELECT 1 from %db_name_src%.dbo.DB_STATUS WHERE UPPER(Status) <> 'AVAILABLE' OR CONVERT(char(10),Data_as_of,101) <> CONVERT(char(10),getdate(),101)) BEGIN RAISERROR('An error has occured',16, 1) END" -b -c -S%srv_name_src% -U%login_src% -P%password_src%
if %errorlevel% EQU 1 GOTO :data_unavailable
rem ********* starting the bulk copy for export**********************************************************************************************************************************
rem ********* Accounts table data**********************************************************************************************************************************
bcp "SELECT account_id,Accountant_Cd from %db_name_src%.dbo.ACCOUNT WHERE Control_ID NOT IN ('0412','0361','0410','0364','0441','0442','0394','0395','0396','0444','0373','0416','0392') AND Account_Status_Cd ='0'" queryout %file_path%ACCOUNT.dat -c -S%srv_name_src% -U%login_src% -P%password_src% >%file_path%\PMMIR_Fetch_ODR_data_Tax_Detail_log.log -e %file_path%\Error.TXT || goto error_handler
rem ********* Asset table data*************************************************************************************************************************************
bcp "SELECT Property_Num,CUSIP_ID,Ticker_Symbol_ID,Asset_Short_Nm,Current_Prc FROM %db_name_src%.dbo.ASSET WHERE Unique_Asset_Fl ='0'" queryout %file_path%ASSET.dat -c -S%srv_name_src% -U%login_src% -P%password_src% >>%file_path%\PMMIR_Fetch_ODR_data_Tax_Detail_log.log -e %file_path%\Error.TXT || goto error_handler
rem ********* Accountbalance table data**********************************************************************************************************************************
bcp "SELECT ACCOUNT_BALANCE.Account_ID,Fiscal_Year_Fed_ST_Gain_Amt,Fiscal_Year_Fed_ST_Loss_Amt,Fiscal_Year_Fed_LT_Gain_Amt,Fiscal_Year_Fed_LT_Loss_Amt FROM %db_name_src%.dbo.ACCOUNT_BALANCE as ACCOUNT_BALANCE , %db_name_src%.dbo.ACCOUNT as ACCOUNT WHERE ACCOUNT_BALANCE.Account_ID = ACCOUNT.Account_ID AND Control_ID NOT IN ('0412','0361','0410','0364','0441','0442','0394','0395','0396','0444','0373','0416','0392') AND Account_Status_Cd ='0'" queryout %file_path%ACCOUNT_BALANCE.dat -c -S%srv_name_src% -U%login_src% -P%password_src% >>%file_path%\PMMIR_Fetch_ODR_data_Tax_Detail_log.log -e %file_path%\Error.TXT || goto error_handler
rem ********* ACCOUNTANT table data**********************************************************************************************************************************
bcp "SELECT distinct ACCOUNTANT.Accountant_Cd,Accountant_nm FROM %db_name_src%.dbo.ACCOUNTANT as ACCOUNTANT, %db_name_src%.dbo.ACCOUNT as ACCOUNT WHERE ACCOUNTANT.Accountant_Cd = ACCOUNT.Accountant_Cd AND Control_ID NOT IN ('0412','0361','0410','0364','0441','0442','0394','0395','0396','0444','0373','0416','0392') AND Account_Status_Cd ='0'" queryout %file_path%ACCOUNTANT.dat -c -S%srv_name_src% -U%login_src% -P%password_src% >>%file_path%\PMMIR_Fetch_ODR_data_Tax_Detail_log.log -e %file_path%\Error.TXT || goto error_handler
rem ********* TaxLot Details table data**********************************************************************************************************************************
bcp "SELECT ACCOUNT.Account_ID,Property_Num,Portfolio_Num,Shares_Par_Value_Qty,Federal_Tax_Acquisition_Dt,Federal_Tax_cost_Amt, 0 as Unique_Asset_fl FROM %db_name_src%.dbo.TAX_DETAIL as TAX_DETAIL,%db_name_src%.dbo.ACCOUNT as ACCOUNT WHERE Unique_Asset_fl = 0 AND TAX_DETAIL.Account_ID = ACCOUNT.Account_ID AND Control_ID NOT IN ('0412','0361','0410','0364','0441','0442','0394','0395','0396','0444','0373','0416','0392') AND Account_Status_Cd ='0'" queryout %file_path%TAX_DETAIL.dat -c -S%srv_name_src% -U%login_src% -P%password_src% >>%file_path%\PMMIR_Fetch_ODR_data_Tax_Detail_log.log -e %file_path%\Error.TXT || goto error_handler
rem ********* DB_STATUS table data**********************************************************************************************************************************
bcp "SELECT Last_Update,Status_Code,Application,Status,Data_As_Of FROM %db_name_src%.dbo.DB_STATUS" queryout %file_path%DB_STATUS.dat -c -S%srv_name_src% -U%login_src% -P%password_src% >>%file_path%\PMMIR_Fetch_ODR_data_Tax_Detail_log.log -e %file_path%\Error.TXT || goto error_handler
rem ********* starting the bulk copy for Import**********************************************************************************************************************************
rem ********* Update DB_STATUS table data**********************************************************************************************************************************
isql -U%login_dest% -d%db_name_dest% -Q"UPDATE DB_STATUS SET Application = 'BB', Status = 'UnAvailable', Status_Code = 0, Last_Update = GETDATE()" -S%srv_name_dest% -U%login_dest% -P%password_dest% >>%file_path%\PMMIR_Fetch_ODR_data_Tax_Detail_log.log -e %file_path%\Error.TXT || goto error_handler
rem ********* Accounts table data**********************************************************************************************************************************
isql -U%login_dest% -d%db_name_dest% -Q"truncate table ACCOUNT" -S%srv_name_dest% -P%password_dest%
bcp %db_name_dest%.dbo.ACCOUNT in %file_path%\ACCOUNT.dat -c -S%srv_name_dest% -U%login_dest% -P%password_dest% -b10000 >>%file_path%\PMMIR_Fetch_ODR_data_Tax_Detail_log.log -e %file_path%\Error.TXT || goto error_handler
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
|
|
|
|
|
Hi All,
I have a BCP utility runs by a job CTS in Windows schedular which copies data from
database1 to database2. When it is seleting data from database1 it will make a ".data"
and keeps data in to it.
Normally it is working fine, but the problem is when the ".data" files size is more
than the drive's free size, it is uanble to copy and we are not getting proper exception.
I want the message to be returned to CTS so that CTS can email to team members. Please give
me suggestions or links.
Can any body please help me in completing this task. I am writing some BCP code
sample below. Any kind of help can be very much thankfull.
Please let me know if you need any more information to help.
Thanks a Lot,
Aleem Mohammad
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
modified on Wednesday, January 21, 2009 2:24 AM
|
|
|
|
|
So what exception IS being thrown and what does the code look like that writes this file??
|
|
|
|
|
bcp returns 0 for errorlevel if the operation was succesfull and if it wasn't, errorlevel is not 0.
Beyond that the only way to get more info about the error is to examine the contents
of the error log file (specified by -e parameter)
|
|
|
|
|
How to implement code for html
|
|
|
|
|
First, use a better subject line. Second, read some documentation or books.
only two letters away from being an asset
|
|
|
|
|
...
"Well, we're getting "F"'d at work. WPF, WCF, and WWF... WTF?" --John Simmons
|
|
|
|
|
Smtnri wrote: How to implement code for html
You've got a couple of choices. The first choice would be to write some code like a real coder, but if I were you I'd go for the hardcore option and whack it out using ones and zeroes.
|
|
|
|
|
I'm so very sorry. Every computer has an IQ requirement of 42. You don't meet this requirement, so pack your computer away, and send it back, stating reason code I D Ten T
|
|
|
|
|
|