Visual basic Windows forms BitArray convert to string - vb.net

Hi I made a code and it outputs random characters and unknown symbols.
The code is:
Function ReceiveMessages() As String
Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint)
from = RemoteIpEndPoint.Address.ToString
Dim BitDet As BitArray
BitDet = New BitArray(receiveBytes)
Dim strReturnData As String = _
System.Text.Encoding.Unicode.GetString(receiveBytes)
rt = strReturnData
I don't think it is a problem with the receiving but it might.
This function is called by:
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Try
receivingUdpClient = New System.Net.Sockets.UdpClient(11211)
Catch ex As Exception
End Try
ThreadReceive = _
New System.Threading.Thread(AddressOf ReceiveMessages)
ThreadReceive.Start()
End Sub
If you could help I would appreciate it.

Related

Visual Basic time synchronizing

I am working oin a project in vb.net although I am not an expert in it, I just used it because U think it is the best for this kind of problem.
I have a project with two buttons and a label; first button is for sync windows date from a server and the other is to change the windows date to (2014, 11, 16). I am doing this because some programs I have doesn't run unless the date is this one and as you know browser must be the real time to run this is the idea of this project.
The second button is working perfectly, but the sync date button doesn't work and throws this error in my label
No connection because the target machine refused to connect
Here is my function and my server ip
Public Function GetNISTTime(ByVal host As String) As String
Dim timeStr As String = ""
Try
Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
LastSysTime = DateTime.UtcNow()
timeStr = reader.ReadToEnd()
reader.Close()
Catch ex As SocketException
GetNISTTime = ex.Message
Exit Function
Catch ex As Exception
GetNISTTime = ex.Message
Exit Function
End Try
'Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))
'Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))
'Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))
'Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))
'Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))
'Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))
'Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))
'Dim Temp As Integer = CInt(AscW(timeStr(7)))
Return timeStr ' New DateTime(yr + 2000, mo, dy, hr, mm, sc)
End Function
and the button
Private Sub real_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles real.Click
GetNISTTime("mail.harf.com.sa")
Label1.Text = GetNISTTime("mail.harf.com.sa").ToString
End Sub
I think the problem is because of the server but I didn't find any dns server that does sync successfully.
This is my program download link if you want to see the problem in with your eyes (you should run it as adminstrator)
http://www.mediafire.com/file/wfw5jpag8w2hofb/Release.rar/file
Also it must be dns in Saudi Arabia time zone
Your function call is not correct.
Private Sub real_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles real.Click
GetNISTTime("mail.harf.com.sa")
Label1.Text = GetNISTTime("mail.harf.com.sa").ToString
End Sub
should be:
Private Sub real_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles real.Click
Label1.Text = GetNISTTime("mail.harf.com.sa")
End Sub
GetNISTTime is a function that returns a string, so your original first line (GetNISTTime("mail.harf.com.sa")) does the work but nothing is done with the return value. Your original second line takes a return value that is a string and then tries to convert it to a string.
In addition, your function may not return anything if an error occurs. You have used a VBA style assignment in the catch block. Instead, try:
Public Function GetNISTTime(ByVal host As String) As String
Dim timeStr As String = ""
Try
Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
LastSysTime = DateTime.UtcNow()
timeStr = reader.ReadToEnd()
reader.Close()
Catch ex As SocketException
return ex.Message
Catch ex As Exception
Return ex.Message
End Try
'any other stuff
Return timeStr
End Function
so i did like what Visual Vincent say and this is my code after editing it
and it worked perfectly with me just need administrator permissions
code
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Runtime.InteropServices
Public Class Daytime
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim d As DateTime
d = "12:52:00"
Try
Microsoft.VisualBasic.TimeOfDay = d 'Your time...
Microsoft.VisualBasic.DateString = New Date(2014, 11, 16) 'The date...
Catch ex As Exception
'You might have to run as Administrator...?
End Try
End Sub
Private Sub real_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles real.Click
Process.Start("CMD", "/C net start w32time & w32tm /resync /force & pause")
End Sub
End Class

An unhandled exception of type 'System.InvalidOperationException' occurred

I have made an E-mailer like program. When I start it, it gives me this Error message:
An unhandled exception of type 'System.InvalidOperationException' occurred in Menü (VB Version 1.0).exe
My Code
Imports System.Net.Mail
Public Class Email
Dim Tárgy As String = TextBox4.Text
Dim Üzenet As String = TextBox5.Text
Dim UserEmail As String = TextBox1.Text
Dim UserPass As String = TextBox2.Text
Dim ToEmail As String = TextBox3.Text
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyMailMessage As New MailMessage()
Try
MyMailMessage.From = New MailAddress(UserEmail)
MyMailMessage.To.Add(ToEmail)
MyMailMessage.Subject = (Tárgy)
MyMailMessage.Body = (Üzenet)
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential((UserEmail), (UserPass))
SMTP.Send(MyMailMessage)
Catch ex As Exception
End Try
End Sub
End Class
Move the initialization of your strings inside the Click event. You don't need them before and doing it at the global level will cause an exception because the TextBoxes and other controls are not yet initialized
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Tárgy As String = TextBox4.Text
Dim Üzenet As String = TextBox5.Text
Dim UserEmail As String = TextBox1.Text
Dim UserPass As String = TextBox2.Text
Dim ToEmail As String = TextBox3.Text
Dim MyMailMessage As New MailMessage()
....
SMTP.Send(MyMailMessage)
End Sub
Also, if you don't do anything with the exception then do not use empty trt/catch blocks. If an exception occurs then you can't see the error.

Read text file with delimiter

