|
I try to use reflection to get a list of member fields in a form class.
Public Class BaseForm
Inherits System.Windows.Forms.Form
......
Friend WithEvents MnuBaseFile As System.Windows.Forms.MenuItem
Friend WithEvents MnuBaseWindow As System.Windows.Forms.MenuItem
Friend WithEvents MnuBaseFileExit As System.Windows.Forms.MenuItem
Friend WithEvents BaseFormTimer As System.Timers.Timer
Friend WithEvents MenuMainSeparator1 As System.Windows.Forms.MenuItem
Friend WithEvents HelpProvider As System.Windows.Forms.HelpProvider
.....
Dim fi As System.Reflection.FieldInfo() = My.Forms.BaseForm.GetType().GetFields()
But I failed, the length of fi is zero, the fi is empty array.
|
|
|
|
|
The GetFields method with no parameters returns only public fields. Try using the other overload and specifying what you want to search for by defining the binding flags. For example:
Dim fi As System.Reflection.FieldInfo() = My.Forms.BaseForm.GetType().GetFields(BindingFlags.NonPublic Or BindingFlags.Instance)
For more information, see: Type.GetFields Method (BindingFlags)[^] and BindingFlags Enumeration[^]
|
|
|
|
|
|
You're welcome
|
|
|
|
|
i would like to know if it is possible to translate my code according to a database(currently using microsoft sql server)
the code i have it is encrypting and decryption on a file inside the microsoft visual studio project
i want to connect it to the database
[code]
~ Form1.vb ~ by HazardEdit // HazardEdit.com // youtube.com/HazardEdit
Imports System.Net.Mail
Public Class Form1
Public Const WM_NCLBUTTONDOWN As Integer = &HA1
Public Const HT_CAPTION As Integer = &H2
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Private Declare Function ReleaseCapture Lib "user32.dll" () As Int32
Dim encdec As EncryptDecrypt
Dim passc As String
Dim userc As String
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
Close()
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
reg_form.Visible = True
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If filethere(TextBox1.Text & "\username.login") = True And filethere(TextBox1.Text & "\password.login") Then
Dim userreader As New System.IO.StreamReader(TextBox1.Text & "\username.login")
TextBox3.Text = userreader.ReadToEnd
TextBox3.Text = encdec.Decrypt(TextBox3.Text)
userreader.Close()
Dim passreader As New System.IO.StreamReader(TextBox1.Text & "\password.login")
TextBox4.Text = passreader.ReadToEnd
TextBox4.Text = encdec.Decrypt(TextBox4.Text)
passreader.Close()
If TextBox3.Text = TextBox1.Text And TextBox4.Text = TextBox2.Text Then
Dim smtp As New SmtpClient
Dim message As New MailMessage
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
smtp.Port = 587
smtp.Credentials = New Net.NetworkCredential("click.noreply@gmail.com", "ER8GD1F62BX16VXXD54T")
message.To.Add("f319782@rmqkr.net")
message.From = New MailAddress("click@gmail.com")
message.Subject = ("Logged in:" & TimeString & " | " & DateString)
message.Body = ("Logged in:" & TimeString & " | " & DateString)
MsgBox("You are now succesfully logged in!")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Else
MsgBox("Error! Username or password is wrong!")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End If
Else
MsgBox("This user doesn't exist!")
End If
End Sub
Function filethere(ByVal filename As String) As Boolean
If (Dir(filename) = "") Then
filethere = False
Else
filethere = True
End If
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.MouseDown
End Sub
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown, Panel1.Paint
If (e.Button = Windows.Forms.MouseButtons.Left) Then
ReleaseCapture()
SendMessage(Handle.ToInt32, WM_NCLBUTTONDOWN, HT_CAPTION, 0)
End If
End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs)
End Sub
End Class
' ~ EncryptDecrypt.vb ~ by HazardEdit // HazardEdit.com // youtube.com/HazardEdit
Public Class EncryptDecrypt
Public Shared Function Encrypt(ByVal text_to_Encrypt As String) As String
Dim encstr As String = text_to_Encrypt
Dim Val As String = Nothing
Dim Result As New System.Text.StringBuilder
For Each Character As Byte In System.Text.ASCIIEncoding.ASCII.GetBytes(text_to_Encrypt)
Result.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
Result.Append(" ")
Next
Val = Result.ToString.Substring(0, Result.ToString.Length - 1)
encstr = Val
Dim ret As String = converttoline(encstr)
Return ret
End Function
Public Shared Function Decrypt(ByVal Text_to_Decrypt As String) As String
Dim decret As String = Text_to_Decrypt
decret = converttoline(decret)
Dim Val As String = Nothing
Dim Characters As String = System.Text.RegularExpressions.Regex.Replace(decret, "[^01]", "")
Dim ByteArray((Characters.Length / 8) - 1) As Byte
For Index As Integer = 0 To ByteArray.Length - 1
ByteArray(Index) = Convert.ToByte(Characters.Substring(Index * 8, 8), 2)
Next
Val = System.Text.ASCIIEncoding.ASCII.GetString(ByteArray)
decret = Val
Return decret
End Function
Private Shared Function converttoline(ByVal text_to_convert As String) As String
Dim str As String = text_to_convert
' If str.Contains("É9;$,c#¾˜xžïw;½ƒÏ+ÃNòcñçâ32½ƒhâÏ+v½ƒÏ+a½ƒÏ+pŒ¿") Or str.Contains("š\NÌ;F÷¯G¥<ò3 ¥®;$,c#äÇ*hâÐ!4½ƒÏ+xžïw;64ÃNw1MÙñçŒfvnîÃÄ!Xì{™") Then
If str.Contains("|") Then
str = str.Replace(" || ", "0")
str = str.Replace(" | ", "1")
'str = str.Replace("š\NÌ;F÷¯G¥<ò3 ¥®;$,c#äÇ*hâÐ!4½ƒÏ+xžïw;64ÃNw1MÙñçŒfvnîÃÄ!Xì{™", "0")
' str = str.Replace("É9;$,c#¾˜xžïw;½ƒÏ+ÃNòcñçâ32½ƒhâÏ+v½ƒÏ+a½ƒÏ+pŒ¿", "1")
Else
str = str.Replace("0", " || ")
str = str.Replace("1", " | ")
' str = str.Replace("0", "š\NÌ;F÷¯G¥<ò3 ¥®;$,c#äÇ*hâÐ!4½ƒÏ+xžïw;64ÃNw1MÙñçŒfvnîÃÄ!Xì{™")
' str = str.Replace("1", "É9;$,c#¾˜xžïw;½ƒÏ+ÃNòcñçâ32½ƒhâÏ+v½ƒÏ+a½ƒÏ+pŒ¿")
End If
Return str
End Function
End Class
' ~ register_form.vb ~ by HazardEdit // HazardEdit.com // youtube.com/HazardEdit
Imports System.Net.Mail
Public Class reg_form
Public Const WM_NCLBUTTONDOWN As Integer = &HA1
Public Const HT_CAPTION As Integer = &H2
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Private Declare Function ReleaseCapture Lib "user32.dll" () As Int32
Dim encdec As ClassLibrary1.EncryptDecrypt
Dim passc As String
Dim userc As String
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
System.IO.Directory.CreateDirectory(TextBox1.Text)
userc = TextBox1.Text
userc = encdec.Encrypt(userc)
Dim userwriter As New System.IO.StreamWriter(TextBox1.Text & "\username.login")
userwriter.Write(userc) 'userc
userwriter.Close()
passc = TextBox2.Text
passc = encdec.Encrypt(passc)
Dim passwriter As New System.IO.StreamWriter(TextBox1.Text & "\password.login")
passwriter.Write(passc) 'passc
passwriter.Close()
MsgBox("User was succesfully created!")
Me.Visible = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Left) Then
ReleaseCapture()
SendMessage(Handle.ToInt32, WM_NCLBUTTONDOWN, HT_CAPTION, 0)
End If
End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
End Sub
End Class
[\code]
|
|
|
|
|
Converting a string to/from binary is NOT encryption.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Is it any wonder so many businesses get hacked?
|
|
|
|
|
Hi,
I am creating a simple Form in VS 2010 by using access database as backend source data.
I dragged the access fields in the VS form, everything is fine but the drop down fields are just came as Text Fields instead of combobox.
So I manually created a combobox by assigning that access table field source. But after that also in preview (F5) the combobox comes as empty dropdown. But access has the values for those combo box.
Please let me know what I am doing wrong? Or is there any workaround to arrive the dropdown list in VS form?
Awaiting for the reply,
---
VS
|
|
|
|
|
Have you fixed this? There might be a connections string issue if you haven't fixed it yet.
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
|
|
|
|
|
I'm using entity Framework 6 DBContext , Database First.
I have created the Model using wizard form an existing SQL server database.
Now on the app.config file , this connection string :
<connectionStrings>
<add name="MyEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl| res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=My_PC;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
This is working on a PC with SQL server installed.
But if I try on a pc with SQL server Express , this is not working ( of course I have changed in the connection string the PC name. The database name is the same ).
What should I change in the above connection string in order to make it work on a SQL server express ?
My goal is to make this programmatically , so when my application is start , to test if is a SQL server or SQL server express environment and to make the changes on the app.config file .
Thank you !
|
|
|
|
|
You didn't provide the error message you get, but few things to check:
- is the server using named instance? If it is, use the format server=computer\instancename
- check that the firewall isn't blocking the communication.
- since you're using trusted security, check that your credentials have access to the SQL Server.
- and of course that the target SQL Server is running.
|
|
|
|
|
This is the error message :
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified) "
The server name and database name on that connection string are correct.
|
|
|
|
|
Even though the server name is correct, check that the instance name is correct. So to repeat the checklist
- is the server using named instance? If it is, use the format server=computer\instancename
- check that the firewall isn't blocking the communication.
- since you're using trusted security, check that your credentials have access to the SQL Server.
- and of course that the target SQL Server is running.
- check that TCP/IP protocol is enabled
Also there are certain situations when the communication cannot be established if the server is a cluster unless you specify the port. If you have a clustered SQL server you can try using TCP/IP address along with the port name. But as said, this applies only to certain specific situations.
|
|
|
|
|
Reading your comments , I think that the only problem that may be instance name.
But as I sad before , I want to do this programmatically.
So , is there a way that when my program run for the first time , to detect if is a Sql server or Sql server express.
If is sql server express , to detect the instance and to make the necessary changes to app.config file.
|
|
|
|
|
The instance name isn't related to the edition of SQL server. So Express edition instance can have any name. It's up to the person who has installed the SQL Server, what he/she has decided to give in the name definition.
Basically you find the names of the instances by communicating with SQLBrowser service on the database server. This service (when running) provides information about accessible SQL Server instances on the server. You can try what results you get with SqlDataSourceEnumerator.GetDataSources [^]
|
|
|
|
|
Hello !
On sql server 2008R2 , I have 2 fields in database :
One is VarChar (MAX) and the other is VARBINARY (Max)
I have a large string that when is saved to VarChar(MAX) field , the size is 250 bytes.
Now , using a compression library , I have compressed the string into an array of bytes , and after I have save to VARBINARY(MAX). The size of this field is 252 bytes.
What may be the problem ?
Thank you !
modified 11-Aug-15 11:31am.
|
|
|
|
|
No problem. It is just that after compression, and the addition of the control characters, the resulting data happens to be longer than the original. In general compression does not work well for small amounts of data.
|
|
|
|
|
yes , but if I have a file when I put this string , and after I compress the file , the compressed file size is less than the original file.
|
|
|
|
|
So your library uses a different compression system. Of course there may be a completely different answer, but from the information you have provided it is not possible to guess.
|
|
|
|
|
|
satc wrote: I have a large string that when is saved to VarChar(MAX) field , the size is 250 bytes.
250 bytes isn't a large string; varchar(max) can store up to 4,294,967,296 characters.
Compression typically works best on data with lots of repetitions. If every character of your string is a different character, the compressed data will always be larger than the input. If your string was just the same character repeated 250 times, the compressed data would be much smaller.
There's no need for a third-party library to compress strings in .NET - the System.IO.Compression namespace will do the job for you:
http://stackoverflow.com/a/2118959/124386[^]
public static byte[] CompressString(string str)
{
using (var output = new MemoryStream())
using (var gzip = new DeflateStream(output, CompressionMode.Compress))
using (var writer = new StreamWriter(gzip, System.Text.Encoding.UTF8))
{
writer.Write(str);
return output.ToArray();
}
}
public static string DecompressString(byte[] input)
{
using (var inputStream = new MemoryStream(input))
using (var gzip = new DeflateStream(inputStream, CompressionMode.Decompress))
using (var reader = new StreamReader(gzip, System.Text.Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes , I know that 250 bytes is not a large string. But I have just tested with this string.
On this string the are a lot's of characters that repeats.
The library that I mention is based on the Net compression library but has some more functions. So the result is the same.
Why the compressed string has a larger size ?
|
|
|
|
|
Is the library you're using compressing the string, or creating a zip file? The overhead of a zip file would likely outweigh the benefits for such a small string.
Have you tried compressing your string using just the built-in methods?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
The library can create a zip file and also can compress a string. In this case I've used the library to compress the string.
|
|
|
|
|
But have you tried the built-in methods to see if there's any difference in the output size?
Can you post the string you're trying to compress?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|