i want to read text file and from the fifth line take only the first column before (;) this my code to browse and read text :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OpenFile As New OpenFileDialog
OpenFile.FileName = ""
OpenFile.Filter = "Fichier Texte (*.pnp)|*.pnp"
OpenFile.ShowDialog()
Try
Dim lire As New System.IO.StreamReader(OpenFile.FileName)
RichTextBox1.Text = lire.ReadToEnd
lire.Close()
Catch ex As Exception
End Try
End Sub
End Class
help me please
You could try something like this:
Dim lineYouWantToRead As Int32 = 5
Dim fieldYouWantToRead As Int32 = 1
Dim capturedValue As String = ""
Using fileReader As New FileIO.TextFieldParser(OpenFile.FileName)
fileReader.TextFieldType = FileIO.FieldType.Delimited
fileReader.SetDelimiters(";")
While fileReader.LineNumber <= lineYouWantToRead - 1
Dim currentLine As String() = fileReader.ReadFields()
capturedValue = currentLine(fieldYouWantToRead - 1)
End While
End Using
RichTextBox1.Text = capturedValue
Let us know if that is any help.

Does not taking the update data

I have One Dataset binding in Reportviewer, My dataset Have two Paramters, in Report loud data will displays in normally put the problem is when i pass the paremeters in new values reportviewer and I click search Button the previous data in reportviewer does meat any changes. means the newer searched data will not be displayed!
Here is my Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mysession()
ReportViewer1.ProcessingMode = ProcessingMode.Local
Dim rep As LocalReport = ReportViewer1.LocalReport
rep.ReportPath = "Leaving Report.rdlc"
Dim ds1 As DataSet = GetData()
Dim EMpReplt As New ReportDataSource()
EMpReplt.Name = "ProlDataSet_Lrepoert"
EMpReplt.Value = ds1.Tables("EMPData")
rep.DataSources.Add(EMpReplt)
End sub
Private Function GetSalesData()
Dim ds As New DataSet
Dim sql As String = "select * from laeve where Status='" & DropDownList1.SelectedValue & "' and Agent='" & Session("Agence") & "'"
Dim command As New SqlCommand(sql,con)
Dim mysqlAdapter As New SqlDataAdapter(command)
mysqlAdapter.Fill(ds, "EMPData")
mysqlAdapter.Dispose()
command.Dispose()
End Using
Return ds
End Function
End Sub
Protected Sub Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
ReportViewer1.LocalReport.Refresh()
End Sub
Try to place the .IsPostBack Using the WebForms ReportViewer Control
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mysession()
If Not Page.IsPostBack Then
ReportViewer1.ProcessingMode = ProcessingMode.Local
Dim rep As LocalReport = ReportViewer1.LocalReport
rep.ReportPath = "Leaving Report.rdlc"
Dim ds1 As DataSet = GetData()
Dim EMpReplt As New ReportDataSource()
EMpReplt.Name = "ProlDataSet_Lrepoert"
EMpReplt.Value = ds1.Tables("EMPData")
rep.DataSources.Add(EMpReplt)
End if
End sub

Converting multiple Images to pdf using pdfsharp

I am trying to convert multiple images to pdf using pdfsharp library.
I am able to convert single image and it works pretty well.
And while converting bulk images to single pdf I am facing problem that it takes all the images and converts them but after conversion If I check it shows me only the last image as it is not appending to the existing image and it overwrites the previous image.
So how do I rectify this?
Any help will be appreciated as I am first time working with pdf library and point me out If I am doing any mistake.And I will be gald to know more about this and I don't feel though If you pointed me out the mistake I have done.
Here is my code:
Private Sub btnAddFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddFolder.Click
If Me.FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim f As New DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath)
Dim fso As New System.Object
For Each file As FileInfo In f.GetFiles
Select Case file.Extension.ToLower
Case ".jpg", ".bmp", ".gif", ".png"
Me.ThumbControl1.BackgroundImage = Nothing
Me.CheckedListBox1.Items.Add(file.FullName, CheckState.Checked)
Me.ThumbControl1.AddThumbnail(file.FullName)
Me.ThumbControl1.BackgroundImage = Nothing
Me.CheckedListBox1.SelectedIndex = 0
End Select
Next
End If
End Sub
Background worker:
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bw.DoWork
For pix As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
Try
Dim source As String = CheckedListBox1.Items(pix).ToString()
Dim destinaton As String = (TryCast(e.Argument, String()))(1)
Dim doc As New PdfDocument()
doc.Pages.Add(New PdfPage())
Dim xgr As XGraphics = XGraphics.FromPdfPage(doc.Pages(0))
Dim img As XImage = XImage.FromFile(source)
xgr.DrawImage(img, 0, 0)
doc.Save(destinaton)
doc.Close()
success = True
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Next
End Sub
Convert button:
Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvert.Click
bw.RunWorkerAsync(New String(1) {srcFile, destFile})
End sub
Saving Pdf:
Private Sub btnSelectDest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelectDest.Click
sfdDestFile.Filter = "PDF Files(*.pdf)|*.pdf"
If sfdDestFile.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then
Return
End If
destFile = sfdDestFile.FileName
End Sub
The problem is that you are creating a new PDF document on each pass through the loop. You need to move this outside the loop. Also, you are referencing page 0, not page pix. Here is how I would fix it:
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bw.DoWork
Dim doc As New PdfDocument()
For pix As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
Try
Dim source As String = CheckedListBox1.Items(pix).ToString()
Dim oPage As New PDFPage()
doc.Pages.Add(oPage)
Dim xgr As XGraphics = XGraphics.FromPdfPage(oPage)
Dim img As XImage = XImage.FromFile(source)
xgr.DrawImage(img, 0, 0)
success = True
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Next
Dim destinaton As String = (TryCast(e.Argument, String()))(1)
doc.Save(destinaton)
doc.Close()
End Sub